nn.LSTM()接收到无效的参数组合



我在代码中使用pytorch中的lstm来预测时间序列。当我写这个代码

class LSTM_model(nn.Module):
def __init__(self, input_size, output_size, hidden_size,num_layers,dropout):
super(LSTM_model,self).__init__()

self.input_size = input_size
self.hidden_size = hidden_size
self.seq_len = seq_len
self.num_layers = num_layers
self.dropout = dropout
self.output_size = output_size

# self.lstm=nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers)
self.lstm = nn.LSTM(self.input_size, self.hidden_size,self.num_layers , self.dropout, batch_first=True)
self.fc= nn.Linear(self.hidden_size , self.output_size)

def forward(self, x , hidden):
x, hidden= self.lstm(x,hidden)
x = self.fc(x)
return x,hidden

但当我使用该类时,我在关于PyTorch的内部nn.LSTM()函数的x,hidden=self.lstm(x,hidden)行中遇到了一个错误。

<ipython-input-63-211c1442b5a7> in forward(self, x, hidden)
15 
16     def forward(self, x , hidden):
---> 17         x, hidden= self.lstm(x,hidden)
18         x = self.fc(x)
19         return x,hidden
D:Anacondalibsite-packagestorchnnmodulesmodule.py in _call_impl(self, *input, **kwargs)
725             result = self._slow_forward(*input, **kwargs)
726         else:
--> 727             result = self.forward(*input, **kwargs)
728         for hook in itertools.chain(
729                 _global_forward_hooks.values(),
D:Anacondalibsite-packagestorchnnmodulesrnn.py in forward(self, input, hx)
232         _impl = _rnn_impls[self.mode]
233         if batch_sizes is None:
--> 234             result = _impl(input, hx, self._flat_weights, self.bias, self.num_layers,
235                            self.dropout, self.training, self.bidirectional, self.batch_first)
236         else:
TypeError: rnn_tanh() received an invalid combination of arguments - got (Tensor, Tensor, list, int, int, float, bool, bool, bool), but expected one of:
* (Tensor data, Tensor batch_sizes, Tensor hx, tuple of Tensors params, bool has_biases, int num_layers, float dropout, bool train, bool bidirectional)
didn't match because some of the arguments have invalid types: (Tensor, Tensor, !list!, !int!, !int!, !float!, !bool!, bool, bool)
* (Tensor input, Tensor hx, tuple of Tensors params, bool has_biases, int num_layers, float dropout, bool train, bool bidirectional, bool batch_first)
didn't match because some of the arguments have invalid types: (Tensor, Tensor, !list!, !int!, int, float, bool, bool, bool)

我用这条线调用了函数

model = LSTM_model(input_size=1, output_size=1, hidden_size=128, num_layers=2, dropout=0).to(device)

这里叫

从tqdm自动导入tqdm

def loop_fn(mode, dataset, dataloader, model, criterion, optimizer,device):
if mode =="train":
model.train()
elif mode =="test":
model.eval()
cost = 0
for feature, target in tqdm(dataloader, desc=mode.title()):
feature, target = feature.to(device), target.to(device)
output , hidden = model(feature,None)
loss = criterion(output,target)

if mode =="train":
loss.backward()
optimizer.step()
optimizer.zero_grad()

cost += loss.item() * feature.shape[0]
cost = cost / len(dataset)
return cost

提前感谢

我花了一段时间才发现,但由于位置参数的原因,您对nn.LSTM的初始化不正确。

self.lstm = nn.LSTM(self.input_size, self.hidden_size,
self.num_layers, self.dropout, batch_first=True)

上面将把self.dropout分配给名为bias:的参数

>>> model.lstm
LSTM(1, 128, num_layers=2, bias=0, batch_first=True)

您可能需要使用关键字参数:

self.lstm = nn.LSTM(
input_size=self.input_size, 
hidden_size=self.hidden_size, 
num_layers=self.num_layers, 
dropout=self.dropout, 
batch_first=True)

这将提供所需的结果:

>>> model.lstm
LSTM(1, 128, num_layers=2, batch_first=True)

最新更新