RNNCell¶
- class RNNCell(input_size, hidden_size, bias=True, nonlinearity='tanh')[源代码]¶
一个使用 tanh 或 ReLU 非线性的 Elman RNN 单元
\[h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh})\]如果
nonlinearity
是 ‘relu’ ,那么会使用 ReLU 代替 tanh。- 参数
- 形状:
- 输入:input,hidden
input: (batch, input_size). Tensor containing input features. hidden: (batch, hidden_size). Tensor containing the initial hidden state for each element in the batch. Defaults to zero if not provided.
- 输出: h’
h’: (batch, hidden_size). Tensor containing the next hidden state for each element in the batch.
实际案例
import numpy as np import megengine as mge import megengine.module as M m = M.RNNCell(10, 20) inp = mge.tensor(np.random.randn(3, 10), dtype=np.float32) hx = mge.tensor(np.random.randn(3, 20), dtype=np.float32) out = m(inp, hx) print(out.numpy().shape)
输出:
(3, 20)