Building deep learning models

The core data structure of Keras is a model, which is a way to organize layers. There are two types of model:

  • Sequential: The main type of model. It is simply a linear stack of layers.
  • Keras functional API: These are used for more complex architectures.

You define a sequential model as follows:

from keras.models import Sequential
model = Sequential()

Once a model is defined, you can add one or more layers. The stacking operation is provided by the add() statement:

from keras.layers import Dense, Activation

For example, add a first fully connected NN layer and the Activation function:

model.add(Dense(output_dim=64, input_dim=100))
model.add(Activation("relu"))

Then add a second softmax layer:

model.add(Dense(output_dim=10))
model.add(Activation("softmax"))

If the model looks fine, you must compile the model by using the model.compile function, specifying the loss function and the optimizer function to be used:

model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])

You may configure your optimizer. Keras tries to make programming reasonably simple, allowing the user to be fully in control when they need to be. Once compiled, the model must be fitted to the data:

model.fit(X_train, Y_train, nb_epoch=5, batch_size=32

Alternatively, you can feed batches to your model manually:

model.train_on_batch(X_batch, Y_batch)

Once trained, you can use your model to make predictions on new data:

classes = model.predict_classes(X_test, batch_size=32)
proba = model.predict_proba(X_test, batch_size=32)

We can summarize the construction of deep learning models in Keras as follows:

  1. Define your model: Create a sequence and add layers.
  2. Compile your model: Specify loss functions and optimizers.
  3. Fit your model: Execute the model using data.
  4. Evaluate the model: Keep an evaluation of your training dataset.
  5. Make predictions: Use the model to generate predictions on new data.

The following figure depicts the preceding processes:

Keras programming model

In the following section, we'll look at how to use the Keras sequential model to study the sentiment classification problem of movie reviews.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset