LSTM in PyTorch II

单向lstm的使用

rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2) 
input = torch.randn(5, 3, 10) #(seq_len, batch, input_size)
h0 = torch.randn(2, 3, 20)    #(num_layers,batch,output_size)
c0 = torch.randn(2, 3, 20)    #(num_layers,batch,output_size)
output, (hn, cn) = rnn(input, (h0, c0))

output.shape #(seq_len, batch, output_size)
torch.Size([5, 3, 20])

hn.shape #(num_layers, batch, output_size)
torch.Size([2, 3, 20])

cn.shape #(num_layers, batch, output_size)
torch.Size([2, 3, 20])

双向lstm的使用

其实就是反向加了一层lstm,然后输出的时候再concat起来,所以维度上会*2

#bidirectional – If True, becomes a bidirectional LSTM. Default: False 
rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2,bidirectional=True)

input = torch.randn(5, 3, 10) #(seq_len, batch, input_size)
h0 = torch.randn(4, 3, 20)    #(num_layers,batch,output_size)
c0 = torch.randn(4, 3, 20)

猜你喜欢

转载自blog.csdn.net/tony2278/article/details/105161408