Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Tech Guides

852 Articles
article-image-transfer-learning
Graham Annett
07 Oct 2016
7 min read
Save for later

Transfer Learning

Graham Annett
07 Oct 2016
7 min read
The premise of transfer learning is the idea that a model trained on a particular dataset can be used and applied to a different dataset. While the notion has been around for quite some time, very recently it's become useful along with Domain Adaptation as a way to use pre-trained neural networks for highly specific tasks (such as in Kaggle competitions) and various fields. Prerequisites For this post, I will be using Keras 1.0.3 configured with TensorFlow 0.8.0. Simple Overview and Example Before using VGG-16 with pre-trained weights, let’s first use a simple example on our own small net to see how it works. For this example we will be using a MNIST trained net and then fine-tuning the last layers to allow for it to predict on a dataset of smiling or not smiling images. from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.utils.np_utils import to_categorical from scipy.misc import imresize def rgb_g(img): grayscaled = 0.2989 * img[:,0,:,:] + 0.5870 * img[:,1,:,:] + 0.1140 * img[:,2,:,:] return grayscaled (X, Y), (_, _) = cifar10.load_data() nb_classes = len(np.unique(Y_train)) Y = np_utils.to_categorical(Y, nb_classes) X = X.astype('float32')/255. # converts 3 channels to 1 and resizes image X = rgb_g(X) X_tmp = [] for i in range(X.shape[0]): X_tmp.append(imresize(X[i], (28,28))) X = np.array(X_tmp) X = X.reshape(-1,1,28,28) model = Sequential() model.add(Convolution2D(32,3,3, border_mode='same', input_shape=(1,28,28))) model.add(Activation('relu')) model.add(Convolution2D(32,3,3, border_mode='same')) model.add(Activation('relu')) model.add(MaxPooling2D((2,2))) model.add(Dropout(.25)) model.add(Flatten()) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adadelta') One thing to notice is that our input for the neural net is 1x28x28. This is important, because as the data we feed in must match this dimension, the MNIST and CIFAR datasets are not images of the same size or number of color channels (MNIST is 1x28x28 while CIFAR10 is 3x32x32, where the first image represents the number of channels in the image). There are a few ways to accommodate this, but generally you are working with what the prior weights and model were trained on and must resize and adjust your input accordingly (for instance, grayscaled images can be repeated from 1 channel into 3 channels to use on RGB trained models). With this model we now will load data from MNIST and fit again, but only fine tune on the last few layers. First let’s look at the model and some of the features of the model. > model.layers [<keras.layers.convolutional.Convolution2D at 0x1368fe358>, <keras.layers.core.Activation at 0x1368fe3c8>, <keras.layers.convolutional.Convolution2D at 0x136905ba8>, <keras.layers.core.Activation at 0x136905898>, <keras.layers.convolutional.MaxPooling2D at 0x136930828>, <keras.layers.core.Dropout at 0x136930860>, <keras.layers.core.Flatten at 0x136947550>, <keras.layers.core.Dense at 0x136973240>, <keras.layers.core.Activation at 0x136973780>, <keras.layers.core.Dropout at 0x13697ef98>, <keras.layers.core.Dense at 0x136988a20>, <keras.layers.core.Activation at 0x136e29ef0>] > model.layers[0].trainable True With keras, we have the ability to specify whether we want a layer to be trainable or not. A trainable layer means that its weights that are learned via fitting the model will update. For this experiment we will be doing what is called fine tuning on only the last layer without changing the number of classes. We still want to keep the last few layers so we will set all the layers but the last 2 to be trainable such that the learned weights will stay the same: for l in range(len(model.layers)-2): model.layers[l].trainable=False model.compile(loss='categorical_crossentropy', optimizer='adadelta') Note: we must also recompile every time we adjust the model's layers. This is oftentimes a tedious process with Theano so can be useful when initially experimenting to use TensorFlow. Now we can train a few epochs on the MNIST dataset and see how well it's priorly learned weights work. (X_mnist, y_mnist), (_, _) = mnist.load_data() y_mnist = np_utils.to_categorical(y_mnist) X_mnist = X_mnist.reshape(-1,1,28,28) model.fit(X_mnist, y_mnist, batch_size=32, nb_epoch=5, validation_split=.2) We can also train on the dataset but use different final layers in the model. If, for instance, you were interested in fine tuning the model based on some dataset with 1 single binary classification, you could do something like: model.pop() model.pop() model.add(Dense(1)) model.add(Activation('softmax')) model.train(x_train, y_train) While this example is quite small and the weights are easily learned, the premise that network weights that took a few days or even weeks to learn isn't that uncommon. Also, having a large pre-trained network can be useful to both gauge your own network results as well as to incorporate into other aspects of your deep learning model. Using VGG-16 for Transfer Learning There are a few well known pre-trained models and weights that while plausibly you could train on your own computer, often the training time is much too long [D1] and requires specialized hardware to train. VGG-16 is perhaps one of the better known of these, but there are many others and Caffe has a nice listing of them. Using the VGG-16 is quite simple and allows for a previously trained model that is quite adaptable without having to spend a large amount of time training. With this type of model, we are able to load the model and use these weights; then we can remove the final layers to change to for instance a binary classification problem. Using these pre-trained networks take specialized hardware usually and require the it [D2] may not work on all computers and GPU's. You need to download the pre-trained weights available here and there is also a gist explaining the general use of it. from keras.models import Sequential from keras.layers.core import Flatten, Dense, Dropout from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D from keras.optimizers import Adam model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) model.load_weights('vgg16_weights.h5') for l in model.layers[:-2]: l.trainable = False model.layers.pop() model.layers.pop() model.add(Dropout(0.5)) model.add(Dense(1, activation='softmax')) model.compile(optimizer=Adam() loss='categorical_crossentropy', metrics=['accuracy']) You should now be able to try out your own models to experience the benefits of transfer learning. About the author Graham Annett is an NLP engineer at Kip.  He has been interested in deep learning for a bit over a year and has worked with and contributed to Keras.  He can be found on GitHub or Here..
Read more
  • 0
  • 0
  • 4280

article-image-ibm-machine-learning-what-it-and-what-does-it-mean-data-scientists
Graham Annett
11 Apr 2017
5 min read
Save for later

IBM Machine Learning: What Is It, and What Does It Mean For Data Scientists?

Graham Annett
11 Apr 2017
5 min read
Like many cloud providers, IBM is invested and interested in developing and building out their machine learning offerings. Because more and more companies are interested in trying to figure out what machine learning can currently do and what it is on the cusp of doing, there has been a plethora of new services and companies trying to see if they can either do anything new or be better than the many other competitors in the market. While much of the machine learning world is based around cloud computing and the ability to horizontally scale during training, the reality is that not every company is cloud based. While IBM has had many machine learning tools and services available on their various cloud platforms, the IBM Machine Learning system seems to be just an on-premise compromise to many of the previously cloud-based APIs that IBM offered under their machine learning and Watson brand. While I am sure many enterprise-level companies have a large amount of data that would previously not have been able to use these services, I’m unaware whether and skeptical that these services would be any better or more utilitarian than their cloud counterparts. This seems like it may be particularly useful to companies in industries with many regulations or worries about hosting data outside of their private network, although this mentality seems like it is being slowly eroded away and becoming outdated in many senses. One of the great things about the IBM Machine Learning system (and many similar companies as well) are the APIs that allow developers to pick whatever language they would like and allowing multiple frameworks because there are so many available and interesting options at the moment. This is a really important aspect for something like deep learning, where there is a constant barrage of new architectures and ideas that iterate upon prior ideas, but require new architecture and developer implementations. While I have not used IBM’s new system and will most likely not be in any particular situation where it would be available, I was lucky enough to participate in IBM’s Catalyst program and used a lot of their cloud services for a deep learning project and testing out many of their available Bluemix offerings. While I thought the system was incredibly nice and simple to use compared to many other cloud providers, I found the various machine learning APIs they offered either weren’t that useful or seemed to provide worse results than their comparable Google, Azure, and other such services. Perhaps that has changed, or their new services are much better and will be available on this new system, but it is hard to find any definitive information that this is true. One aspect of these machine learning tools that I am not excited about is the constant focus on using machine learning to create some business cost-savings models (which the IBM Machine Learning press release touts), which companies may state is passed onto the customers, but it seems like this is rarely the truth (and one of the things that was stressed on in the IBM Machine Learning launch event). The ability for machine learning methodology to solve tedious and previously complicated problems is much more important and fascinating to me than simply saving money via optimizing business expenses. While many of these problems are much harder to solve and we are far from solving them, the current applications of machine learning in business and marketing areas often provides proof for the rhetoric that machine learning is exploitive and a toxic solution. Along with this, while the IBM Machine Learning and Data Science products may seem to be aimed at someone in a data scientist role, I can never help but wonder to what extent data scientists are actually using these tools outside of pure novelty or an extremely simple prototyping step in a more in-depth analytical pipeline. I personally think the ability of these tools to create usefulness for someone who may otherwise not be interested in many aspects of traditional data science is where the tools are incredibly powerful and useful. While not all traditional developers or analysts are interested in in-depth data exploration, creating pipelines, and many other traditional data science skills, the ability to do so at ease and without having to learn skills and tooling can be seen as a huge burden to those not particularly interested. While true machine learning and data science skills are unlikely to be completely replaceable, many of the skills that a traditional data scientist has will be less important as more people are capable of doing what previously may have been a quite technical or complicated process. These sorts of products and services that are a part of the IBM Machine Learning catalog reinforce that idea and are an important step in allowing services to be used regardless of data location or analytical background, and are an important step forward for machine learning and data science in general. About the author  Graham Annett is an NLP engineer at Kip (Kipthis.com). He has been interested in deep learning and has worked with and contributed to Keras. He can be found on GitHub or on his website. 
Read more
  • 0
  • 0
  • 4186

article-image-reactive-programming-rxswift
Darren Sapalo
13 May 2016
7 min read
Save for later

Reactive Programming with RxSwift

