deep-learning - PyTorch 中的 LSTM 模型实现
问题描述
我试图在 PyTorch 中实现 CNN+LSTM 模型,但我对 LSTM 部分有疑问(我以前从未使用过 LSTM)。您能否编写多对一 LSTM 模型类(图片链接:https ://i.ibb.co/SRGWT5j/lstm.png )...
解决方案
对于 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/
推荐阅读
- javascript - 在 Jest 中添加测试用例如何降低代码覆盖率?
- bash - 在本地使用函数来检查空白字符串
- string - 如何检查字符串是否只包含字母、数字、下划线和句点。颤振/飞镖
- mysql - 从 MySQL 中的 JSON 数组中删除
- arrays - 使两个数组等效的最佳方法
- ruby-on-rails - 解析时.yaml文件语法错误找不到问题
- javascript - 如何使提交表单时创建的div在重新提交表单时保持原位
- python - 在 Instagram 中被阻止后如何发送请求
- xamarin.android - Android 启动器“OnDestroy”被调用两次
- python - ImportError 没有名为 pygal 的模块一直显示