Recurrent Layers
DL4J equivalents and API reference for Keras recurrent layers — SimpleRNN, LSTM, and associated utilities.
Keras Recurrent Layer Import
Recurrent layer support is implemented in the layers/recurrent package.
Support Summary
SimpleRNN
SimpleRnn
Yes
GRU
—
No
LSTM
LSTM
Yes
ConvLSTM2D
—
No
KerasSimpleRnn
Imports a Keras SimpleRNN layer as a DL4J SimpleRnn layer.
Constructor
public KerasSimpleRnn(Integer kerasVersion) throws UnsupportedKerasConfigurationExceptionPass-through constructor. kerasVersion is the major Keras version (1 or 2).
getSimpleRnnLayer
public Layer getSimpleRnnLayer()Returns the configured DL4J SimpleRnn layer.
Parameters (from Keras config):
layerConfig— dictionary containing Keras layer configuration
getOutputType
Returns the output InputType for this RNN layer.
Parameters:
inputType— array of inputInputTypeobjects
Returns: output InputType
getNumParams
Returns the number of trainable parameters. For a SimpleRNN with hidden size h and input size i, this is h*(h+i) + h (recurrent weights + input weights + bias).
getInputPreprocessor
Returns the appropriate DL4J InputPreProcessor if a format conversion is needed before the RNN layer (e.g., feed-forward to RNN).
getUnroll
Returns whether this SimpleRNN layer should be unrolled for truncated BPTT. Corresponds to the Keras unroll parameter.
setWeights
Loads weights into the layer. The weight map uses DL4J parameter names derived from the Keras HDF5 structure.
KerasLSTM
Imports a Keras LSTM layer as a DL4J LSTM layer. Both Keras 1 and Keras 2 LSTM configurations are supported.
Constructor
getLSTMLayer
Returns the configured DL4J LSTM layer.
getOutputType
getNumParams
Returns the number of trainable parameters (12 weight matrices for 4 gates × input + recurrent + bias terms).
getInputPreprocessor
setWeights
Loads LSTM gate weight matrices (kernel, recurrent kernel) and biases from the weight map.
getUnroll
Returns whether the LSTM should be unrolled for truncated BPTT.
getGateActivationFromConfig
Reads the LSTM gate (recurrent) activation function from the Keras layer configuration. Corresponds to the Keras recurrent_activation parameter (default: hard_sigmoid).
Parameters:
layerConfig— dictionary containing the Keras layer configuration
Returns: DL4J IActivation for the gate activation
getForgetBiasInitFromConfig
Reads the forget gate bias initialization value from the Keras config. Keras 2 uses unit_forget_bias=True by default, which initializes the forget gate bias to 1.
Parameters:
layerConfig— dictionary containing the Keras layer configurationtrain— whether this is the training phase
Returns: forget gate bias initialization value
KerasRnnUtils
Shared utilities for RNN layer import.
getUnrollRecurrentLayer
Reads the unroll parameter from the Keras layer configuration. Unrolling converts the RNN to a fixed-length sequence of operations; this can improve speed on short sequences but uses more memory.
getRecurrentDropout
Reads the recurrent dropout rate from the Keras layer configuration. Note: non-zero recurrent dropout rates are currently not supported in DL4J. If a model was trained with recurrent dropout, the dropout will be silently ignored at import time (inference behavior is unaffected).
Input Shape Convention
DL4J RNN layers expect input in [batch, features, timesteps] format (channels-first time axis). Keras RNNs use [batch, timesteps, features]. The importer automatically inserts the necessary RnnToFeedForwardPreProcessor or FeedForwardToRnnPreProcessor at layer boundaries.
When constructing input arrays manually for inference, use [batch, features, timesteps]:
Unsupported Layers
GRU: the Keras GRU gate formulation differs from DL4J's. Models using GRU must be redesigned with LSTM or SimpleRNN before import.
ConvLSTM2D: no DL4J equivalent. Models using ConvLSTM2D cannot currently be imported.
See Also
Last updated
Was this helpful?