Darren Sapalo
13 May 2016
7 min read
In a previous article, Building an iPhone app Using Swift, Ryan Loomba showed us how to build iOS apps using Swift, starting from a new project (Building an iPhone app Using Swift Part 1), and how to create lists using a table view and present a map using map view (Building an iPhone app Using Swift Part 2). In this article, we’ll discuss what RxSwift is and how it can be used to improve our Swift code. The Github repository for Rx.NET defines Reactive Extensions (Rx) as “a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators.” Initially, Netflix developed this library to improve the way their API backend handles streams of data, but there were uses for the library even on the frontend to achieve a responsive user interface. The above links provide a better explanation of what the library is and the rationale for why they developed it. This article will focus on staying as simple as possible and explain how Rx can help Swift developers with the problems that they might encounter with regards to mobile development. Mobile Development and Background Threads If you’ve worked on mobile apps requiring Internet access, you’ll realize that there are things that should be done on the main thread (UI manipulation and accessing context-related resources) and things that should be done on a background thread (network queries and code that takes some time to perform). This is because you shouldn’t block the main thread with long-running code, such as performing a network query to get some JSON data from a server, or else your user interface will appear to be hanging! The Non-Rx Approach For our example, let’s say you need to query some JSON data from a server and display it on the screen. On your AppDelegate class, you could possibly have some queue setup for network requests. AppDelegate.swift static var networkQueue = dispatch_queue_create("com.appName.networkQueue", DISPATCH_QUEUE_CONCURRENT) We normally dispatch code to be run on a different thread by writing the code below: dispatch_async(AppDelegate.networkQueue) { // Query some server to get some json data } Let’s say that the best-case scenario will always happen and your network call will be successful. You have Internet access, the server was alive and responded, you have proper authorization to access the data you are requesting, and you successfully retrieve the data. I have enumerated these because I want to emphasize that there are so many things that can go wrong and prevent you from a successful network query. With the best-case scenario, you have your parsed data and you’re ready to display it on your UILabel. However, you’re currently on a background thread, which means that you should switch back to the main thread to manipulate the UI. This means your code will look something like this: dispatch_async(AppDelegate.networkQueue) { let url = "http://myapi.myserver.com/users" let request = NSMutableURLRequest(URL: NSURL(string: url)!) let task = session.dataTaskWithRequest(request, completionHandler: { data, response, error -> Void in let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary dispatch_async(dispatch_get_main_queue()) { self.label.text = json.valueForKey("result") // Query some server to get some json data } } task.resume() } There are two things I want to point out here. Firstly, there are two calls to a global method called “dispatch_async” to run code on a specified queue, and they are nested inside each other. Another thing is that we’re expecting this code to run perfectly at all times; there are no error checking of whether the request was successful, whether data or response was nil, or whether error has some value or not. As mentioned above, there are many things that can go wrong when performing network queries, and your code needs to handle it elegantly. The RxSwift Approach With RxSwift, network queries and code that takes some time to perform are converted into Observables, which emit some data with its type specified. Views and controllers subscribe to them as Observers. Observables The network query can have three possible states: Currently emitting a new value (onNext), which can occur repeatedly or not at all An has error occurred and the stream has stopped completely (onError) The stream has ended (onComplete) For instance, the above example of a network query returning a JSON value could be defined as an observable that emits an NSDictionary, because that’s exactly the type of result we’re expecting: func rxGetUsers() -> Observable<NSDictionary> { return Observable.create { observer in let url = "http://myapi.myserver.com/users" let request = NSMutableURLRequest(URL: NSURL(string: url)!) let task = session.dataTaskWithRequest(request, completionHandler: { data, response, error -> Void in if (error != nil) { // an error occured observer.onError(NSError(domain: "Getting user data", code: 1, userInfo: nil)) } else if (data == nil){ // No data response observer.onError(NSError(domain: "Getting user data", code: 2, userInfo: nil)) } // other error checking let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary if (json == nil) { // No json data found observer.onError(NSError(domain: "Getting user data", code: 3, userInfo: nil)) return } observer.onNext(json) observer.onComplete() } task.resume() return NopDisposable.instance } } With the rxGetUsers function defined above, it is easier to see what the code does: when an error occurs, observer.onError is called and the management of the error is deferred to the observer (a ViewController, for example) instead of the observable (the network query). Only when the error checking for the network is done is the observer.onNext method called, and the stream is finished with the observer.onComplete method call. Observers With the network query encapsulated in a single function and returned as an Observable instance, we can proceed to use this query by subscribing an observer (see the subscribe method). The Rx library provides options on what threads the code will run on (observeOn and subscribeOn), a way for you to handle the result or errors with direct access to the ViewController’s properties such as UI references (onNext and onError), a way for you to be informed when the observable stream is finished (onComplete), and a way for you to disregard the results of a network query (via disposing of the subscription variable). The relationship between an Observer that observes an Observable is called a subscription. You might need to do the last one I mentioned if your user suddenly chooses to press home and leaves your app, and you lose access to your context and resources to interact with. let subscription = rxGetUsers() // When finished, return to main thread to update UI .observeOn(MainScheduler.instance) // Perform parallel work on separate thread .subscribeOn(ConcurrentDispatchQueueScheduler.init(queue: AppDelegate.networkQueue)) .subscribe { // What to do on each emission onNext: (dict: NSDictionary) in { self.label.text = dict.valueForKey(“result”) as? String }, // What to do when an error occurs onError: (error) in { print(error) // or you could display an alert! }, // What to do when the stream is finished onCompleted: { print(“Done with the network request!”) // or perform another network query here! } } Summary Once you understand the basics of Rx, I am sure that you will come to appreciate its great use of the Observer pattern and move past the annoyance and difficulty in handling network requests that respect the life cycle of a mobile app, or the confusing callback-ception/hell required when multiple queries need to be put together. In the next article, we’ll show the simple usage of operators and data binding provided by Rx. About the author Darren Sapalo is a software developer, an advocate for UX, and a student taking up his Master's degree in Computer Science. He enjoyed developing games on his free time when he was twelve. Finally finished with his undergraduate thesis on computer vision, he took up some industry work with Apollo Technologies Inc. developing for both the Android and iOS platforms.
Read more
  • 0
  • 0
  • 4084

article-image-exploring-r-packages
Peter Shultz
20 Jul 2016
5 min read
Save for later

Exploring R packages

Peter Shultz
20 Jul 2016
5 min read
In any given discipline, from business to academia, there is a need for data analysis. Among the most popular tools to analyze data sets is R, a programming language that allows you to easily perform statistical analyses and create data visualizations. In this post, I'm going to share with you the best tools to manipulate datasets in R such that they're easy to analyze. In addition, you'll be introduced to a wide variety of visualization tools that are sure to bring your data to life. If you haven't used R before, no problem! I'll get you set up with the proper software. This week, save 50% on some of our top R products or pick up any 5 for $50. It's the perfect opportunity to push your analytical skills forward, and get even more out of R...  Downloading R and RStudio If you haven't already downloaded R, you can do so at the following links for a Mac, for a PC, and for various flavors of Linux. Downloading R is all well and good, but doing significant work with the language is best done using an integrated development environment, or IDE. One of the most popular choices is RStudio, with support for Mac, Windows, Debian, Ubuntu, and RedHat. Downloading is painless: click this link and choose the proper download for your system and architecture. After installing, fire up RStudio. You're now ready to begin programming with R! Learning R Learning a new programming language is tough, especially if you haven't learned one before. Luckily, there are dozens of great resources available to teach you the ins-and-outs of R. While MOOCs are always an option, you might have better luck using sites like DataCamp or Code School. If you'd rather go old school, I’ll recommend the PDF R for Beginners. The coolest option that I've seen of late is a package called swirl. This package allows you to learn about R right within RStudio. If I were relearning the language, this would be my first stop. Packages R by itself can do quite a bit, but the real fun comes in with packages. Put simply, packages extend the functionality of R to do just about anything users can dream of. Don't believe me? Check out all 8,153 of the packages currently available (as of 26/03/2016). At their core, R packages are just libraries of specially-created R functions. Rather than making an R function and keeping it for the good of one, programmers in the R community share their R functions by packaging them up and sharing them on the Comprehensive R Archive Network (CRAN). Not all packages are going to come in handy to beginners. That's why I listed some that are integral to any work in R, whether you're a newcomer or a PhD-holding statistican. Learning swirl: You can learn R right within RStudio using this package. Lessons take 15-20 minutes, so you're guaranteed to walk away with having learned something, even if only on a coffee break. Manipulation tidyr AKA "Tidy R": Cleans up datasets. This package was actually made by the developers of RStudio. As RStudio describes, tidyr allows users to easily manipulate datasets by categorizing columns of your data. Performing statistical analysis on those columns then becomes a cinche. dplyr: Goes hand-in-hand with tidyr. Easily creates data tables (think Excel table), more frequenty referred to by the R community as data frames. Visualization ggplot2: Considered one of the most important visualization packages in all of R. Its syntax can be a little scary, but once you see a couple of examples, it can be fully utilized to make great visualizations. This really is the R community's visual gold standard. htmlwidgets: Allows users to make visualizations that can then be easily exported on the Internet. htmlwidgets is used by a bevy of other packages. You can see them all at this link. shiny: Interested in making a web app from your analysis but without skills in HTML, CSS, or JavaScript? If so, shiny is for you. Also fromthe makers of RStudio, shiny has developed quite a community. Its site is chock full of documentation to help get you started. LightningR: An up-and-coming visualization tool that I've worked with in the past. Lightning's visualizations utilize the best technology in web graphics, and their gallery of visualizations speaks for itself. The R packages listed above are just a few of my favorites, and are especially good for just starting out. Doing anything with R the first time around can be challenging, and so limiting the number of packages you utilize is important. Keep it simple! Installation Installing and utilizing packages is an easy three-step process: Install Include Use Toinstall, enter the command install.packages("<package_name>"), where <package_name> is the name of your package. Next, load the package using the command library(<package_name). At this point, any functions within the installed package areready to use. Call the function by typing <function>(), where <function> is the function name. When it comes to utilizing packages, documentation is your best friend. Luckily, any package available on CRAN will have documentation, or perhaps its own site! About the Author Peter Shultz is a student at the University of Michigan, studying computer science.
Read more
  • 0
  • 0
  • 4026

article-image-your-machine-learning-plotting-kill-you
Sam Wood
21 Jan 2016
4 min read
Save for later

Is Your Machine Learning Plotting To Kill You?

Sam Wood
21 Jan 2016
4 min read
Artificial Intelligence is just around the corner. Of course, it's been just around the corner for decades, but in part that's our own tendency to move the goalposts about what 'intelligence' is. Once, playing chess was one of the smartest things you could do. Now that a computer can easily beat a Grand Master, we've reclassified it as just standard computation, not requiring proper thinking skills. With the rise of deep learning and the proliferation of machine learning analytics, we edge ever closer to the moment where a computer system will be able to accomplish anything and everything better than a human can. So should we start worrying about SkyNet? Yes and no. Rule of the Human Overlords Early use of artificial intelligence will probably look a lot like how we used machine learning today. We'll see 'AI empowered humans' being the Human Overlords to their robot servants. These AI are smart enough to come up with the 'best options' to address human problems, but haven't been given the capability to execute them. Think about Google Maps - there, an extremely 'intelligent' artificial program comes up with the quickest route for you to take to get from point A to point B. But it doesn't force you to take it - you get to decide from the options offered which one will best suit your needs. This is likely what working alongside the first AI will look like. Rise of the Driverless Car The problem is that we are almost certainly going to see the power of AI increase exponentially - and any human greenlighting will become an increasingly inefficient part of the system. In much the same way that we'll let the Google Maps AI start to make decisions for us when we let it drive our driverless cars, we'll likely start turning more and more of our decisions over for AI to take responsibility for. Super smart AI will also likely be able to comprehend things that humans just can't understand. The mass of data that it's analysed will be beyond any one human to be able to judge effectively. Even today, financial algorithms are making instantaneous choices about the stock market - with humans just clicking 'yes' because the computer knows best. We've already seen electronic trading glitches leading to economic crises - six years ago! Just how much responsibility might we start turning over to smart machines? The Need to Solve Ethics If we've given power to an AI to make decisions for us, we'll want to ensure it has our best interests at heart, right? It's vital to program some sort of ethical system into our AI - the problem is, humans aren't very good at deciding what is and isn't ethical! Think about a simple and seemingly universal rule like 'Don't kill people'. Now think about all the ways we disagree about when it's okay to break that rule - in self-defence, in executing dangerous criminals, to end suffering, in combat. Imagine trying to code all of that into an AI, for every different moral variation. Arguably, it might be beyond human capacity. And as for right and wrong, well, we've had thousands of years of debate about that and we still can't agree exactly what is and isn't ethical. So how can we hope to program a morality system we'd be happy to give to an increasingly powerful AI? Avoiding SkyNet It may seem a little ridiculous to start worrying about the existential threat of AI when your machine learning algorithms keep bugging out on your constantly. And certainly, the possibilities offered by AI are amazing - more intelligence means faster, cheaper, and more effective solutions to humanity's problems. So despite the risk of us being outpaced by alien machine minds that have no concept of our human value system, we must always balance that risk against the amazing potential rewards. Perhaps what's most important is just not to be blase about what super-intelligent means for AI. And frankly, I can't remember how I lived before Google Maps.
Read more
  • 0
  • 0
  • 4012

article-image-relations-in-backbone
Andrew Burgess
05 Jan 2015
7 min read
Save for later

Relations In Backbone

Andrew Burgess
05 Jan 2015
7 min read
In this Backbone tutorial from Andrew Burgess, author of Backbone.js Blueprints, we’ll be looking at two key extensions that you can use when working with models and collections. As you will see, both give more rigidity to what is an incredibly flexible framework. This can be extremely valuable when you want a reliable way to perform certain front-end web development tasks. Relations In Backbone Backbone is an extremely flexible front-end framework. It is very unopinionated, as frameworks go, and can be bent to build anything you want. However, for some things you want your framework to be a little more opinionated and give you a reliable way to perfrom some operation. One of those things is relating models and collections. Sure, a collection is a group of models; but what if you want to relate them the other way: what if you want a model to have a "child" collection? You could role your own implementation of model associations, or you could use one of the Backbone extension libraries made expressly for this purpose. In this article, we'll look at two extension options: Backbone Associations and Backbone Relational. Backbone Associations We'll use the example of an employer with a collection of employees. In plain Backbone, you might have an Employer model and an Employees collection, but how can we relate an Employer instance to an Employees instance? For starters, let's create the Employee model: var Employee = Backbone.Model.extend({}); Notice that we are extending the regular Backbone.Model, not a special "class" that Backbone Associations gives us. However, we'll use a special class that next: var Employer = Backbone.AssociatedModel.extend({ relations: [{ type: Backbone.Many, key: 'employees', relatedModel: Employee }] }); The Employer will be an extention of Backbone.AssociatedModel class. We give it a special property: relations. It's an array, because a model can have multiple associations; but we'll just give it one for now. There are several properties that we could give a relation object, but only three are required. The first is type: it must be either Backbone.Many (if we are creating a 1:N relation) or Backbone.One (if we are creating a 1:1 relation). The second required parameter is key, which is the name of the property that will appear as the collection on the model instance. Finally, we have the relatedModel, which is a reference to the model class. Now, we can create Employee instances. var john = new Employee({ name: "John" }), jane = new Employee({ name: "Jane" }), paul = new Employee({ name: "Paul" }), kate = new Employee({ name: "Kate" }); Then, we can create an Employer instance and relate the Employee instances to it: var boss = new Employer({ name: 'Winston', employees: [john, jane, paul, kate] }); Notice that we've used the special relation key name employees. Even though we've assigned a regular array, it will be converted to a full Backbone.Collection object behind the scenes. This is great, because now you can use collection-specific functions, like this: boss.get('employees').pluck('name'); // ['John', 'Jane', 'Paul', 'Kate'] We can even use some special syntax with the get method to get properties on our nested models: boss.get('employees[0].name'); // 'John' boss.get('employees[3].name'); // 'Kate' Unfortunately, relations with Backbone Associations are a one-way thing: there's no relation going from Employee back to Employer. We can, of course, set an attribute on our model instances: john.set({ employer: boss }); But there's nothing special about this. We can make it an association, however, if we change our Employee from a regular Backbone.Model class to a Backbone.AssociatedModel class. var Employee = Backbone.AssociatedModel.extend({ relations: [{ type: Backbone.One, key: 'employer', relatedModel: 'Employer' }] }); Two things are different about this relation. First, it's a one-to-one (1:1) relation, so we use the type Backbone.One. Second, we make the relatedModel a string, instead of a reference to the Employer object; this is necessary because Employer comes after Employee in our code, and hence can't be references directly as this point; model class will look it up later, when it's needed. Now, we'll still have to set our employer attribute on Employee models, like so: john.set({ employer: boss }); The difference now is that we can use the features Backbone Associations provides us with, like nested getting: john.get('employer.name'); // Winston One more thing: I mentioned that the array attribute with the special key name becomes a collection object. By default, it's a generic Backbone.Collection. However, if you want to make your own collection class with some special features, you can add a collectionType property to the relation: var EmployeeList = Backbone.Collection.extend({ model: Employee }); var Employer = Backbone.AssociatedModel.extend({ relations: [{ type: Backbone.Many, collectionType: EmployeeList key: 'employees', relatedModel: Employee }] }); Backbone Relational Backbone Relational has very similar syntax to Backbone Associations; however, I prefer this library because it makes our relations two-way affairs from the beginning. First, both models are required to extend Backbone.RelationalModel. var Employee = Backbone.RelationalModel.extend({}); var EmployeeList = Backbone.Collection.extend({ model: Employee }); var Employer = Backbone.RelationalModel.extend({ relations: [{ type: Backbone.HasMany, key: 'employees', relatedModel: Employee, collectionType: EmployeeList reverseRelation: { key: 'employer' } }] }); Notice that our Employer class has a relations attributes. The key, relatedModel, and type attributes are required and perform the same duties their Backbone Associated counterparts do. Optionally, the collectionType property is also avaiable. The big difference with Backbone Relational—and the reason I prefer it—is because of the reverseRelation property: we can use this to make the relationship act two ways. We're giving it a single property here, a key: this value will be the attribute given to model instances on the other side of the relationship. In this case, it means that Employee model instances will have an employer attribute. We can see this in action if we create our employer and employees, just as we did before: var john = new Employee({ name: "John" }), jane = new Employee({ name: "Jane" }), paul = new Employee({ name: "Paul" }), kate = new Employee({ name: "Kate" }); var boss = new Employer({ name: 'Winston', employees: [john, jane, paul, kate] }); And now, we have a two-way relationship. Based on the above code, this part should be obvious: boss.get('employees').pluck('name'); // ['John', 'Jane', 'Paul', 'Kate'] But we can also do this: john.get('employer').get('name'); // Winston Even though we never gave the john instance an employer attribute, Backbone Relational did it for us, because we gave an object on one side of the relationship knowledge of the connection. We could have done it the other way, as well, by giving the employees an employer: var boss = new Employer({ name: 'Winston' }); var john = new Employee({ name: "John", employer: boss }), jane = new Employee({ name: "Jane", employer: boss }), paul = new Employee({ name: "Paul", employer: boss }), kate = new Employee({ name: "Kate", employer: boss }); boss.get('employees').pluck('name'); // ['John', 'Jane', 'Paul', 'Kate'] It's this immediate two-way connection that makes me prefer Backbone Relational. But both libraries have other great features, so check them out in full before making a decision. Backbone Associations Backbone Relational I've found that the best way to get better at using Backbone is to really understand what's going on behind the curtain, to get a feel for how Backbone "thinks." With this in mind, I wrote Backbone.js Blueprints. As you build seven different web applications, you'll learn how to use and abuse Backbone to the max. About the Author Andrew Burgess is a primarily JavaScript developer, but he dabbles in as many languages as he can find. He's written several eBooks on Git, JavaScript, and PHP, as well as Backbone.js Blueprints, published by Packt Publishing. He's also an web development instructor as Tuts+, where he produces videos on everything from JavaScript to the command line.
Read more
  • 0
  • 0
  • 3999
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime
article-image-speed-speed-speed
Owen Roberts
26 Jul 2016
3 min read
Save for later

Speed, Speed, Speed

Owen Roberts
26 Jul 2016
3 min read
We’re currently in the middle of our Skill Up campaign, with all our titles at just $10 each!If you’re late to the party, Skill Up is your opportunity to get the knowledge and skills you need to become the developer you want to be in 2016 and beyond. Along with the launch of our campaign we’ve also released our 2016 Skill Up report; you can download it here if you haven’t done so already, and in the report one particular piece of information really stood out to me - the link between salary and programming language used. Take a look at the graph below: The one thing linking the top earning languages is the speed that each language is able to offer. Whether it’s SQL’s ease of fine tuning or C’s structured design created for simplicity, each is renowned for being faster than their peers and the alternatives. It should be no surprise that faster languages end up offering more pay. Scala’s ability to handle the stress of big data applications and still crunch data fast has made it one of, if not THE biggest programming languages for any big data related issue. Even Perl, a language that has fallen by the wayside in the eyes of many since 2005 is a speed machine, often beating Python 3 when it comes to everyday tasks leading it to carving out its own niche in finance, bioinformatics, and other specialized sectors. The benefits for a company to hire those who can create are obvious – we all know how important it is to customers to have a fast product; those few seconds it takes to load can be the deciding factor as to whether your product is picked up or left in the dust. This is especially true for enterprise level applications or big data crunchers. If the solution you're selling is too slow for the cost you're offering then why should these customers stay with your product when there are so many potentially faster options on the market at a comparable price? So this Skill Up, why not take the opportunity to speed up your applications? Whether it’s by laying the foundations for a completely new language (it’s never too late to change after all) or checking out how to streamline your current apps for better performance and speed, there’s no better time to ensure your programming skills are better than ever.
Read more
  • 0
  • 0
  • 3987

article-image-year-machine-learning
Owen Roberts
22 Jan 2016
5 min read
Save for later

This Year in Machine Learning

Owen Roberts
22 Jan 2016
5 min read
The world of data has really boomed in the last few years. When I first joined Packt Hadoop was The Next Big Thing on the horizon and what people are now doing with all the data we have available to us was unthinkable. Even in the first few weeks of 2016 we’re already seeing machine learning being used in ways we probably wouldn’t have thought about even a few years ago – we’re using machine learning for everything from discovering a supernova that was 570 billion times brighter than the sun to attempting to predict this year’s Super Bowl winners based on past results, but So what else can we expect in the next year for machine learning and how will it affect us? Based on what we’ve seen over the last three years here are a few predictions about what we can expect to happen in 2016 (With maybe a little wishful thinking mixed in too!) Machine Learning becomes the new Cloud Not too long ago every business started noticing the cloud, and with it came a shift in how companies were structured. Infrastructure was radically adapted to take full advantage that the benefits that the cloud offers and it doesn’t look to be slowing down with Microsoft recently promising to spend over $1 billion in providing free cloud resources for non-profits. Starting this year it’s plausible that we’ll see a new drive to also bake machine learning into the infrastructure. Why? Because every company will want to jump on that machine learning bandwagon! The benefits and boons to every company are pretty enticing – ML offers everything from grandiose artificial intelligence to much more mundane such as improvements to recommendation engines and targeted ads; so don’t be surprised if this year everyone attempts to work out what ML can do for them and starts investing in it. The growth of MLaaS Last year we saw Machine Learning as a Service appear on the market in bigger numbers. Amazon, Google, IBM, and Microsoft all have their own algorithms available to customers. It’s a pretty logical move and why that’s not all surprising. Why? Well, for one thing, data scientists are still as rare as unicorns. Sure, universities are creating new course and training has become more common, but the fact remains we won’t be seeing the benefits of these initiatives for a few years. Second, setting up everything for your own business is going to be expensive. Lots of smaller companies simply don’t have the money to invest in their own personal machine learning systems right now, or have the time needed to fine tune it. This is where sellers are going to be putting their investments this year – the smaller companies who can’t afford a full ML experience without outside help. Smarter Security with better protection The next logical step in security is tech that can sense when there are holes in its own defenses and adapt to them before trouble strikes. ML has been used in one form or another for several years in fraud prevention, but in the IT sector we’ve been relying on static rules to detect attack patterns. Imagine if systems could detect irregular behavior accurately or set up risk scores dynamically in order to ensure users had the best protection they could at any time? We’re a long way from this being fool-proof unfortunately, but as the year progresses we can expect to see the foundations of this start being seen. After all, we’re already starting to talk about it. Machine Learning and Internet of Things combine We’re already nearly there, but with the rise in interest in the IoT we can expect that these two powerhouses to finally combine. The perfect dream for IoT hobbyists has always been like something out of the Jetsons or Wallace and Gromit –when you pass that sensor by the frame of your door in the morning your kettle suddenly springs to life so you’re able to have that morning coffee without waiting like the rest of us primals; but in truth the Internet of Things has the potential to be so much more than just making the lives of hobbyists much easier. By 2020 it is expected that over 25 billion ‘Things’ will be connected to the internet, and each one will be collating reams and reams of data. For a business with the capacity to process this data they can collect the insight they could collect is a huge boon for everything from new products to marketing strategy. For IoT to really live up to the dreams we have for it we need a system that can recognize and collate relevant data, which is where a ML system is sure to take center stage. Big things are happening in the world of machine learning, and I wouldn’t be surprised if something incredibly left field happens in the data world that takes us all by surprise, but what do you think is next for ML? If you’re looking to either start getting into the art of machine learning or boosting your skills to the next level then be sure to give our Machine Learning tech page a look; it’s filled our latest and greatest ML books and videos out right now along with the titles we’re realizing soon, available to preorder in your format of choice.
Read more
  • 0
  • 0
  • 3986

article-image-beginner-bitcoin-project-network-visualizer
Alex Leishman
12 Dec 2014
9 min read
Save for later

Beginner Bitcoin Project - Network Visualizer

Alex Leishman
12 Dec 2014
9 min read
This post will give you a basic introduction to the bitcoin protocol by guiding you through how to create a simple, real-time visualization of transactions in the bitcoin network. Bitcoin is easy to understand on the surface, but very complex when you get into the details. The explanations in this guide are simplified to make the content accessible to people unfamiliar with bitcoins. In-depth documentation can be found at https://bitcoin.org. Overview of the bitcoin network Bitcoin is a public P2P payment network and ledger enabling people or machines to securely transfer value over the Internet without trusting a third party. The tokens used for this value exchange are called bitcoins (lowercase “b”). A bitcoin is divisible to eight decimal places. Currently one bitcoin has a market value of about $350. Bitcoins “sit” at a bitcoin address, just like money “sits” in a bank account. A bitcoin address is a public identifier like a bank account number. In order to send bitcoins from one address to another you must prove ownership of the sending address by signing a transaction with the private key of the sending address. This private key is like the PIN or password to your bank account. Every bitcoin address has a unique corresponding private key. The amount of bitcoins in each of the existing addresses is stored in a public ledger called the blockchain. The blockchain holds the history of all valid/accepted transactions sent through the bitcoin network. These transactions are what we will be visualizing. To create a live visualization of the network we must connect to a bitcoin node or set of nodes. Nodes are servers in the bitcoin network responsible for propagating and relaying transactions. It's important to note that not all transactions sent into the bitcoin network are valid, and most nodes will not relay an invalid transaction. Therefore, although a single node will eventually see any transaction accepted by the network, it will not see many of the spam or malicious transactions because most other nodes will not propagate them. There are some very well connected nodes (super nodes) or clusters of nodes that can provide a more comprehensive view of the live state of the network. [Blockchain.info](https://blockchain.info) operates a super node and allows developers free access to its data through both REST and WebSockets APIs. We will be using their WebSockets API for this project. Let's get coding. You can see the finished project code here: https://github.com/leishman/btc_network_visualizer. First we will create a basic index.html file with jQuery and our main.js file required. The only HTML element we need is a <div> container: <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="css/main.css"> </head> <body> <h1>Bitcoin Network Visualizer</h1> <!-- Visualization Container --> <div class="vizContainer js-visualize"></div> <!-- Load jQuery --> <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.1.min.js"></script>')</script> <!-- Link main.js, where our visualization code is located --> <script src="js/main.js"></script> </body> </html> Let's throw some styling in there: /* gray container for visualization */ .vizContainer { width: 100%; height: 500px; background: #EEE; } /* outer element for bubble */ .txBubble { position: absolute; } /* inner element for bubble */ .txBubbleInner { width: 100%; height: 100%; position: relative; background: green; border-radius: 50%; -webkit-animation: expand 1s ease-in-out; } /* box displaying information about a transaction on click */ .toolTip { width: auto; height: 40px; padding: 0 5px; background: #AAA; border-radius: 4px; color: white; position: absolute; } /* words in tooltip */ .toolTip p { margin: 0; line-height: 40px; } /* Animations */ @-webkit-keyframes expand { 0% { width: 0; height: 0; left: 50%; top: 50%; } 100% { width: 100%; height: 100%; left: 0; top: 0; } } To get started, we must establish a WebSocket connection with blockchain.info. You can view their documentation [here](https://blockchain.info/api/api_websocket). The following code illustrates how to set up the WebSockets connection, subscribe to all unconfirmed (new) transactions in the network, and log the data to the console: /////////////////////////////////////////// /// Connect with Blockchain Websockets API //////////////////////////////////////////// // create new websocket object using a secure connection (wss) var blkchainSocket = new WebSocket('wss://ws.blockchain.info/inv'); // once the socket connection is established blkchainSocket.onopen = function(event) { var subMessage; // message to subscribe to all unconfirmed transactions subMessage = '{"op":"unconfirmed_sub"}'; // send message to subscribe blkchainSocket.send(subMessage); } // callback to execute when a message is displayed blkchainSocket.onmessage = function(event) { // Parse the data returned to convert to JSON var txData = JSON.parse(event.data); // log data to console console.log(txData); } If you run this code, you should see a live stream of transactions in your console. Let's take a look at the data we receive: { "op":"utx", "x":{ "hash":"427937e561d2ab6236014d92509a1a872eec327de2b9f6d84cfcbce8af2db935", "vin_sz":1, "vout_sz":2, "lock_time":"Unavailable", "size":259, "relayed_by":"127.0.0.1", "tx_index":68494254, "time":1415169009, "inputs":[ { "prev_out":{ "value":29970360, "addr":"17L4qAEjVbKZ99iXJFhWMzRWv3c8LHrUeq", "type":0 } } ], "out":[ { "value":3020235, // output 1, 3020235 Satoshis going to address 1DTnd.... "addr":"1DTnd8vwpJT3yo64xZST5Srm9Z5JEWQ6nA", "type":0 }, { "value":26940125, // output 2, 26940125 Satoshis going to address 17L4.... "addr":"17L4qAEjVbKZ99iXJFhWMzRWv3c8LHrUeq", "type":0 } ] } } There is a lot of information here. The only thing we will concern ourselves with is the output, which specifies the amount and destination of the coins being sent in the transaction. It is important to note that a single transaction can send various bitcoin amounts to multiple addresses. The output specifies the destinations and amounts of bitcoin sent by a transaction. Often a transaction has at least two outputs, where one of them sends bitcoins to a receiver and the other sends bitcoins as change back to the sender. The details of this are beyond the scope of this guide, but you can read more about it here. Now that we have the data, we can visualize it. Let's make a function called visualize that can be passed data sent from the API and display it on the screen: ///////////////////////// /// Visualize the data ///////////////////////// // Input: JSON data object representing a new Bitcoin transaction from Blockchain.info API // Result: Append a circle (bubble) to the DOM with a size proportional to the transaction value function visualize(data) { // declare variables var r, outputs, txDot, vizHeight, vizWidth, vizContainter, dot, txVal = 0, valNorm = 10000000; // query DOM for viz Container vizContainter = $('.js-visualize'); // get height and width of viz container vizHeight = vizContainter.height(); vizWidth = vizContainter.width(); // get value of first tx ouput (for test only) outputs = data.x.out; // sum all unspent outputs to calculate total value of Tx for(var i = 0; i < outputs.length; i++){ txVal += outputs[i].value; } // calculate radius of circle to display based on Tx value r = (txVal / valNorm) / 2; // generate random position randTop = randomInt(vizHeight) + 88; randLeft = randomInt(vizWidth) - r; // set min and max sizes for radius (r) if(r < 5) { r = 5; } else if(r > 100) { r = 100; } // create HTML elements to use as bubble txBubble = $('<div class="txBubble"><div class="txBubbleInner"></div></div>') .css({'top': randTop, 'left': randLeft, 'width': r, 'height': r}) .attr('data-txvalue', txVal); // add bubble element to DOM dot = vizContainter.append(txBubble); } This code creates a new element styled as a circle for each new transaction received. The size of the circle is represented by the total amount of bitcoins moved in a transaction. We set a max and min bound size of the circle to prevent invisible or over-sized circles from appearing. We can then add a tooltip to each bubble to display the total value of the transaction. The values returned by blockchain.info are expressed in Satoshis (named after the mysterious creator of bitcoin - Satoshi Nakamoto). One bitcoin is equal to 100 million Satoshis, so we have a function called satoshi2btc  do the conversion for us: ///////////////////////////////////////// /// Create a tooltip to display Tx data ////////////////////////////////////////// // Input: event, passed by calling function. showTooltip() acts as a callback function when a bubble is clicked // see $(document).on('click', '.txBubble', showTooltip); // Result: Display tooltip with the transaction value (in BTC) represented by the bubble function showTooltip(event) { // declare variables var addrs, value, tooltip; // get value of tx stored as data attribute value = $(this).data('txvalue'); // get coordinates of user's click xCoord = event.clientX; yCoord = event.clientY; // remove other tooltips to ensure only 1 is displayed at a time $('.toolTip').remove(); // create a tooltip and position it at user's click tooltip = $('<div class="toolTip"></div>') .css({'top': yCoord, 'left': xCoord}) .html('<p>' + satoshi2btc(value) + ' BTC</p>'); // add tooltip to DOM $('.js-visualize').append(tooltip); } // define random integer function // radomInt(5) will return a number from 0 to 4 function randomInt(range) { return Math.floor(Math.random() * range); } // convert Satoshis to BTC // There are 100,000,000 Satoshis in 1 Bitcoin function satoshi2btc(val) { return val / 100000000; } ///////////////////////////////////////// /// Bind Tooltip event on document load ////////////////////////////////////////// // bind showTooltip function on DOM load $(function() { $(document).on('click', '.txBubble', showTooltip); }); In summary, we used the blockchain.info WebSockets API to create a live JavaScript visualization of the transactions in the bitcoin network. We used our new understanding of the bitcoin protocol to visually represent the value of each transaction. This is just the tip of the iceberg and a great way to get your feet wet with bitcoin development. About the Author Alex Leishman is a software engineer who is passionate about bitcoin and other digital currencies. He works at MaiCoin.com where he is helping to build the future of money.
Read more
  • 0
  • 0
  • 3983

article-image-picking-tensorflow-can-now-pay-dividends-sooner
Sam Abrahams
23 May 2016
9 min read
Save for later

Picking up TensorFlow can now pay dividends sooner

Sam Abrahams
23 May 2016
9 min read
It's been nearly four months since TensorFlow, Google's computation graph machine learning library, was open sourced, and the momentum from its launch is still going strong. Over the time, both Microsoft and Baidu have released their own deep-learning libraries (CNTK and warp-ctc, respectively), and the machine learning arms race has escalated even further with Yahoo open sourcing CaffeOnSpark. Google hasn't been idle, however, and with the recent releases of TensorFlow Serving and the long awaited distributed runtime, now is the time for businesses and individual data scientists to ask: is it time to commit to TensorFlow? TensorFlow's most appealing features There are a lot of machine learning libraries available today—what makes TensorFlow stand out in this crowded space? 1. Flexibility without headaches TensorFlow heavily borrows concepts from the more tenured machine learning library Theano. Many models written for research papers were built in Theano, and its composable, node-by-node writing style translates well when implementing a model whose graph was drawn by hand first. TensorFlow's API is extremely similar. Both Theano and TensorFlow feature a Python API for defining the computation graph, which then hooks into high performance C/C++ implementations of mathematical operations. Both are able to automatically differentiate their graphs with respect to their inputs, which facilitates learning on complicated neural network structures and both integrate tightly with Numpy for defining tensors (n-dimensional arrays). However, one of the biggest advantages TensorFlow currently has over Theano (at least when comparing features both Theano and TensorFlow have) is its compile time. As of the time of writing this, Theano's compile times can be quite lengthy and although there are options to speed up compilation for experimentation, they come at the cost of a slower output model. TensorFlow's compilation is much faster, which leads to less headaches when trying out slightly different versions of models. 2. It's backed by Google (and the OSS community) At first, it may sound more like brand recognition than a tangible advantage, but when I say it's 'backed' by Google, what I mean is that Google is seriously pouring tons of resources into making TensorFlow an awesome tool. There is an entire team at Google dedicated on maintaining and improving the software steadily and visibly, while simultaneously running a clinic on how to properly interact with and engage the open source community. Google proved itself willing to adopt quality submissions from the community as well as flexible enough to adapt to public demands (such as moving the master contribution repository from Google's self-hosted Gerrit server to GitHub). These actions combined with genuinely constructive feedback from Google's team on pull-requests and issues helped make the community feel like this was a project worth supporting. The result? A continuous stream of little improvements and ideas from the community while the core Google team works on releasing larger features. Not only does TensorFlow recieve the benefits of a larger contributor base because of this, it also is more likely to withstand user decay as more people have invested time in making TensorFlow their own. 3. Easy visualizations and debugging with TensorBoard TensorBoard was the shiny toy that shipped on release with the first open source version of TensorFlow, but it's much more than eye candy. Not only can you use it as a guide to ensure what you've coded matches your reference model, but you can also keep track of data flowing through your model. This is especially useful when debugging subsections of your graph, as you can go in and see where any hiccups may have occurred. 4. TensorFlow Serving cuts the development-deployment cycle by nearly half The typical life cycle of machine learning models in the business world is generally as follows: Research and develop a model that is more accurate/faster/more descriptive than the previous model Write down the exact specifications of the finalized model Recreate the model in C++/C/Java/some other fast, compiled language Push the new model into deployment, replacing the old model Repeat On release, TensorFlow promised to "connect research and production." However, the community had to wait until just recently for that promise to come to fruition with TensorFlow Serving. This software allows you to run it as a server that can natively run models built in TensorFlow, which makes the new life cycle look like this: Research and develop a new model Hook the new model into TensorFlow Serving Repeat While there is overhead in learning how to use TensorFlow Serving, the process of hooking up new models stays the same, whereas rewriting new models in a different language is time consuming and difficult. 5. Distributed learning out of the box The distributed runtime is one of the newest features to be pushed to the TensorFlow repository, but it has been, by far, the most eagerly anticipated aspect of TensorFlow. Without having to incorporate any other libraries or software packages, TensorFlow is able to run distributed learning tasks on heterogenous hardware with various CPUs and GPUs. This feature is absolutely brand new (it came out in the middle of writing this post!), so do your research on how to use it and how well it runs. Areas to look for improvement TensorFlow can't claim to be the best at everything, and there are several sticking points that should be addressed sooner rather than later. Luckily, Google has been making steady improvements to TensorFlow since it was released, and I would be surprised if most of these were not remedied within the next few months. Runtime speed Although the TensorFlow team promises deployment worthy models from compiled TensorFlow code, at this time, its single machine training speed lags behind most other options. The team has made improvements in speed since its release, but there is still more work to be done. In-place operations, a more efficient node placement algorithm, and better compression techniques could help here. Distributed benchmarks are not available at this time—expect to see them after the next official TensorFlow release. Pre-trained models Libraries such as Caffe, Torch, and Theano have a good selection of pre-trained, state-of-the-art models that are implemented in their library. While Google did release a version of its Inception-v3 model in TensorFlow, it needs more options to provide a starting place for more types of problems. Expanded distributed support Yes, TensorFlow did push code for it's distributed runtime, but it still needs better documentation as well as more examples. I'm incredibly excited that it's available to try out right now, but it's going to take some time for most people to put it into production. Interested in getting up and running with TensorFlow? You'll need a primer on Python. Luckily, our Python Fundamentals course in Mapt gives you an accessible yet comprehensive journey through Python - and this week it's completely free. Click here, login, then get stuck in... The future Most people want to use software that is going to last for more than a few months—what does the future look like for TensorFlow? Here are my predictions about the medium-term future of the library. Enterprise-level distributions Just as Hadoop has commercial distributions of its software, I expect to see more and more companies offering supported suites that tie into TensorFlow. Whether they have more pre-trained models built on top of Keras (which already supports a TensorFlow backend), or make TensorFlow work seamlessly with a distributed file system like Hadoop, I forsee a lot of demand for enterprise features and support with TensorFlow. TensorFlow's speed will catch up (and most users won't need it) As mentioned earlier, TensorFlow still lags behind many other libraries out there. However, with the improvements already made; it's clear that Google is determined to make TensorFlow as efficient as possible. That said, I believe most applications of TensorFlow won't desperately need the speed increase. Of course, it's nice to have your models run faster, but most businesses out there don't have petabytes of useful data to work with, which means that model training usually doesn't take the "weeks" that we often see claimed as training time. TensorFlow is going to get easier, not more difficult, over time While there are definitely going to be many new features in upcoming releases of TensorFlow, I expect to see the learning curve of the software go down as more resources, such as tutorials, examples, and books are made available. The documentation's terminology has already changed in places to be more understandable; navigation within the documentation should improve over time. Finally, while most of the latest features in TensorFlow don't have the friendliest APIs right now, I'd be shocked if more user-friendly versions of TensorFlow Serving and the distributed runtime weren't in the works right now. Should I use TensorFlow? TensorFlow appears primed to fulfil the promise that was made back in November: a distributed, flexible data flow graph library that excels at neural network composition. I leave it to you decision makers to figure out whether TensorFlow is the right move for your own machine learning tasks, but here is my overall impression of TensorFlow: no other machine learning framework targeted at production-level tasks is as flexible, powerful, or improving as rapidly as TensorFlow. While other frameworks may carry advantages over TensorFlow now, Google is putting the effort into making consistent improvements, which bodes well for a community that is still in its infancy. About the author Sam Abrahams is a freelance data engineer and animator in Los Angeles, CA. He specializes in real-world applications of machine learning and is a contributor to TensorFlow. Sam runs a small tech blog, Memdump, and is an active member of the local hacker scene in West LA.
Read more
  • 0
  • 0
  • 3969
