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

Class LSTMCell

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

r"""A long short-term memory (LSTM) cell. .. math:: \begin{array}{ll} i = \sigma(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\ f = \sigma(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\ g = \tanh(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\ o = \sigma(W_{io} x + b_{i

Source from the content-addressed store, hash-verified

122
123
124class LSTMCell(RNNCellBase):
125
126 r"""A long short-term memory (LSTM) cell.
127
128 .. math::
129
130 \begin{array}{ll}
131 i = \sigma(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\
132 f = \sigma(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\
133 g = \tanh(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\
134 o = \sigma(W_{io} x + b_{io} + W_{ho} h + b_{ho}) \\
135 c' = f * c + i * g \\
136 h' = o * \tanh(c') \\
137 \end{array}
138
139 where :math:`\sigma` is the sigmoid function, and :math:`*` is the Hadamard product.
140
141 Args:
142 input_size(:class:`int`): The number of expected features in the input `x`
143 hidden_size(:class:`int`): The number of features in the hidden state `h`
144 bias(:class:`bool`): If ``False``, then the layer does not use bias weights `b_ih` and
145 `b_hh`. Default: ``True``
146
147 Shape:
148 - Inputs: input, (h_0, c_0)
149 input: `(batch, input_size)`. Tensor containing input features.
150 h_0: `(batch, hidden_size)`. Tensor containing the initial hidden state for each element in the batch.
151 c_0: `(batch, hidden_size)`. Tensor containing the initial cell state for each element in the batch.
152 If `(h_0, c_0)` is not provided, both **h_0** and **c_0** default to zero.
153
154 - Outputs: (h_1, c_1)
155 h_1: `(batch, hidden_size)`. Tensor containing the next hidden state for each element in the batch.
156 c_1: `(batch, hidden_size)`. Tensor containing the next cell state for each element in the batch.
157
158 Examples:
159
160 .. code-block::
161
162 import numpy as np
163 import megengine as mge
164 import megengine.module as M
165
166 m = M.LSTMCell(10, 20)
167 inp = mge.tensor(np.random.randn(3, 10), dtype=np.float32)
168 hx = mge.tensor(np.random.randn(3, 20), dtype=np.float32)
169 cx = mge.tensor(np.random.randn(3, 20), dtype=np.float32)
170 hy, cy = m(inp, (hx, cx))
171 print(hy.numpy().shape)
172 print(cy.numpy().shape)
173
174 Outputs:
175
176 .. code-block::
177
178 (3, 20)
179 (3, 20)
180
181 """

Callers 2

test_lstm_cellFunction · 0.90
create_cellMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_lstm_cellFunction · 0.72