View source on GitHub |
Gated Recurrent Unit - Cho et al. 2014.
Inherits From: GRU
, RNN
, Layer
, Module
tf.keras.layers.GRU(
units,
activation='tanh',
recurrent_activation='sigmoid',
use_bias=True,
kernel_initializer='glorot_uniform',
recurrent_initializer='orthogonal',
bias_initializer='zeros',
kernel_regularizer=None,
recurrent_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
recurrent_constraint=None,
bias_constraint=None,
dropout=0.0,
recurrent_dropout=0.0,
return_sequences=False,
return_state=False,
go_backwards=False,
stateful=False,
unroll=False,
time_major=False,
reset_after=True,
**kwargs
)
See the Keras RNN API guide for details about the usage of RNN API.
Based on available runtime hardware and constraints, this layer will choose different implementations (cuDNN-based or pure-TensorFlow) to maximize the performance. If a GPU is available and all the arguments to the layer meet the requirement of the cuDNN kernel (see below for details), the layer will use a fast cuDNN implementation.
The requirements to use the cuDNN implementation are:
activation
==tanh
recurrent_activation
==sigmoid
recurrent_dropout
== 0unroll
isFalse
use_bias
isTrue
reset_after
isTrue
- Inputs, if use masking, are strictly right-padded.
- Eager execution is enabled in the outermost context.
There are two variants of the GRU implementation. The default one is based on v3 and has reset gate applied to hidden state before matrix multiplication. The other one is based on original and has the order reversed.
The second variant is compatible with CuDNNGRU (GPU-only) and allows
inference on CPU. Thus it has separate biases for kernel
and
recurrent_kernel
. To use this variant, set reset_after=True
and
recurrent_activation='sigmoid'
.
For example:
inputs = tf.random.normal([32, 10, 8])
gru = tf.keras.layers.GRU(4)
output = gru(inputs)
print(output.shape)
(32, 4)
gru = tf.keras.layers.GRU(4, return_sequences=True, return_state=True)
whole_sequence_output, final_state = gru(inputs)
print(whole_sequence_output.shape)
(32, 10, 4)
print(final_state.shape)
(32, 4)
Methods
get_dropout_mask_for_cell
get_dropout_mask_for_cell(
inputs, training, count=1
)
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
Args | |
---|---|
inputs
|
The input tensor whose shape will be used to generate dropout mask. |
training
|
Boolean tensor, whether its in training mode, dropout will be ignored in non-training mode. |
count
|
Int, how many dropout mask will be generated. It is useful for cell that has internal weights fused together. |
Returns | |
---|---|
List of mask tensor, generated or cached mask based on context. |
get_recurrent_dropout_mask_for_cell
get_recurrent_dropout_mask_for_cell(
inputs, training, count=1
)
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
Args | |
---|---|
inputs
|
The input tensor whose shape will be used to generate dropout mask. |
training
|
Boolean tensor, whether its in training mode, dropout will be ignored in non-training mode. |
count
|
Int, how many dropout mask will be generated. It is useful for cell that has internal weights fused together. |
Returns | |
---|---|
List of mask tensor, generated or cached mask based on context. |
reset_dropout_mask
reset_dropout_mask()
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call()
method so
that the cached mask is cleared before calling the cell.call()
. The mask
should be cached across the timestep within the same batch, but shouldn't
be cached between batches. Otherwise it will introduce unreasonable bias
against certain index of data within the batch.
reset_recurrent_dropout_mask
reset_recurrent_dropout_mask()
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
reset_states
reset_states(
states=None
)
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful
= True
.
Args:
states: Numpy arrays that contains the value for the initial state, which
will be feed to cell at the first time step. When the value is None,
zero filled numpy array will be created based on the cell state size.
Raises | |
---|---|
AttributeError
|
When the RNN layer is not stateful. |
ValueError
|
When the batch size of the RNN layer is unknown. |
ValueError
|
When the input numpy array is not compatible with the RNN layer state, either size wise or dtype wise. |