article-image-5-habits-successful-developers
Hari Vignesh
10 May 2017
5 min read
Save for later

5 habits of successful developers

Hari Vignesh
10 May 2017
5 min read
There are about 18.2 million software developers worldwide, and that number is expected to rise to 26.4 million by 2019 – a 45% increase, says Evans Data Corp, in its latest Global Developer Population and Demographic Study.That means that the number ofpeople with your skill setis going to increase, by a lot. Start developing successful habits now so you’ll stand out from the crowd later. 1. Staying up to date Successful developers know that this is mandatory. Keep learning new things or work on something – even if it’s just a small side project – every day. Staying updated in the IT industry is critical.Let me give you a simple example: the evolution of mobile phones.Initially,mobile phones took five years to reach the masses but when smartphones were introduced they reached billions in two years. So, with respect to the technology from the year 2000 - 2010, every technology or framework took five years to mature. But now, technology is becoming outdated in less than two years. To withstand this heat, you need to be awake and keep your eyes and ears open for new emerging technology and frameworks.So, how do you stay up to date? Medium is a great platform to start. Also, engage in other developer communities and tech forums. 2. Lucid understanding and ownership Understanding is the foundation for every developer, and this should be your strength. Your peers or leaders will prefer to explain things to you only once, and you should be in a position to grasp and perform tasks without raising any flags. Time is a crucial factor for everyone in the organization so the organization will expect you to understand processes quickly. How do you understand quickly? You need to upgrade your domain knowledge constantly. For example, if you’re working in a health care sector, you need to be half doctor — you need to understand things better so you can deliver a high quality product. Successful developers produce quality products and, in order to think about quality, you need to be an expert in your domain. 3. Crafting with best practices If we have two developers with the same experiences, what metrics can you use todetermine which one isbetter? Well, it’s based on best practices. A task can be achieved in multiple ways but whoever provides the best, easiest, and most scalable solution is obviously more sellable than the other person.Qualities like providing multiple solutions, scalable solutions, optimal solutions — all these will manifest when you gain more experience. You can gain this experience quickly if you spend more time with developer documentation and community, as well as by asking the right questions.  Writing clean, scalable, and optimal code is one of the most valued skills of all time in the software industry. Nobody can reach the ultimate level, but you should keep a check on this all the time.There are multiple paths you can take to learn best practices. The best path is to grab a mentor. Find experts in your field, discuss the problems with them, and their experience will definitely show you the best practices. 4. Avoiding your comfort zone Almost 90% of developers just want to hang out in their comfort zones. They opt for big corporate jobs where they’ll have scheduled work every day, party on the weekends, etc. Well, even if you’re employed with a big tech giant organization, you should be doing something outside of that work like   freelancing, open source contributions, core and library creation, and much more. This will obviously take up some of your free time and be less comfortable but the end result will be beautiful.You need to do something different to get something different.  If you prefer being in your comfort zone then trust me, you will lose your job after five years. You need to constantly prove your presence in this industry to keep being successful. Engage yourself in conferences, tech meet-ups, make hobby projects, present white papers, improve your skillsets, and much more. 5. Community interaction Most developers in the world are self-taught (including myself). We learn everything from the developer community for free. What have we done in return for the community? Well, successful developers play a vital role in contributing to open source or to the developer community. They write articles, present things in meet-ups, organize meet-ups, and share knowledge. This will in-turn help the others who are dependent on free education and that’s how the fraternity will grow. So, how can you help the community? Here are some suggestions to get your started: You can start writing your tips, tricks, and experiences in a blog. You can start making “how to” videos. You can contribute to open source projects. You can start writing free libraries. You can start presenting in meet-ups. You can participate in developer community programs. Mentor a few projects. If you couldn’t manage time for making new habits, here’s a simple trick: If you need to inject any habit in your life, all you need to do is to practice it strictly for 21 days. If you you're able to do that, your brain and body will pick it up automatically for the rest of your life. About the Author  Hari VigneshJayapalan is a Google Certified Android app developer, IDF Certified UI & UX Professional, street magician, fitness freak, technology enthusiast, and wannabe entrepreneur. He can be found on Twitter @HariofSpades.
Read more
  • 0
  • 0
  • 3958

