Writing Custom Keras Models


In addition to sequential models and models created with the functional API, you may also define models by defining a custom call() (forward pass) operation.

To create a custom Keras model, you call the keras_model_custom() function, passing it an R function which in turn returns another R function that implements the custom call() (forward pass) operation. The R function you pass takes a model argument, which provides access to the underlying Keras model object should you need it.

Typically, you’ll wrap your call to keras_model_custom() in yet another function that enables callers to easily instantiate your custom model.

Creating a Custom Model

This example demonstrates the implementation of a simple custom model that implements a multi-layer-perceptron with optional dropout and batch normalization:

Note that we include a name parameter so that users can optionally provide a human readable name for the model.

Note also that when we create layers to be used in our forward pass we set them onto the self object so they are tracked appropriately by Keras.

In call(), you may specify custom losses by calling self$add_loss(). You can also access any other members of the Keras model you need (or even add fields to the model) by using self$.