SimpleRNNCell¶
- class paddle.nn. SimpleRNNCell ( input_size, hidden_size, activation='tanh', weight_ih_attr=None, weight_hh_attr=None, bias_ih_attr=None, bias_hh_attr=None, name=None ) [source]
-
Elman RNN (SimpleRNN) cell. Given the inputs and previous states, it computes the outputs and updates states.
The formula used is as follows:
ht=act(Wihxt+bih+Whhht−1+bhh)yt=htwhere act is for
activation
.Please refer to Finding Structure in Time for more details.
- Parameters
-
input_size (int) – The input size.
hidden_size (int) – The hidden size.
activation (str, optional) – The activation in the SimpleRNN cell. It can be tanh or relu. Defaults to tanh.
weight_ih_attr (ParamAttr, optional) – The parameter attribute for weightih. Default: None.
weight_hh_attr (ParamAttr, optional) – The parameter attribute for weighthh. Default: None.
bias_ih_attr (ParamAttr, optional) – The parameter attribute for the biasih. Default: None.
bias_hh_attr (ParamAttr, optional) – The parameter attribute for the biashh. Default: None.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
- Variables:
-
weight_ih (Parameter): shape (hidden_size, input_size), input to hidden weight, corresponding to Wih in the formula.
weight_hh (Parameter): shape (hidden_size, hidden_size), hidden to hidden weight, corresponding to Whh in the formula.
bias_ih (Parameter): shape (hidden_size, ), input to hidden bias, corresponding to bih in the formula.
bias_hh (Parameter): shape (hidden_size, ), hidden to hidden bias, corresponding to bhh in the formula.
- Inputs:
-
inputs (Tensor): shape [batch_size, input_size], the input, corresponding to xt in the formula.
states (Tensor, optional): shape [batch_size, hidden_size], the previous hidden state, corresponding to ht−1 in the formula. When states is None, zero state is used. Defaults to None.
- Returns
-
shape [batch_size, hidden_size], the output, corresponding to ht in the formula. - states (Tensor): shape [batch_size, hidden_size], the new hidden state, corresponding to ht in the formula.
- Return type
-
outputs (Tensor)
Notes
All the weights and bias are initialized with Uniform(-std, std) by default. Where std = 1√hidden_size. For more information about parameter initialization, please refer to ParamAttr.
Examples
>>> import paddle >>> x = paddle.randn((4, 16)) >>> prev_h = paddle.randn((4, 32)) >>> cell = paddle.nn.SimpleRNNCell(16, 32) >>> y, h = cell(x, prev_h) >>> print(y.shape) [4, 32]
-
forward
(
inputs,
states=None
)
forward¶
-
Defines the computation performed at every call. Should be overridden by all subclasses.
- Parameters
-
*inputs (tuple) – unpacked tuple arguments
**kwargs (dict) – unpacked dict arguments
- property state_shape
-
Abstract method (property). Used to initialize states. A (possiblely nested structure of) shape[s], where a shape is a list/tuple of integers (-1 for batch size would be automatically inserted into a shape if shape is not started with it). Not necessary to be implemented if states are not initialized by get_initial_states or the shape argument is provided when using get_initial_states.
-
extra_repr
(
)
extra_repr¶
-
Extra representation of this layer, you can have custom implementation of your own layer.