【问题标题】:LSTM model implementation in PyTorch [closed]PyTorch 中的 LSTM 模型实现 [关闭]
【发布时间】:2019-11-18 13:23:33
【问题描述】:

我试图在 PyTorch 中实现 CNN+LSTM 模型,但我的 LSTM 部分有问题(我以前从未使用过 LSTM)。你能写Many-to-one-LSTM模型类吗(图片链接:https://i.ibb.co/SRGWT5j/lstm.png)...

【问题讨论】:

    标签: deep-learning pytorch lstm recurrent-neural-network


    【解决方案1】:

    对于 Pytorch 中的 nn.LSTM,根据文档 https://pytorch.org/docs/stable/nn.html?highlight=lstm#torch.nn.LSTM

    它将输入作为 (embedding_size_dimension , hidden_​​size_dimension , number_of_layers) (目前忽略双向参数,也可以传入初始的hidden_​​state和cell_state)

    所以我们需要传递一个形状为 [max sentence length , batch size , embedding size ] 的张量

    只是一个示例模型

    class Model(nn.Module):
        def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5):
            super(Model, self).__init__()
            self.output_size = output_size
            self.n_layers = n_layers
            self.hidden_dim = hidden_dim
    
            self.embedding = nn.Embedding(vocab_size, embedding_dim)
            self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=drop_prob)
    
        def forward(self, sentence):
            batch_size = sentence.size(0)
            sentence = sentence.long()
            embeds = self.embedding(sentence)
            lstm_out, hidden = self.lstm(embeds)
            # so here lstm_out will be of [max sentence length , batch size , hidden size]
            # so for simple many-to-one we can just use output of last cell of LSTM
            out = lstm_out[-1,:,:]
            return out
    

    你可以参考这个链接,它在 pytorch 中很好地解释了 LSTM,它还有一个 SentimentNet 模型的示例

    https://blog.floydhub.com/long-short-term-memory-from-zero-to-hero-with-pytorch/

    【讨论】:

      猜你喜欢
      • 2022-07-17
      • 1970-01-01
      • 2020-09-18
      • 2018-10-14
      • 2021-01-06
      • 1970-01-01
      • 2019-08-19
      • 2020-09-18
      • 2018-08-10
      相关资源
      最近更新 更多