MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / RNNCell

Class RNNCell

imperative/python/megengine/module/rnn.py:60–121  ·  view source on GitHub ↗

r"""An Elman RNN cell with tanh or ReLU non-linearity. .. math:: h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh}) If :attr:`nonlinearity` is `'relu'`, then ReLU is used in place of tanh. Args: input_size(:class:`int`): The number of expected features in the input `

Source from the content-addressed store, hash-verified

58
59
60class RNNCell(RNNCellBase):
61
62 r"""An Elman RNN cell with tanh or ReLU non-linearity.
63
64 .. math::
65
66 h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh})
67
68 If :attr:`nonlinearity` is `'relu'`, then ReLU is used in place of tanh.
69
70 Args:
71 input_size(:class:`int`): The number of expected features in the input `x`.
72 hidden_size(:class:`int`): The number of features in the hidden state `h`.
73 bias(:class:`bool`): If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True``.
74 nonlinearity(:class:`str`): The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'``
75
76 Shape:
77 - Inputs: input, hidden
78 input: `(batch, input_size)`. Tensor containing input features.
79 hidden: `(batch, hidden_size)`. Tensor containing the initial hidden state for each element in the batch. Defaults to zero if not provided.
80 - Outputs: h'
81 h': `(batch, hidden_size)`. Tensor containing the next hidden state for each element in the batch.
82
83 Examples:
84
85 .. code-block::
86
87 import numpy as np
88 import megengine as mge
89 import megengine.module as M
90
91 m = M.RNNCell(10, 20)
92 inp = mge.tensor(np.random.randn(3, 10), dtype=np.float32)
93 hx = mge.tensor(np.random.randn(3, 20), dtype=np.float32)
94 out = m(inp, hx)
95 print(out.numpy().shape)
96
97 Outputs:
98
99 .. code-block::
100
101 (3, 20)
102
103 """
104
105 def __init__(
106 self,
107 input_size: int,
108 hidden_size: int,
109 bias: bool = True,
110 nonlinearity: str = "tanh",
111 ) -> None:
112 self.nonlinearity = nonlinearity
113 super(RNNCell, self).__init__(input_size, hidden_size, bias, num_chunks=1)
114
115 def forward(self, input: Tensor, hx: Optional[Tensor] = None) -> Tensor:
116 if hx is None:
117 hx = zeros(shape=(input.shape[0], self.gate_hidden_size),)

Callers 2

test_rnn_cellFunction · 0.90
create_cellMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_rnn_cellFunction · 0.72