In this blog we will consider Keras models and API of Keras Model class. The main data structure of Keras is a model. A model is a way of organizing layers. There are two basic model types available in Keras: the Sequential model and the Model class used with the functional API. The simplest type of model is the Sequential model, which is a linear collection of layers. For more complex architectures you must use the Keras functional API, which allows you to create arbitrary network structure.

These models have a number of common properties and common methods:

  • model.layers – is a list of layers contained in the model.
  • model.inputs – is a list of model input tensors.
  • model.outputs – is a list of model output tensors.
  • model.summary() – returns a summary view of the model.
  • model.get_config() – returns a dictionary containing the model configuration.
  • model.get_weights() – returns a list of all weight tensors in the model.
  • model.set_weights(weights) – sets the values ​​of the model weights, from the array. The arrays in the list should have the same form as that returned by get_weights ().
  • model.to_json() – returns the representation of the model as a JSON string. This view does not include weight, but only architecture.

Here is an example of using the model.to_json() method:

from keras.models import model_from_json
json_string = model.to_json()
model = model.from_json(json_string)

Model  class API

Using the functional API, you can create an instance of the Model class for some input tensor and output tensor using the following code:

from keras.models import Model
from keras.layers import Input, Dense
a = Input(shape=(32,))
b = Dense(32)(a)
model = Model(inputs=a, outputs=b)

This model will include all the levels required to calculate b based on a. In the case of models with multiple inputs or multiple outputs, you can also use lists:

model = Model(inputs=[a1, a2], outputs=[b1, b2, b3])

Basic methods of the Model class

Let us consider the most important methods of the Model class, which are necessary for organizing the process of neural networks learning.

Method of setting up a model for learning:

compile (self, optimizer, loss = None, metrics = None, loss_weights = None, sample_weight_mode = None, weighted_metrics = None, target_tensors = None)

Training a model for a certain number of epochs:

fit (self, x = None, y = None, batch_size = None, epochs = 1, verbose = 1, callbacks = None, validation_split = 0.0, validation_data = None, shuffle = True, class_weight = None, sample_weight = None, initial_epoch = 0, steps_per_epoch = None, validation_steps = None)

The main arguments of this method are:

  • x: a training data array (if the model has one input) or a list of arrays (if the model has multiple inputs).
  • y: target data array (if the model has one output) or a list of arrays (if the model has multiple outputs).
  • batch_size: the number of samples to update the gradient. If not specified, batch_size will be set to 32 by default.
  • epochs: The number of epochs for model learning.
  • validation_split: Float between 0 and 1. The proportion of training data that will be used as validation data. The model will highlight this part of the training data, will not train on it, and will evaluate the error and any model indicators from this data at the end of each era.
  • initial_epoch: the epoch from which to start learning (useful for resuming the previous learning cycle).

Method for assessing the quality of the training model. This method returns the values ​​of errors and indicators for the model in test mode:

evaluate (self, x = None, y = None, batch_size = None, verbose = 1, sample_weight = None, steps = None)

Method to create output predictions for input samples:

predict(self, x, batch_size = None, verbose = 0, steps = None)

The main arguments are:

  • x: input data as in the form of an array (or a list of Numpy arrays, if the model has multiple inputs).
  • steps: the total number of steps (sample batches) before the prediction round is announced.

Method of extracting a layer based on its name or index. This method returns an instance of the layer:

get_layer (self, name = None, index = None)

The main arguments are:

  • name: String, the name of the layer.
  • index: Integer, index of the layer.