article-image-fizzbuzz-only-pandas-how-not-pass-job-interview
Greg Roberts
09 Jun 2016
10 min read
Save for later

FizzBuzz with only pandas: How to not pass a job interview

Greg Roberts
09 Jun 2016
10 min read
I love Python, and the Data Analysis library pandas in particular. It's a fantastic library for working with tabular data, contains connectors for many common data formats out of the box, has an excellent performance profile (as it sits on top of NumPy), and has many many common data operations built in. As a data analyst, I use pandas every day, for most of the day. Sometimes I think of ways I could make more use out of it, for example this gist for getting results from Neo4J into a DataFrame. Sometimes, like today, I think about how I could misuse it for a purely procrastinatory purpose. Want to learn Python? This week our Python Fundamentals course is free inside Mapt. It's an accessible introduction that's comprehensive enough to give you the confidence you need to explore Python further. Click here, log in, and go straight to it!  FizzBuzz FizzBuzz is a popular, simple test of a programmer's knowledge and ability, often employed in job interviews to test a candidate's programming/problem solving ability. The basic problem is usually stated as follows: "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'." There is so much discussion of FizzBuzz on the internet, that it almost seems pointless to discuss it anymore. Regardless of your opinion of it as a tool to test programmer knowledge/ability, it is certainly an interesting, if somewhat basic challenge, and is still approached (with varying degrees of irreverence) to this day. The Challenge Partially inspired by Joel Grus' tongue in cheek article above, and partially out of pure interest, I recently started to ponder how one might go about implementing a FizzBuzz solution using pandas. The intention would be to rely on as few non-pandas operations as possible, whilst still producing legible code. First of all, here's a fairly standard vanilla Python solution to the problem: def fizzbuzz(x): '''returns the fizzbuzz output for an integer x''' output = '' if x % 3 == 0: output += 'Fizz' if x % 5 ==0: output += 'Buzz' if (x % 3) > 0 and (x % 5) > 0: output += str(x) return output for i in range(1,101): print fizzbuzz(i) Now, the most simple way to apply this with pandas would be to just use the apply function on a series of integers: import pandas as pd pd.Series(range(1,100)).apply(fizzbuzz) which is simple, terse and readable, but the logic is still being done outside of pandas. What I'm really after is a way to express that fizzbuzz logic entirely with pandas operations. My first crack at this is displayed below. #create a DataFrame containing all the values we need, all the integers, #and a series of Fizzes and Buzzes we can hack together to make our output values = pd.DataFrame( { 'n':range(1,101), 'fizz':'Fizz', 'buzz':'Buzz' } ) #get columns for each of the seperate output types fizzes = values[values['n'] % 3 == 0]['fizz'] buzzes = values[values['n'] % 5 == 0]['buzz'] ints = values[(values['n'] % 3 > 0) & (values['n'] % 5 > 0)].n.apply(str) #put the columns together as one dataframe again outputs = pd.concat([fizzes,buzzes,ints], axis=1) #for each row, concatenate the non-null values together outputs.apply(lambda x: x[~pd.isnull(x)].str.cat(),axis=1) First, we're taking advantage of pandas' quite clever constructor functions to create a DataFrame with all the values we need. Secondly, we use pandas' expressive filtering syntax to create three separate columns containing ONLY the values we need. The third part is to concatenate these columns together to give us one more dataframe containing only the values we need. This takes advantage of pandas' powerful and extensive indexing capabilities, which returns us a dataframe with a nice contiguous index, and all our values in order. Finally, we use apply again to turn each row into a single string. When you supply axis = 1 to the apply method, it feeds each row to your operation in the form of a Series, which makes it easier to work with the row in question. I was fairly happy with this as a first pass. All the logic for deciding what to print is done with pandas operations, and the flow is fairly clear. It's still pretty long though. Yes it could be condensed down to two (very long and ugly) lines of code, but it still doesn't feel like an 'optimal' solution to this (very silly) problem. We can condense this logic down further, and reach one-liner-nirvana by making even more impractical use of pandas' DataFrame constructor: pd.DataFrame( { 'f':pd.Series('Fizz',index=filter(lambda x: x% 3 == 0, range(1,100))), 'b':pd.Series('Buzz',index=filter(lambda x: x% 5 == 0, range(1,100))), 'n':pd.Series( filter(lambda x: x%3>0 and x%5>0,range(1,100)), index=filter(lambda x: x%3>0 and x%5>0,range(1,100)) ).apply(str) } ).apply( lambda x: x[~pd.isnull(x)].str.cat(), axis=1 ) This really feels like progress. The flow is essentially the same as before, but now we construct the Fizz, Buzz and output Series within the DataFrame constructor. This makes the code more succinct, and also serves the crucial purpose of saving some precious bytes of memory, reducing the evident strain on my 16GB, i5 work PC. We can still do better however. You'll note that the above solution contains a filter() function for the fizzbuzz logic, which is a step backwards (within this already backwards problem), Also, the FizzBuzz lines actually say BuzzFizz, which is very annoying and not actually fixable. Version 0.18 of pandas introduced some really neat changes including an expanded set of arithmetic operations for Timedeltas. Another neat little addition was the addition of the ability to filter Series objects using a callable condition. You can also supply an 'other' argument to return when the conditions aren't met. This allows us to bring that filter logic back into pandas, and create our best/worst solution yet: pd.concat( [ pd.Series('Fizz', index=range(1, 100)).where(lambda x: x.index % 3 ==0, other=''), pd.Series('Buzz', index=range(1, 100)).where(lambda x: x.index % 5 ==0, other=''), pd.Series(range(1,100),index=range(1,100)).where(lambda x:(x % 5 > 0) & (x % 3 > 0), '').apply(str), ], axis=1 ).apply(lambda x: x.str.cat(), axis=1) Ok, Ok, I admit it, I've gone too far. I looked at this daft code, and realised my quest for pandas based purity had driven me to creating this abomination. But hey, I learnt a bit about my favorite library in the process, so what's the harm? If I relax my condition for pandas purity slightly, this can be coaxed into something almost readable: pd.concat( [ pd.Series('Fizz', range(0, 100, 3)), pd.Series('Buzz', range(0, 100, 5)), pd.Series(range(100)).where(lambda x:(x % 5 > 0) & (x % 3 > 0), '').apply(str), ], axis=1 ).apply(lambda x: x.str.cat(), axis=1)[1:] And that's where I decided to stop. It's not going to get any better/worse than that, and besides, I have actual work to do. It's been an entertaining bit of work, and I'm satisfied this solution is about as small as I'm going to get within the ridiculous constraints I set myself. Also, I can hear the ardent code golfers screaming at me that NONE of my pandas solutions are valid, because they all print an index column as well as the fizzbuzz values. I don't think there is a way of overcoming this purely in pandas, so you'll have to settle for just wrapping any of them with 'n'.join(...) Performance So, we have several methods for fizzbuzzing with pandas. The next obvious question is about scalability and performance. What if you're interviewing to be a Big Data Engineer at Google, and they ask you for the fastest, most scalable, general fizzbuzz solution in your arsenal? Let's take our functions above for a test drive. We'll encapsulate each as a function, then see how they scale as we ask for ever larger ranges. I'm using a quick and dirty method of timing this as I don't actually care that much. def fizzbuzz(x): '''returns the fizzbuzz output for an integer x''' output = '' if x % 3 == 0: output += 'Fizz' if x % 5 ==0: output += 'Buzz' if (x % 3) > 0 and (x % 5) > 0: output += str(x) return output #our vanilla solution def fb_vanilla(rng): return map(fizzbuzz, range(1, rng+1)) #our trivial pandas solution def fb_pandas_vanilla(rng): return pd.Series(range(1, rng+1)).apply(fizzbuzz) #I'm going to skip the first big pandas solution, this is pretty much identical #our second pandas solution, down to one line def fb_pandas_long(rng): return pd.DataFrame( { 'f':pd.Series('Fizz',index=filter(lambda x: x% 3 == 0, range(1,rng+1))), 'b':pd.Series('Buzz',index=filter(lambda x: x% 5 == 0, range(1,rng+1))), 'n':pd.Series( filter(lambda x: x%3>0 and x%5>0,range(1,rng+1)), index=filter(lambda x: x%3>0 and x%5>0,range(1,rng+1)) ).apply(str) } ).apply( lambda x: x[~pd.isnull(x)].str.cat(), axis=1 ) #our more succinct, pandas only solution. def fb_pandas_shorter(rng): return pd.concat( [ pd.Series('Fizz', index=range(1, rng+1)).where(lambda x: x.index % 3 ==0, other=''), pd.Series('Buzz', index=range(1, 100)).where(lambda x: x.index % 5 ==0, other=''), pd.Series(range(1,rng+1),index=range(1,rng+1)).where(lambda x:(x % 5 > 0) & (x % 3 > 0), '').apply(str), ], axis=1 ).apply(lambda x: x.str.cat(), axis=1) #our shortest solution, relying on some non-pandas stuff def fb_pandas_shortest(rng): return pd.concat( [ pd.Series('Fizz', range(0, rng+1, 3)), pd.Series('Buzz', range(0, rng+1, 5)), pd.Series(range(rng+1)).where(lambda x:(x % 5 > 0) & (x % 3 > 0), '').apply(str), ], axis=1 ).apply(lambda x: x.str.cat(), axis=1)[1:] #Let's do some testing! functions = [ fb_vanilla, fb_pandas_vanilla, fb_pandas_long, fb_pandas_shorter, fb_pandas_shortest ] times = {x.__name__:[] for x in functions} tests = range(1,1000,100) from time import time for i in tests: for x in functions: t1 = time() _ = x(i) t2 = time() times[x.__name__].append(t2-t1) results = pd.DataFrame(times, index = tests) Well, so far so terrible. The first, longest solution actually scales O(n), which I find hilarious for some reason. The rest of my attempts fare a little better, but nothing compares to the vanilla python solution, which doesn't even show up on this chart, because it completes in <1ms. Let's discard that long solution, and try an even more strenuous test, up to 100,000. That seems pretty conclusive. Get me Google on the phone, I have solved this problem. When should this be used? NEVER. Like, seriously, those results are beyond terrible. I was expecting the pandas solutions to fall down compared to the vanilla python solutions, but this is ridiculous. If you're asked to implement fizzbuzz in your next interview, the only possible reason you would have for using any of these is because you're a flippant contrarian like me. Having said that, it's important to note that I still love pandas, and don't blame these results on the fantastic devs there. This is thoroughly not what the library is designed for, and I'm only doing this to test my own fluency with the tool. Overall I think I acheived my goal with this. I made myself chuckle a few times, and I learnt a few things about pandas in the process. If you have any suggestions for optimisations to this code, reach out in the comments or on Twitter. Equally, if you have any thoughts on why specifically these pandas functions are SOOO much slower than the vanilla solution, I'd be interested to hear them.
Read more
  • 0
  • 0
  • 3929

