RNNCell
- classtorch.nn.RNNCell(input_size, hidden_size, bias=True, nonlinearity='tanh', device=None, dtype=None)[源代码]
-
带有 tanh 或 ReLU 激活函数的 Elman RNN 单元。
$h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh})$如果
nonlinearity
的值为 ‘relu’,则使用 ReLU 替代 tanh。- 参数
- 输入类型:input, hidden
-
-
input: 输入特征的张量
-
hidden: 表示初始隐藏状态的张量,默认值为零。
-
- 输出: h'
-
-
h' 形状为 (batch, hidden_size): 包含批次中每个元素下一个隐藏状态的张量
-
- 形状:
-
-
输入: $(N, H_{in})$ 或 $(H_{in})$ 张量,包含输入特征。其中 $H_{in}$ 等于 input_size。
-
hidden: $(N, H_{out})$ 或 $(H_{out})$ 张量,包含初始隐藏状态。其中 $H_{out}$ 等于 hidden_size。如果没有提供,默认值为零。
-
输出: $(N, H_{out})$ 或 $(H_{out})$ 形式的张量,包含下一个隐藏状态。
-
- 变量
-
-
weight_ih (torch.Tensor) – 输入到隐藏层的可学习权重,其形状为(hidden_size, input_size)
-
weight_hh (torch.Tensor) – 隐藏层到隐藏层的可学习权重,形状为(hidden_size, hidden_size)
-
bias_ih — 输入到隐藏层的可学习偏置项,其形状为(hidden_size)
-
bias_hh — 隐藏层到隐藏层的可学习偏置项,其形状为 (hidden_size)
-
注意
所有的权重和偏置都从$\mathcal{U}(-\sqrt{k}, \sqrt{k})$ 初始化,其中 $k = \frac{1}{\text{hidden\_size}}$。
示例:
>>> rnn = nn.RNNCell(10, 20) >>> input = torch.randn(6, 3, 10) >>> hx = torch.randn(3, 20) >>> output = [] >>> for i in range(6): ... hx = rnn(input[i], hx) ... output.append(hx)