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

Class LSTM

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

r"""Applies a multi-layer long short-term memory LSTM to an input sequence. For each element in the input sequence, each layer computes the following function: .. math:: \begin{array}{ll} \\ i_t = \sigma(W_{ii} x_t + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\

Source from the content-addressed store, hash-verified

429
430
431class LSTM(RNNBase):
432 r"""Applies a multi-layer long short-term memory LSTM to an input sequence.
433
434 For each element in the input sequence, each layer computes the following
435 function:
436
437 .. math::
438 \begin{array}{ll} \\
439 i_t = \sigma(W_{ii} x_t + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\
440 f_t = \sigma(W_{if} x_t + b_{if} + W_{hf} h_{t-1} + b_{hf}) \\
441 g_t = \tanh(W_{ig} x_t + b_{ig} + W_{hg} h_{t-1} + b_{hg}) \\
442 o_t = \sigma(W_{io} x_t + b_{io} + W_{ho} h_{t-1} + b_{ho}) \\
443 c_t = f_t \odot c_{t-1} + i_t \odot g_t \\
444 h_t = o_t \odot \tanh(c_t) \\
445 \end{array}
446
447 where :math:`h_t` is the hidden state at time `t`, :math:`c_t` is the cell
448 state at time `t`, :math:`x_t` is the input at time `t`, :math:`h_{t-1}`
449 is the hidden state of the layer at time `t-1` or the initial hidden
450 state at time `0`, and :math:`i_t`, :math:`f_t`, :math:`g_t`,
451 :math:`o_t` are the input, forget, cell, and output gates, respectively.
452 :math:`\sigma` is the sigmoid function, and :math:`\odot` is the Hadamard product.
453
454 In a multilayer LSTM, the input :math:`x^{(l)}_t` of the :math:`l` -th layer
455 (:math:`l >= 2`) is the hidden state :math:`h^{(l-1)}_t` of the previous layer multiplied by
456 dropout :math:`\delta^{(l-1)}_t` where each :math:`\delta^{(l-1)}_t` is a Bernoulli random
457 variable which is :math:`0` with probability :attr:`dropout`.
458
459 If ``proj_size > 0`` is specified, LSTM with projections will be used. This changes
460 the LSTM cell in the following way. First, the dimension of :math:`h_t` will be changed from
461 ``hidden_size`` to ``proj_size`` (dimensions of :math:`W_{hi}` will be changed accordingly).
462 Second, the output hidden state of each layer will be multiplied by a learnable projection
463 matrix: :math:`h_t = W_{hr}h_t`. Note that as a consequence of this, the output
464 of LSTM network will be of different shape as well. See Inputs/Outputs sections below for exact
465 dimensions of all variables. You can find more details in
466 `Long Short-Term Memory Based Recurrent Neural Network Architectures for Large Vocabulary Speech
467 Recognition<https://arxiv.org/abs/1402.1128>`.
468
469 Args:
470 input_size(:class:`int`): The number of expected features in the input `x`.
471 hidden_size(:class:`int`): The number of features in the hidden state `h`.
472 num_layers(:class:`int`): Number of recurrent layers. E.g., setting ``num_layers=2``
473 would mean stacking two LSTMs together to form a `stacked LSTM`,
474 with the second LSTM taking in outputs of the first LSTM and
475 computing the final results. Default: 1.
476 bias(:class:`bool`): If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`.
477 Default: ``True``.
478 batch_first(:class:`bool`): If ``True``, then the input and output tensors are provided
479 as `(batch, seq, feature)` instead of `(seq, batch, feature)`.
480 Note that this does not apply to hidden or cell states. See the
481 Inputs/Outputs sections below for details. Default: ``False``.
482 dropout(:class:`float`): If non-zero, introduces a `Dropout` layer on the outputs of each
483 LSTM layer except the last layer, with dropout probability equal to
484 :attr:`dropout`. Default: 0.
485 bidirectional(:class:`bool`): If ``True``, becomes a bidirectional LSTM. Default: ``False``.
486 proj_size(:class:`int`): If ``> 0``, will use LSTM with projections of corresponding size. Default: 0.
487
488 Shape:

Callers 1

test_lstmFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_lstmFunction · 0.72