将循环层附加到具有不同隐藏大小的 PyTorch LSTM 模型

我正在使用 PyTorch 开发用于序列分析的 BI-LSTM 模型。我正在使用torch.nn.LSTM. 使用该模块,您可以拥有多个层,只需传递一个参数num_layers作为层数(例如,num_layers=2)。然而,它们都是相同的hidden_size,这对我来说部分好,我只是想让它们都一样,hidden_size 但最后一层的大小不同。基本示例如下:


rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2)

inp = torch.randn(5, 3, 10)

h0 = torch.randn(2, 3, 20)

c0 = torch.randn(2, 3, 20)

output, (hn, cn) = rnn(inp, (h0, c0))

输出昏暗为 ( 5, 3,20)


一个解决方案(但对我不利)是实现额外的模型,该模型输出我需要的维度并从第一个模型中获取输入,例如:


rnn_two = nn.LSTM(input_size=20, hidden_size=2)

output2, _ = rnn_two(output)

但是,我不想这样做,因为我使用并行化模型DataParallel,所以我需要全部成为一个包。我希望找到类似于 keras 的东西,例如:


rnn.add(LSTM, hidden_size=2)

我检查了LSTM 源代码,但找不到我需要的。


有什么建议?


动漫人物
浏览 282回答 2
2回答

开心每一天1111

如果我没记错的话,可以这样做:import torch.nn as nnimport torch.nn.functional as Fclass RnnWith2HiddenSizesModel(nn.Module):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super(RnnWith2HiddenSizesModel, self).__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2)&nbsp; &nbsp; &nbsp; &nbsp; self.rnn_two = nn.LSTM(input_size=20, hidden_size=2)&nbsp; &nbsp; def forward(self, inp, hc):&nbsp; &nbsp; &nbsp; &nbsp; output, _ = self.rnn(inp, hc)&nbsp; &nbsp; &nbsp; &nbsp; output2, _ = self.rnn_two(output)&nbsp; &nbsp; &nbsp; &nbsp; return output2inp = torch.randn(5, 3, 10)h0 = torch.randn(2, 3, 20)c0 = torch.randn(2, 3, 20)rnn = RnnWith2HiddenSizesModel()output = RnnWith2HiddenSizesModel()(inp, (h0, c0))tensor([[[-0.0305,&nbsp; 0.0327],&nbsp; &nbsp; &nbsp;[-0.1534, -0.1193],&nbsp; &nbsp; &nbsp;[-0.1393,&nbsp; 0.0474]],&nbsp; &nbsp; [[-0.0370,&nbsp; 0.0519],&nbsp; &nbsp; &nbsp;[-0.2081, -0.0693],&nbsp; &nbsp; &nbsp;[-0.1809,&nbsp; 0.0826]],&nbsp; &nbsp; [[-0.0561,&nbsp; 0.0731],&nbsp; &nbsp; &nbsp;[-0.2307, -0.0229],&nbsp; &nbsp; &nbsp;[-0.1780,&nbsp; 0.0901]],&nbsp; &nbsp; [[-0.0612,&nbsp; 0.0891],&nbsp; &nbsp; &nbsp;[-0.2378,&nbsp; 0.0164],&nbsp; &nbsp; &nbsp;[-0.1760,&nbsp; 0.0929]],&nbsp; &nbsp; [[-0.0660,&nbsp; 0.1023],&nbsp; &nbsp; &nbsp;[-0.2176,&nbsp; 0.0508],&nbsp; &nbsp; &nbsp;[-0.1611,&nbsp; 0.1053]]], grad_fn=<CatBackward>)

桃花长相依

尽管@Mikhail Berlinkov 的回答按需要工作这一事实,但它并没有推广到一般情况(甚至在问题中都没有要求),为此我想提出第二个解决方案:import torchimport torch.nn as nnimport torch.nn.functional as Ffrom functools import reduceclass RNNModel(nn.Module):&nbsp; &nbsp; def __init__(self, *models):&nbsp; &nbsp; &nbsp; &nbsp; super(RNNModel, self).__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.models = models&nbsp; &nbsp; def forward(self, inp):&nbsp; &nbsp; &nbsp; &nbsp; return reduce(lambda arg, model: model(arg, None)[0], self.models, inp)并且可以称为:rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2)rnn_two = nn.LSTM(input_size=20, hidden_size=2)inp = torch.randn(5, 3, 10)rnn_model = RNNModel(rnn, rnn_two)output = rnn_model(inp)output.shape等于预期(即,5,3,2)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python