About Keras Models

knitr::opts_chunk$set(comment = NA, eval = FALSE)


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 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) %>% 

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.


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:

tweet_a <- layer_input(shape = c(140, 256))
tweet_b <- layer_input(shape = c(140, 256))

# This layer can take as input a matrix and will return a vector of size 64
shared_lstm <- layer_lstm(units = 64)

# When we reuse the same layer instance multiple times, the weights of the layer are also
# being reused (it is effectively *the same* layer)
encoded_a <- tweet_a %>% shared_lstm
encoded_b <- tweet_b %>% shared_lstm

# We can then concatenate the two vectors and add a logistic regression on top
predictions <- layer_concatenate(c(encoded_a, encoded_b), axis=-1) %>% 
  layer_dense(units = 1, activation = 'sigmoid')

# We define a trainable model linking the tweet inputs to the predictions
model <- keras_model(inputs = c(tweet_a, tweet_b), outputs = predictions)

Learn more by reading the Guide to the Functional API.


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.


All models share the following properties:


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


Keras Model


Keras Model composed of a linear stack of layers


Configure a Keras model for training


Train a Keras model


Evaluate a Keras model


Predict Method for Keras Models


Print a summary of a model

`save_model_hdf5()` `load_model_hdf5()`

Save/Load models using HDF5 files


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


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

Try the keras package in your browser

Any scripts or data that you put into this service are public.

keras documentation built on Oct. 9, 2019, 1:04 a.m.