AI News, Keras tutorial – build a convolutional neural network in 11 lines
- On 8. marts 2018
- By Read More
Keras tutorial – build a convolutional neural network in 11 lines
In a previous tutorial, I demonstrated how to create a convolutional neural network (CNN) using TensorFlow to classify the MNIST handwritten digit dataset. TensorFlow is a brilliant tool, with lots of power and flexibility. However, for quick prototyping work it can be a bit verbose. Enter Keras and this Keras tutorial. Keras is a higher level library which operates over either TensorFlow or Theano, and is intended to stream-line the process of building deep learning networks. In fact, what was accomplished in the previous tutorial in TensorFlow in around 42 lines* can be replicated in only 11 lines* in Keras. This Keras tutorial will show you how to do this.
in this case we have 32 output channels (as per the architecture shown at the beginning). The next input is the kernel_size, which in this case we have chosen to be a 5×5 moving window, followed by the strides in the x and y directions (1, 1). Next, the activation function is a rectified linear unit and finally we have to supply the model with the size of the input to the layer (which is declared in another part of the code –
we don’t have to explicitly handle the batching up of our data during training in Keras, rather we just specify the batch size and it does it for us (I have a post on mini-batch gradient descent if this is unfamiliar to you). In this case we are using a batch size of 128. Next we pass the number of training epochs (10 in this case). The verbose flag, set to 1 here, specifies if you want detailed information being printed in the console about the progress of the training. During training, if verbose is set to 1, the following is output to the console: Finally, we pass the validation or test data to the fit function so Keras knows what data to test the metric against when evaluate() is run on the model. Ignore the callbacks argument for the moment –
To create a callback we create an inherited class which inherits from keras.callbacks.Callback: The Callback super class that the code above inherits from has a number of methods that can be overridden in our callback definition such as on_train_begin, on_epoch_end, on_batch_begin and on_batch_end. The name of these methods are fairly self explanatory, and represent moments in the training process where we can “do stuff”. In the code above, at the beginning of training we initialise a list self.acc =  to store our accuracy results. Using the on_epoch_end() method, we can extract the variable we want from the logs, which is a dictionary that holds, as a default, the loss and accuracy during training. We then instantiate this callback like so: Now we can pass history to the .fit() function using the callback parameter name. Note that .fit() takes a list for the callback parameter, so you have to pass it history like this: [history]. To access the accuracy list that we created after the training is complete, you can simply call history.acc, which I then also plotted: Hope that helps. Have fun using Keras.
Train a Keras model
Trains the model for a fixed number of epochs (iterations on a dataset).
history object that contains all information collected during
Getting started with the Keras Sequential model
You can create a Sequential model by passing a list of layer instances to the constructor: You can also simply add layers via the .add() method: The model needs to know what input shape it should expect.
For this reason, the first layer in a Sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape.
There are several possible ways to do this: As such, the following snippets are strictly equivalent: Before training a model, you need to configure the learning process, which is done via the compile method.
A detailed example of how to use data generators with Keras
python keras 2 fit_generator tensorflow large dataset Have you ever had to load a dataset that was so memory consuming that you wished a magic trick could seamlessly take care of that?
We have to keep in mind that in some cases, even the most state-of-the-art configuration won't have enough memory space to process the data the way we used to do it.
In this blog post, we are going to show you how to generate the dataset at hand in real time while feeding it right away to your deep learning model.
A good way to keep track of samples and their labels is to adopt the following framework: For example, let's say that our training set contains id-1, id-2 and id-3 with respective labels 0, 1 and 2, with a validation set containing id-4 with label 1.
In that case, the Python variables partition and labels look like and Also, for the sake of modularity, we will write Keras code and customized classes in separate files, so that your folder looks like Finally, it is good to note that the code in this tutorial is written for 3-dimensional data (i.e.
The private method in charge of this task is called __data_generation and only needs to know about the list of IDs included in batches as well as their corresponding labels.
Please note that Keras only accepts labels written in a binary form (in a 6-label problem, the third label is writtten [0 0 1 0 0 0]), which is why we need the sparsify function to perform this task, should y be a list of numerical values.
If your labels start at 1, simply change the expression y[i] == j to y[i] == j+1 in the piece of code above.