article-image-nwjs-app-and-shortcut-apis
Adam Lynch
18 Dec 2015
5 min read
Save for later

NW.js: The App and Shortcut APIs

Adam Lynch
18 Dec 2015
5 min read
The NW.js GUI library provides an "App" API, which contains a variety of methods and properties, some of which are essential to pretty much any app, and some have more obscure use cases. You can access the API as follows: var gui = require('nw.gui'); gui.App.quit(); As you can see from the example, the App API contains a quit method, which will kill your application. gui.App.argv, gui.App.dataPath, and gui.App.manifest are properties containing an array of arguments passed to your application when it was executed, an object representing your app's JSON manifest, and the application's data path in user's directory. gui.App.dataPath is typically a directory with the name you gave as the name property in your app manifest, located in the current user's "AppData/Local/" directory on Windows, ~/Library/Application Support/ on Mac OS X, or in ~/.config/ on Linux. vargui. = require('nw.gui'); gui.App.on('open', function(command){ gui.App.closeAllWindows(); }); The App API gives us two events we can listen for: open and reopen. The open event is fired when someone opens a file with your application, i.e. from the command line like: myapp a.txt. The function passed will receive the entire command (myapp a.txt) as the only argument. The reopen is exclusive to Mac OS X and is fired when the user clicks the dock icon for your app while it is already running. Also used in the example is the gui.App.closeAllWindows method which could come in handy if your app contains multiple windows. Other methods out of scope for this post include ones for getting and setting proxy configuration, editing cross-origin policies, setting where crash dumps get written to when NW.js itself crashes, and forcing a crash in the browser or renderer. Keyboard shortcuts This is also where you'll find methods to add or remove "global hot keys", i.e. keyboard shortcuts. To add a shortcut, you could do like this: var gui = require('nw.gui'); var shortcut = newgui.Shortcut({ key : "V", active : function() { console.log("Shortcut: " + this.key + " pressed."); }, failed : function(msg) { // Error adding / parsing the key console.log(msg); } }); gui.App.registerGlobalHotKey(shortcut); With the above code, any time the user presses the A key, the active callback is called. We create a new Shortcut instance (another piece of the the NW.js' GUI library) and pass it to gui.App.registerGlobalHotKey. The failed callback is called if there was a problem adding or parsing the key option. This is useful for development because there are some peculiar restrictions on what can be passed as the key option. The key option has to contain exactly one "key" (no more, no less) and can contain zero or more "modifiers". A "key" is one of the following: A-Z, 0-9, Comma, Period, Home, End, PageUp, PageDown, Insert, Delete, Arrow keys (Up, Down, Left, Right) and the Media Keys (MediaNextTrack, MediaPlayPause, MediaPrevTrack, MediaStop). The supported "modifiers" are: Ctrl, Alt, and Shift. Strangely, it was decided that on Mac OS X Ctrl would bind to Command instead, intentionally. I find this very strange as it seems you cannot bind any shortcuts which use the ctrl key on a Mac because of this. Hopefully in a future version Ctrl will map to Ctrl, Command will be supported, and it would be up to the user to check the current platform and bind to the correct keys accordingly. For clarity, here are a few example key bindings: - A: Valid. - A+B: Fails; you're not allowed to have multiple "keys". - Alt+Shift+T: Valid. - Ctrl+B: Valid but maps to Command+B on Mac OS X. It's not recommended to bind a shortcut to just one "key" like the A key as it'll block usage of that key for other applications while your app is running or until your app "unregisters" it. Unbinding a shortcut The API is pretty symmetric in that you can call gui.App.unregisterGlobalHotKey to remove or unbind a shortcut. You do have to pass the Shortcut instance again though which is a bit awkward. Thankfully, you can also pass a new Shortcut instance with just the key option as a workaround. So either of the last two lines here would work: var gui = require('nw.gui'); var shortcut = newgui.Shortcut({ key : 'V', active : function() { console.log('Shortcut: ' + this.key + ' pressed.'); }, failed : function(msg) { console.log(msg); } }); gui.App.registerGlobalHotKey(shortcut); gui.App.unregisterGlobalHotKey(shortcut); // option 1 gui.App.unregisterGlobalHotKey({key: 'V'}); // option 2 Events The Shortcut instance emits active and failed events as well as accepting them as options. You could either or both if you'd like. Here's an unrealistic example: var gui = require('nw.gui'); var shortcut = newgui.Shortcut({ key : 'V', active : function() { console.log('ACTIVE: Constructor option'); }, failed : function(msg) { console.log('FAILED (Constructor option): ' + msg); } }); shortcut.on('active', function(){ console.log('ACTIVE: Event listener'); }); shortcut.on('failed', function(msg){ console.log('FAILED (Event listener): ' + msg); }); gui.App.registerGlobalHotKey(shortcut); System-wide These shortcuts are system-wide and will be called even if your app isn't focused. If you'd like to have shortcuts which do something when your app is focused, then you could check if the app is focused in the active callback using the Window API we'll cover later. Summary and Alternative This article provides a quick look at the App API, but if you really need to bind to keys, which aren't supported by this API, or if you can't use this API because your app will be used in both NW.js and on the Web, then you could use a JavaScript library to bind your shortcuts. These will not be "global" or "system-wide" though. About The Author Adam Lynch is a TeamworkChat Product Lead & Senior Software Engineer at Teamwork. He can be found on Twitter @lynchy010.
Read more
  • 0
  • 0
  • 3908
