library(keras) 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) %>% 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.
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
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:
model$layers --- A flattened list of the layers comprising the model graph.
model$inputs --- List of input tensors.
model$outputs --- List of output tensors.
These functions enable you to create, train, evaluate, persist, and generate predictions with models:
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/Load models using HDF5 files
Retrieves a layer based on either its name (unique) or index.
Remove the last layer in a model
Save/Load model weights using HDF5 files
Layer/Model weights as R arrays
Model configuration as JSON
Model configuration as YAML
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.