首页 文章

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

提问于
浏览
-1

我打算在每层中实现一个2层和256个单元的LSTM . 我试图理解PyTorch LSTM框架 . 我可以编辑的torch.nn.LSTM中的变量是input_size,hidden_size,num_layers,bias,batch_first,dropout和bidirectional .

但是,如何在单个图层中包含多个单元格?

1 回答

  • 0

    这些单元格将根据输入中的序列大小自动展开 . 请查看此代码:

    # 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/找到更多示例 .

相关问题