article-image-introduction-service-workers
Sebastian Müller
05 Jun 2015
7 min read
Save for later

An Introduction to Service Workers

Sebastian Müller
05 Jun 2015
7 min read
The shiny new Service Workers provide powerful features such as a scriptable cache for offline support, background syncing, and push notifications in your browser. Just like Shared Workers, a Service Worker runs in the background, but it can even run when your website is not actually open. These new features can make your web apps feel more like native mobile apps. Current Browser Support As of writing this article, Service Workers is enabled in Chrome 41 by default. But this does not mean that all features described in the W3 Service Workers Draft are fully implemented yet. The implementation is in the very early stages and things may change. In this article, we will cover the basic caching features that are currently available in Chrome. If you want to use Service Workers in Firefox 36, it is currently a flagged feature that you must enable manually. In order to do this, type “about:config” into your URL field and search for “service worker” in order to set the “dom.Service Workers.enabled” setting to true. After a restart of Firefox, the new API is available for use. Let’s get started - Registering a Service Worker index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>ServiceWorker Demo</title> </head> <body> <script> if ('serviceWorker' in navigator) { navigator.serviceWorker.register('my-service-worker.js') .then(function(registration) { console.log('yay! serviceWorker installed!', registration); }, function(error) { console.log('something went wrong!:', error); }); } </script> </body> </html> To register a Service Worker, we call navigator.serviceWorker.register() with a path to our Service Worker file. Due to security reasons, it is important that your service worker file is located at the top-level relative to your website. Paths like ‘scripts/my-service-worker.js’ won’t work. The register method returns a Promise, which is fulfilled when the installation process is successful. The promise can be rejected if you, e.g., have a syntax error in your Service Worker file. Cool, so let’s review what a basic Service Worker that lives in the ‘my-service-worker.js’ file might look like. A basic Service Worker my-service-worker.js: this.addEventListener('install', function(event) { console.log('install event!'); // some logic here... }); In our Service Worker file, we can register event listeners for several events that are triggered by the browser. In our case, we listen for the ‘install’ event, which is triggered when the browser sees the Service Worker the first time. Later on, we will add some code to the ‘install’ event listener to make our web app offline ready. For now, we add a simple ‘console.log’ message to be able to check that our event listener function was called. Now when you open the index.html file in your browser (important: you need to be running a webserver to serve these two files), you should see a success log message in the Chrome developer tools console. You might wonder why the ‘install event!’ log message from the service worker file is not showing up in the console. This is due to the fact that all Service Workers are running in a separate thread. Next, we will cover how to debug Service Workers. In Chrome, you can open the URL “chrome://serviceworker-internals” to get a list of Service Workers registered in your browser: When you visit this page, right after you’ve visited the index.html, you should see a worker with the installation status: ‘ACTIVATED’ and running status ‘RUNNING’. Then you will know that everything went fine. After a while, the worker should be in the running status ‘STOPPED’. This is due to the fact that Chrome completely handles the lifetime of a Service Worker. You have no guarantee how long your service worker runs after the installation, for example. After digging into the basics of installing Service Workers, we clearly have no advantages yet. So let’s take look at the described offline caching features of Service Workers next. Make your Web apps Offline Ready Let’s face it: The Cache Manifest standard to make your apps offline ready has some big disadvantages. It’s not possible to script the caching mechanism in any way. You have to let the browser handle the caching logic. With Service Workers, the browser gives you the moving parts and lets you handle the caching stuff the way you want. So let’s dive into the basic caching mechanisms that Service Workers provide. Let’s get back to our index.html file and add an image named ‘my-image.png’ to the body that we want to have available when we are offline: <body> <img src="my-image.png" alt="my image" /> … </body> Now that we have an image in our index.html, let’s extend our existing service worker to cache our image ‘my-image.png’ and our index.html for offline usage: // (1.) importScripts('./cache-polyfill.js'); // this event listener gets triggered when the browser sees the ServiceWorker the first time this.addEventListener('install', function(event) { console.log('install!'); // (2.) event.waitUntil( caches.open('my-cache') .then(function(cache) { console.log('cache opened'); // (3.) return cache.addAll([ '/', '/my-image.png' ]); }) ); }); this.addEventListener('fetch', function(event) { // (4.) event.respondWith( caches.match(event.request).then(function(response) { if (response) { // (5.) console.log('found response in cache for:', event.request.url); return response; } // (6.) return fetch(event.request); }) ); }); We use a global function available in the Service Worker context called ‘importScripts’ that lets us load external scripts for using libraries and other stuff in our Service Worker’s logic. As of writing this article, not all caching API are implemented in the current version of Chrome. This is why we are loading a cache polyfill that adds the missing API that is needed for our application to work in the browser. In our install event listener, we use the waitUntil method from the provided event object to tell the browser with a promise when the installation process in our Service Worker is finished. The provided promise is the return value of the caches.open() method that opens the cache with name ‘my-cache’. When the cache has been opened successfully, we add the index.html and our image to cache. The browser pre-fetches all defined files and adds them to the cache. Only when all requests have been successfully executed is the installation step of the service worker finished and the Service Worker can be activated by the browser. The event listener for the event type ‘fetch’ is called when the browser wants to fetch a resource, e.g., an image. In this listener, you have full control of what you want to send as a response with the event.respondWith() method. In our case, we open up the cache used in the install event to see if we have a cached response for the given request. When we find a matching response for the given request, we return the cached response. With this mechanism, you are able to serve all cached files, even if you are offline or the webserver is down. We have no cached response for the given request and the browser will handle the fetching for the uncached file with the shiny new fetch API. To see if the cache is working as intended, open the index.html file your browser and shut down your web server afterward. When you refresh your page, you should get the index.html and the my-image.png file out of the Service Worker cache: With this few lines of Service Worker code, you have implemented a basic offline-ready web application that caches your index.html and your image file. As already mentioned, this is only the beginning of Service Workers and many more features like push notifications will be added this year. About the Author Sebastian Müller is Senior Software Engineer at adesso AG in Dortmund, Germany. He spends his time building Single Page Applications and is interested in JavaScript Architectures. He can be reached at @Sebamueller on Twitter and as SebastianM on Github.
Read more
  • 0
  • 0
  • 3863

article-image-best-tools-improve-your-development-workflow
Antonio Cucciniello
01 Nov 2017
5 min read
Save for later

The best tools to improve your development workflow

Antonio Cucciniello
01 Nov 2017
5 min read
For thoseweb developers out there who are looking for some tools that can help them become more productive and efficient, this list is for you. I will go through some of the basic tools you can take a look at in order to become a better web developer. Text editors First off, where do we write our code? That's right, in text editors. You need to have a text editor that you trust and one that you love. I have used a couple that I will recommend that you check out. Currently I am using Atom. It is a text editor that is minimal but can have plenty of features added to it. You may install various plugins that make things easier, or connect it to things like GitHub for your source control needs with your project. Another text editor I use is Sublime Text. This is extremely similar to Atom. It is actually a little faster to open than Atom as well. The only issue with this editor is that when you are using the free version, it asks you to donate or buy every couple of times you save a file. This can get annoying, but nonetheless, it's still a very powerful editor. The main key here is to find something that you love. Stick with it, and learn the ins and outs of it. You want to become a pro with it. This will greatly increase your productivity if you know your text editor inside and out. Source control High on the list of must have tools for web development, or even just development in general, is a form of Source Control. You need a place to backup your code and save in multiple states. It also allows for better and easier collaboration between multiple people working in different branches. I recommend using git and GitHub. The user interface is very friendly and the two integrate seamlessly. I have also used Subversion and AWS Code Commit, but these did not leave as great as impression as GitHub did. Source Control is very important, so make sure that you have selected it and use it for every project. It also doubles as a place to display your code over time. Command line interfaces This is not a specific tool per say, because it is already part of your computer. If you have a Mac or Linux, it is terminal. If you have Windows it is the command shell. I recommend learning as many of the commands that you can. You can do things like create files, create directories, delete files, edit files, and so much more. It allows you to be so much more productive. You must think of this as your go to tool for plenty of things you end up doing. Pro Tip for Windows Users: I personally do not like the Windows Command Prompt nearly as much as the Unix one. Look into using Cygwin, which allows you to use Unix commands on a windows command prompt. Debugging If you are doing anything in web development you need to be able to debug your code. No one writes perfect code and you will inevitably spend time fixing bugs. In order to reduce time spent on bugs, you should look into a tool that can help you with debugging.  Personally, if you are using Google Chrome, I suggest using Chrome DevTools. It allows you to set breakpoints, edit code, and manipulate page elements as well as checking all CSS properties of the different HTML elements on the page. It is extremely powerful and can help you when debugging to see what is happening in real time on the webpage. HTTP client I believe you need something like Postman to test HTTP requests from web services. It makes it extremely easy to test and create APIs. You can make all different types of requests, pass in whatever headers you want, and even see what the response is like! This is important for any developer who needs to make API requests.  So, there you have it. These are the best tools for web development, in my opinion. I hope this list has helped you get started in improving your web development workflow. Always be on the lookout for improving your toolset as time goes on. You can always improve, so why not let these tools make it easier for you?  About the Author  Antonio Cucciniello is a Software Engineer with a background in C, C++ and JavaScript (Node.Js) from New Jersey.  His most recent project called Edit Docs is an Amazon Echo skill that allows users to edit Google Drive files using your voice.  He loves building cool things with software, reading books on self-help and improvement, finance, and entrepreneurship. Follow him on twitter @antocucciniello, and follow him on GitHub here: https://github.com/acucciniello. 
Read more
  • 0
  • 0
  • 3801
Modal Close icon
Modal Close icon