layer_rnn | R Documentation |
Base class for recurrent layers
layer_rnn(
object,
cell,
return_sequences = FALSE,
return_state = FALSE,
go_backwards = FALSE,
stateful = FALSE,
unroll = FALSE,
zero_output_for_mask = FALSE,
...
)
object |
Object to compose the layer with. A tensor, array, or sequential model. |
cell |
A RNN cell instance or a list of RNN cell instances. A RNN cell is a class that has:
|
return_sequences |
Boolean (default |
return_state |
Boolean (default |
go_backwards |
Boolean (default |
stateful |
Boolean (default |
unroll |
Boolean (default |
zero_output_for_mask |
Boolean (default |
... |
For forward/backward compatability. |
The return value depends on the value provided for the first argument.
If object
is:
a keras_model_sequential()
, then the layer is added to the sequential model
(which is modified in place). To enable piping, the sequential model is also
returned, invisibly.
a keras_input()
, then the output tensor from calling layer(input)
is returned.
NULL
or missing, then a Layer
instance is returned.
sequences
: A 3-D tensor with shape (batch_size, timesteps, features)
.
initial_state
: List of initial state tensors to be passed to the first
call of the cell.
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 FALSE
entry indicates
that the corresponding timestep should be ignored.
training
: 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.
3-D tensor with shape (batch_size, timesteps, features)
.
If 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 state_size
could
be a high dimension tensor shape.
If return_sequences
: 3D tensor with shape
(batch_size, timesteps, output_size)
.
This layer supports masking for input data with a variable number
of timesteps. To introduce masks to your data,
use a layer_embedding()
layer with the mask_zero
parameter
set to TRUE
.
Note on using statefulness in RNNs:
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.
To enable statefulness:
Specify stateful=TRUE
in the layer constructor.
Specify a fixed batch size for your model, by passing
batch_size=...
to the layer_input()
layer(s) of your model.
Remember to also specify the same batch_size=...
when
calling fit()
, or otherwise use a generator-like
data source like
tf.data.Dataset
.
Specify shuffle=FALSE
when calling fit()
, since your
batches are expected to be temporally ordered.
To reset the states of your model, call reset_state()
on either
a specific layer, or on your entire model.
Note on specifying the initial state of RNNs:
You can specify the initial state of RNN layers symbolically by
passing a named argument initial_state
to the layer or to reset_state()
.
The value of
initial_state
should be a tensor or list of tensors representing
the initial state of the RNN layer.
First, let's define a RNN Cell, as a layer subclass.
rnn_cell_minimal <- Layer( "MinimalRNNCell", initialize = function(units, ...) { super$initialize(...) self$units <- as.integer(units) self$state_size <- as.integer(units) }, build = function(input_shape) { self$kernel <- self$add_weight( shape = shape(tail(input_shape, 1), self$units), initializer = 'uniform', name = 'kernel' ) self$recurrent_kernel <- self$add_weight( shape = shape(self$units, self$units), initializer = 'uniform', name = 'recurrent_kernel' ) self$built <- TRUE }, call = function(inputs, states) { prev_output <- states[[1]] h <- op_matmul(inputs, self$kernel) output <- h + op_matmul(prev_output, self$recurrent_kernel) list(output, list(output)) } )
Let's use this cell in a RNN layer:
cell <- rnn_cell_minimal(units = 32) x <- layer_input(shape = shape(NULL, 5)) layer <- layer_rnn(cell = cell) y <- layer(x)
cells <- list(rnn_cell_minimal(units = 32), rnn_cell_minimal(units = 4)) x <- layer_input(shape = shape(NULL, 5)) layer <- layer_rnn(cell = cells) y <- layer(x)
Other rnn cells:
rnn_cell_gru()
rnn_cell_lstm()
rnn_cell_simple()
Other rnn layers:
layer_bidirectional()
layer_conv_lstm_1d()
layer_conv_lstm_2d()
layer_conv_lstm_3d()
layer_gru()
layer_lstm()
layer_simple_rnn()
layer_time_distributed()
rnn_cell_gru()
rnn_cell_lstm()
rnn_cell_simple()
rnn_cells_stack()
Other layers:
Layer()
layer_activation()
layer_activation_elu()
layer_activation_leaky_relu()
layer_activation_parametric_relu()
layer_activation_relu()
layer_activation_softmax()
layer_activity_regularization()
layer_add()
layer_additive_attention()
layer_alpha_dropout()
layer_attention()
layer_auto_contrast()
layer_average()
layer_average_pooling_1d()
layer_average_pooling_2d()
layer_average_pooling_3d()
layer_batch_normalization()
layer_bidirectional()
layer_category_encoding()
layer_center_crop()
layer_concatenate()
layer_conv_1d()
layer_conv_1d_transpose()
layer_conv_2d()
layer_conv_2d_transpose()
layer_conv_3d()
layer_conv_3d_transpose()
layer_conv_lstm_1d()
layer_conv_lstm_2d()
layer_conv_lstm_3d()
layer_cropping_1d()
layer_cropping_2d()
layer_cropping_3d()
layer_dense()
layer_depthwise_conv_1d()
layer_depthwise_conv_2d()
layer_discretization()
layer_dot()
layer_dropout()
layer_einsum_dense()
layer_embedding()
layer_equalization()
layer_feature_space()
layer_flatten()
layer_flax_module_wrapper()
layer_gaussian_dropout()
layer_gaussian_noise()
layer_global_average_pooling_1d()
layer_global_average_pooling_2d()
layer_global_average_pooling_3d()
layer_global_max_pooling_1d()
layer_global_max_pooling_2d()
layer_global_max_pooling_3d()
layer_group_normalization()
layer_group_query_attention()
layer_gru()
layer_hashed_crossing()
layer_hashing()
layer_identity()
layer_integer_lookup()
layer_jax_model_wrapper()
layer_lambda()
layer_layer_normalization()
layer_lstm()
layer_masking()
layer_max_num_bounding_boxes()
layer_max_pooling_1d()
layer_max_pooling_2d()
layer_max_pooling_3d()
layer_maximum()
layer_mel_spectrogram()
layer_minimum()
layer_mix_up()
layer_multi_head_attention()
layer_multiply()
layer_normalization()
layer_permute()
layer_rand_augment()
layer_random_brightness()
layer_random_color_degeneration()
layer_random_color_jitter()
layer_random_contrast()
layer_random_crop()
layer_random_flip()
layer_random_grayscale()
layer_random_hue()
layer_random_posterization()
layer_random_rotation()
layer_random_saturation()
layer_random_sharpness()
layer_random_shear()
layer_random_translation()
layer_random_zoom()
layer_repeat_vector()
layer_rescaling()
layer_reshape()
layer_resizing()
layer_separable_conv_1d()
layer_separable_conv_2d()
layer_simple_rnn()
layer_solarization()
layer_spatial_dropout_1d()
layer_spatial_dropout_2d()
layer_spatial_dropout_3d()
layer_spectral_normalization()
layer_stft_spectrogram()
layer_string_lookup()
layer_subtract()
layer_text_vectorization()
layer_tfsm()
layer_time_distributed()
layer_torch_module_wrapper()
layer_unit_normalization()
layer_upsampling_1d()
layer_upsampling_2d()
layer_upsampling_3d()
layer_zero_padding_1d()
layer_zero_padding_2d()
layer_zero_padding_3d()
rnn_cell_gru()
rnn_cell_lstm()
rnn_cell_simple()
rnn_cells_stack()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.