GRU的一个单元
- torch.nn.GRUCell(
- input_size,
- hidden_size,
- bias=True,
- device=None,
- dtype=None)
输入:(batch,input_size)
输出和隐藏层:(batch,hidden_size)
- import torch.nn as nn
-
- rnn = nn.GRUCell(input_size=5,hidden_size=10)
-
- input_x = torch.randn(3, 5)
- #batch,input_size
-
- h0 = torch.randn(3, 10)
- #batch,hidden_size
-
- output= rnn(input_x, h0)
- output.shape, output
- '''
- (torch.Size([3, 10]),
- tensor([[-0.4414, 1.0060, 0.3346, -0.2446, -0.4170, -0.6201, -1.0049, 0.1765,
- 0.2238, -2.0249],
- [ 0.2764, 0.6327, 0.1682, -0.0433, 1.2226, -1.0959, 0.0345, -0.6375,
- -1.4599, -0.3670],
- [ 0.9447, -0.0849, 0.3983, -0.4078, 0.9805, -0.1826, 0.2151, 0.3382,
- -0.1147, -0.2307]], grad_fn=
)) - '''
功能性 |
|
输入: |
|
输出: |
|
用法: |
|
应用场景: |
|
一个具有 seq_len
,bidirectional=True
和指定的 num_layers
的 GRU 对应的 GRUCell 的数量为:
seq_len:对于长度为 seq_len
的输入序列,GRU 在内部会进行 seq_len
次循环操作,每次循环处理序列中的一个时间步长。所以这部分会贡献 seq_len
个 GRUCell。
bidirectional=True:当 GRU 是双向的,即 bidirectional=True
,那么对于每一个时间步长,都会有两个 GRUCell 被调用:一个是正向的,另一个是反向的。因此,双向性将 GRUCell 的数量增加一倍。
num_layers:这表示你要堆叠多少层的 GRU。每一层都会为每个时间步调用其自己的 GRUCell(考虑到双向性,这可能是两个)。所以如果你有 num_layers
层,那么你需要乘以这个数字。
综上所述,总的 GRUCell 的数量为: Total GRUCells=seq_len×(2 if bidirectional else 1)×num_layers