View source on GitHub
|
Base class for recurrent layers.
Inherits From: Layer, Operation
tf.keras.layers.RNN(
cell,
return_sequences=False,
return_state=False,
go_backwards=False,
stateful=False,
unroll=False,
zero_output_for_mask=False,
**kwargs
)
Used in the notebooks
| Used in the tutorials |
|---|
Output shape:
- 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), wherestate_sizecould be a high dimension tensor shape. - If
return_sequences: 3D tensor with shape(batch_size, timesteps, output_size).
Masking:
This layer supports masking for input data with a variable number
of timesteps. To introduce masks to your data,
use a keras.layers.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=Truein the layer constructor. - Specify a fixed batch size for your model, by passing
batch_size=...to theInputlayer(s) of your model. Remember to also specify the samebatch_size=...when callingfit(), or otherwise use a generator-like data source like akeras.utils.PyDatasetor atf.data.Dataset. - Specify
shuffle=Falsewhen callingfit(), 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
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 layer.
You can specify the initial state of RNN layers numerically by
calling reset_state() with the keyword argument states. The value of
states should be a numpy array or list of numpy arrays representing
the initial state of the RNN layer.
Examples:
from keras.layers import RNN
from keras import ops
# First, let's define a RNN Cell, as a layer subclass.
class MinimalRNNCell(keras.Layer):
def __init__(self, units, **kwargs):
super().__init__(**kwargs)
self.units = units
self.state_size = units
def build(self, input_shape):
self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
initializer='uniform',
name='kernel')
self.recurrent_kernel = self.add_weight(
shape=(self.units, self.units),
initializer='uniform',
name='recurrent_kernel')
def call(self, inputs, states):
prev_output = states[0]
h = ops.matmul(inputs, self.kernel)
output = h + ops.matmul(prev_output, self.recurrent_kernel)
return output, [output]
# Let's use this cell in a RNN layer:
cell = MinimalRNNCell(32)
x = keras.Input((None, 5))
layer = RNN(cell)
y = layer(x)
# Here's how to use the cell to build a stacked RNN:
cells = [MinimalRNNCell(32), MinimalRNNCell(64)]
x = keras.Input((None, 5))
layer = RNN(cells)
y = layer(x)
Methods
from_config
@classmethodfrom_config( config, custom_objects=None )
Creates an operation from its config.
This method is the reverse of get_config, capable of instantiating the
same operation from the config dictionary.
if "dtype" in config and isinstance(config["dtype"], dict):
policy = dtype_policies.deserialize(config["dtype"])
| Args | |
|---|---|
config
|
A Python dictionary, typically the output of get_config.
|
| Returns | |
|---|---|
| An operation instance. |
get_initial_state
get_initial_state(
batch_size
)
inner_loop
inner_loop(
sequences, initial_state, mask, training=False
)
reset_state
reset_state()
reset_states
reset_states()
View source on GitHub