如何在Pytorch中实现带有多个单元的LSTM层?

问题描述:

我打算在每层中实现2层和256个单元的LSTM。我正在尝试了解PyTorch LSTM框架。我可以编辑的torch.nn.LSTM中的变量是input_size,hidden_​​size,num_layers,bias,batch_first,dropout和双向。如何在Pytorch中实现带有多个单元的LSTM层?

但是,如何在单个图层中有多个单元?

这些单元格将根据您在输入中的序列大小自动展开。请看看这个代码:

# One cell RNN input_dim (4) -> output_dim (2). sequence: 5, batch 3 
# 3 batches 'hello', 'eolll', 'lleel' 
# rank = (3, 5, 4) 
inputs = Variable(torch.Tensor([[h, e, l, l, o], 
           [e, o, l, l, l], 
           [l, l, e, e, l]])) 
print("input size", inputs.size()) # input size torch.Size([3, 5, 4]) 

# Propagate input through RNN 
# Input: (batch, seq_len, input_size) when batch_first=True 
# B x S x I 
out, hidden = cell(inputs, hidden) 
print("out size", out.size()) # out size torch.Size([3, 5, 2]) 

您可以在https://github.com/hunkim/PyTorchZeroToAll/找到更多的例子。