Base class for recurrent layers
layer_rnn( object, cell, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, time_major = FALSE, ..., zero_output_for_mask = FALSE )
What to compose the new
Layer instance with. Typically a
Sequential model or a Tensor (e.g., as returned by
The return value depends on
Layer instance is returned.
Sequential model, the model with an additional layer is returned.
a Tensor, the output tensor from
layer_instance(object) is returned.
A RNN cell instance or a list of RNN cell instances. A RNN cell is a class that has:
call(input_at_t, states_at_t) method, returning
(output_at_t, states_at_t_plus_1). The call method of the
cell can also take the optional argument
section "Note on passing external constants" below.
state_size attribute. This can be a single integer
(single state) in which case it is the size of the recurrent
state. This can also be a list of integers (one size per state).
state_size can also be TensorShape or list of
TensorShape, to represent high dimension state.
output_size attribute. This can be a single integer or a
TensorShape, which represent the shape of the output. For backward
compatible reason, if this attribute is not available for the
cell, the value will be inferred by the first element of the
get_initial_state(inputs=NULL, batch_size=NULL, dtype=NULL)
method that creates a tensor meant to be fed to
call() as the
initial state, if the user didn't specify any initial state via other
means. The returned initial state should have a shape of
[batch_size, cell$state_size]. The cell might choose to create a
tensor full of zeros, or full of other values based on the cell's
inputs is the input tensor to the RNN layer, which should
contain the batch size as first dimension (
and also dtype (
inputs$dtype). Note that
shape might be
NULL during the graph construction. Either
inputs or the pair of
dtype are provided.
batch_size is a scalar tensor that represents the batch size
of the inputs.
tf.DType that represents the dtype of
For backward compatibility, if this method is not implemented
by the cell, the RNN layer will create a zero filled tensor with the
In the case that
cell is a list of RNN cell instances, the cells
will be stacked on top of each other in the RNN, resulting in an
efficient stacked RNN.
FALSE). Whether to return the last
output in the output sequence, or the full sequence.
FALSE). Whether to return the last state
in addition to the output.
TRUE, process the input sequence backwards and return the
TRUE, the last state
for each sample at index
i in a batch will be used as initial
state for the sample of index
i in the following batch.
If TRUE, the network will be unrolled, else a symbolic loop will be used.
Unrolling can speed-up a RNN, although it tends to be more
memory-intensive. Unrolling is only suitable for short sequences.
The shape format of the
TRUE, the inputs and outputs will be in shape
(timesteps, batch, ...), whereas in the FALSE case, it will be
(batch, timesteps, ...). Using
time_major = TRUE is a bit more
efficient because it avoids transposes at the beginning and end of the
RNN calculation. However, most TensorFlow data is batch-major, so by
default this function accepts input and emits output in batch-major
standard layer arguments.
Whether the output should use zeros for the masked timesteps. Note that
this field is only used when
return_sequences is TRUE and mask is
provided. It can useful if you want to reuse the raw output sequence of
the RNN without interference from the masked timesteps, eg, merging
See the Keras RNN API guide for details about the usage of RNN API.
inputs: Input tensor.
mask: Binary tensor of shape
[batch_size, timesteps] indicating whether
a given timestep should be masked. An individual
TRUE entry indicates
that the corresponding timestep should be utilized, while a
entry indicates that the corresponding timestep should be ignored.
training: R or Python Boolean indicating whether the layer should behave in
training mode or in inference mode. This argument is passed to the cell
when calling it. This is for use with cells that use dropout.
initial_state: List of initial state tensors to be passed to the first
call of the cell.
constants: List of constant tensors to be passed to the cell at each
N-D tensor with shape
(batch_size, timesteps, ...),
(timesteps, batch_size, ...) when
time_major = TRUE.
return_state: a list of tensors. The first tensor is
the output. The remaining tensors are the last states,
each with shape
(batch_size, state_size), where
could be a high dimension tensor shape.
return_sequences: N-D tensor with shape
[batch_size, timesteps, output_size], where
output_size could be a high dimension tensor shape, or
[timesteps, batch_size, output_size] when
else, N-D tensor with shape
[batch_size, output_size], where
output_size could be a high dimension tensor shape.
This layer supports masking for input data with a variable number of
timesteps. To introduce masks to your data, use
layer_embedding() with the
mask_zero parameter set to
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/
To enable statefulness:
stateful = TRUE in the layer constructor.
Specify a fixed batch size for your model. For sequential models,
batch_input_shape = list(...) to the first layer in your model.
For functional models with 1 or more Input layers, pass
batch_shape = list(...) to all the first layers in your model.
This is the expected shape of your inputs including the batch size.
It should be a list of integers, e.g.
list(32, 10, 100).
For dimensions which can vary (are not known ahead of time),
NULL in place of an integer, e.g.
list(32, NULL, NULL).
shuffle = FALSE when calling
To reset the states of your model, call
layer$reset_states() on either
a specific layer, or on your entire model.
You can specify the initial state of RNN layers symbolically by calling them
with the keyword argument
initial_state. The value of initial_state should
be a tensor or list of tensors representing the initial state of the RNN
You can specify the initial state of RNN layers numerically by calling
reset_states with the named argument
states. The value of
be an array or list of arrays representing the initial state of the RNN
You can pass "external" constants to the cell using the
RNN$__call__ (as well as
RNN$call) method. This requires that the
cell$call method accepts the same keyword argument
constants. Such constants
can be used to condition the cell transformation on additional static inputs
(not changing over time), a.k.a. an attention mechanism.