我想创建包含不同隐藏层的LSTM层来预测时间序列数据,因为LSTM_1的第一层包含10个隐藏层,LSTM_2包含1个隐藏层,下面将说明所提出的神经网络体系结构
proposed neural network architecture
def __init__(self, nb_features=1, hidden_size_1=100, hidden_size_2=100, nb_layers_1 =10, nb_layers_2 = 1, dropout=0.5): #(self, nb_features=1, hidden_size=100, nb_layers=10, dropout=0.5):
super(Sequence, self).__init__()
self.nb_features = nb_features
self.hidden_size_1 = hidden_size_1
self.hidden_size_2 = hidden_size_2
self.nb_layers_1 =nb_layers_1
self.nb_layers_2 = nb_layers_2
self.lstm_1 = nn.LSTM(self.nb_features, self.hidden_size_1, self.nb_layers_1, dropout=dropout)
self.lstm_2 = nn.LSTM(self.hidden_size_1, self.hidden_size_2, self.nb_layers_2, dropout=dropout)
self.lin = nn.Linear(self.hidden_size_2, 1)
def forward(self, input):
h_t1 = Variable(torch.zeros(self.nb_layers_1, input.size()[1], self.hidden_size_1))
c_t1 = Variable(torch.zeros(self.nb_layers_1, input.size()[1], self.hidden_size_1))
h_t2 = Variable(torch.zeros(self.nb_layers_2, input.size()[1], self.hidden_size_2))
c_t2 = Variable(torch.zeros(self.nb_layers_2, input.size()[1], self.hidden_size_2))
outputs = []
for i, input_t in enumerate(input.chunk(input.size(1))):
h_t1, c_t1 = self.lstm_1(input_t, (h_t1, c_t1))
h_t2, _ = self.lstm_2(h_t1, (h_t2, c_t2))
output = self.lin(h_t2[-1])
outputs += [output]
outputs = torch.stack(outputs, 1).squeeze(2)
return outputs
但是,上面的代码“ RuntimeError:期望隐藏的[0]大小(3,60,100),得到(1,60,100)”出现运行时错误。上面的代码是否正确,能否请您提出一些建议?非常感谢