Path: blob/master/keras/nn_keras_basics.ipynb
2614 views
Table of Contents
Keras Basics
Basic Keras API to build a simple multi-layer neural network.
Basics of training a model:
The easiest way to build models in keras is to use Sequential model and the .add() method to stack layers together in sequence to build up our network.
We start with
Dense(fully-connected layers), where we specify how many nodes you wish to have for the layer. Since the first layer that we're going to add is the input layer, we have to make sure that theinput_dimparameter matches the number of features (columns) in the training set. Then after the first layer, we don't need to specify the size of the input anymore.Then we specify the
Activationfunction for that layer, and add aDropoutlayer if we wish.For the last
DenseandActivationlayer we need to specify the number of class as the output and softmax to tell it to output the predicted class's probability.
Once our model looks good, we can configure its learning process with .compile(), where you need to specify which optimizer to use, and the loss function ( categorical_crossentropy is the typical one for multi-class classification) and the metrics to track.
Finally, .fit() the model by passing in the training, validation set, the number of epochs and batch size. For the batch size, we typically specify this number to be power of 2 for computing efficiency.
Saving and loading the models
It is not recommended to use pickle or cPickle to save a Keras model. By saving it as a HDF5 file, we can preserve the configuration and weights of the model.