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

Class RNN

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

r"""Applies a multi-layer Elman RNN with :math:`\tanh` or :math:`\text{ReLU}` non-linearity to an input sequence. For each element in the input sequence, each layer computes the following function: .. math:: h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh}) whe

Source from the content-addressed store, hash-verified

306
307
308class RNN(RNNBase):
309
310 r"""Applies a multi-layer Elman RNN with :math:`\tanh` or :math:`\text{ReLU}` non-linearity to an
311 input sequence.
312
313
314 For each element in the input sequence, each layer computes the following function:
315
316 .. math::
317 h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh})
318
319 where :math:`h_t` is the hidden state at time `t`, :math:`x_t` is
320 the input at time `t`, and :math:`h_{(t-1)}` is the hidden state of the
321 previous layer at time `t-1` or the initial hidden state at time `0`.
322 If :attr:`nonlinearity` is ``'relu'``, then :math:`\text{ReLU}` is used instead of :math:`\tanh`.
323
324 Args:
325 input_size(:class:`int`): The number of expected features in the input `x`.
326 hidden_size(:class:`int`): The number of features in the hidden state `h`.
327 num_layers(:class:`int`): Number of recurrent layers. E.g., setting ``num_layers=2``
328 would mean stacking two RNNs together to form a `stacked RNN`,
329 with the second RNN taking in outputs of the first RNN and
330 computing the final results. Default: 1.
331 nonlinearity(:class:`str`): The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'``.
332 bias(:class:`bool`): If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`.
333 Default: ``True``.
334 batch_first(:class:`bool`): If ``True``, then the input and output tensors are provided
335 as `(batch, seq, feature)` instead of `(seq, batch, feature)`.
336 Note that this does not apply to hidden or cell states. See the
337 Inputs/Outputs sections below for details. Default: ``False``.
338 dropout(:class:`float`): If non-zero, introduces a `Dropout` layer on the outputs of each
339 RNN layer except the last layer, with dropout probability equal to
340 :attr:`dropout`. Default: 0.
341 bidirectional(:class:`bool`): If ``True``, becomes a bidirectional RNN. Default: ``False``.
342
343 Shape:
344 - Inputs: input, h_0
345 input: :math:`(L, N, H_{in})` when ``batch_first=False`` or :math:`(N, L, H_{in})`
346 when ``batch_first=True``. Containing the features of the input sequence.
347 h_0: :math:`(D * \text{num\_layers}, N, H_{out})`. Containing the initial hidden
348 state for each element in the batch. Defaults to zeros if not provided.
349
350 where:
351
352 .. math::
353 \begin{aligned}
354 N ={} & \text{batch size} \\
355 L ={} & \text{sequence length} \\
356 D ={} & 2 \text{ if bidirectional=True otherwise } 1 \\
357 H_{in} ={} & \text{input\_size} \\
358 H_{out} ={} & \text{hidden\_size}
359 \end{aligned}
360
361 - Outputs: output, h_n
362 output: :math:`(L, N, D * H_{out})` when ``batch_first=False`` or :math:`(N, L, D * H_{out})` when ``batch_first=True``.
363 Containing the output features `(h_t)` from the last layer of the RNN, for each `t`.
364 h_n: :math:`(D * \text{num\_layers}, N, H_{out})`. Containing the final hidden state for each element in the batch.
365

Callers 1

test_rnnFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_rnnFunction · 0.72