1. LSTM
-
长短期记忆网络,主要用于做序列建模用
-
原理
-
主要参数
- batch_first:多gpu训练时要设置为True
- bidirectional: True表示双向
- input_size: 序列的特征维度
- hidden_size:隐含层的特征维度
-
使用
>>> rnn = nn.LSTM(10, 20, 2)
>>> input = torch.randn(5, 3, 10) # seq, batch, features
>>> h0 = torch.randn(2, 3, 20)
>>> c0 = torch.randn(2, 3, 20)
>>> output, (hn, cn) = rnn(input, (h0, c0))
- 说明
- 一般在使用的时候都是省略了h,c。h是t时刻的隐藏状态,c是t时刻的单元格状态
- 注意: 多GPU训练的时候,需要将batch放到最前面
2. GRU
-
同样也是主要用于序列建模
-
原理
-
主要参数
- 主要就是初始化和输入时
-
使用
>>> rnn = nn.GRU(10, 20, 2)
>>> input = torch.randn(5, 3, 10)
>>> h0 = torch.randn(2, 3, 20)
>>> output, hn = rnn(input, h0)
- 注意
- h0 的特征shape是固定的(num_layers*num_directions, batch, hidden_size)