Overview

There are two types of built-in models available in Keras: sequential models and models created with the functional API. In addition, you can also create custom models that define their own forward-pass logic.

Sequential

Sequential models are created using the keras_model_sequential() function and are composed of a set of linear layers:

model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 32, input_shape = c(784)) %>% 
  layer_activation('relu') %>% 
  layer_dense(units = 10) %>% 
  layer_activation('softmax')

Note that Keras objects are modified in place which is why it’s not necessary for model to be assigned back to after the layers are added.

Learn more by reading the Guide to the Sequential Model.

Functional

The functional API enables you to define more complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. To create a model with the functional API compose a set of input and output layers then pass them to the keras_model() function:

Learn more by reading the Guide to the Functional API.

Custom

Custom models enable you to implement custom forward-pass logic (e.g. to encapsulate the logic associated with constructuing various types of models). See the article on Writing Custom Keras Models for additional documentation, including an example that demonstrates creating a custom model that encapsulates a simple multi-layer-perceptron model with optional dropout and batch normalization layers.

Properties

All models share the following properties:

  • model$layers — A flattened list of the layers comprising the model graph.

  • model$inputs — List of input tensors.

  • model$outputs — List of output tensors.

Functions

These functions enable you to create, train, evaluate, persist, and generate predictions with models:

keras_model()

Keras Model

keras_model_sequential()

Keras Model composed of a linear stack of layers

compile()

Configure a Keras model for training

fit()

Train a Keras model

evaluate()

Evaluate a Keras model

predict()

Predict Method for Keras Models

summary()

Print a summary of a model

save_model_hdf5() load_model_hdf5()

Save/Load models using HDF5 files

get_layer()

Retrieves a layer based on either its name (unique) or index.

pop_layer()

Remove the last layer in a model

save_model_weights_hdf5() load_model_weights_hdf5()

Save/Load model weights using HDF5 files

get_weights() set_weights()

Layer/Model weights as R arrays

get_config() from_config()

Layer/Model configuration

model_to_json() model_from_json()

Model configuration as JSON

model_to_yaml() model_from_yaml()

Model configuration as YAML