简体   繁体   English

nn.LSTM() 收到 arguments 的无效组合

[英]nn.LSTM() received an invalid combination of arguments

I use lstm from pytorch in my code to predict time series.我在我的代码中使用来自 pytorch 的 lstm 来预测时间序列。 while i write this code当我写这段代码时

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

but when I use the class, I get an error with the x,hidden=self.lstm(x,hidden) line about the internal nn.LSTM() function from PyTorch.但是当我使用 class 时,我收到关于来自 Z95B88F180E9EB5678E0F9EBACAC 的内部nn.LSTM() function 的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:\Anaconda\lib\site-packages\torch\nn\modules\module.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:\Anaconda\lib\site-packages\torch\nn\modules\rnn.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)

i called the function with this line我用这条线调用了 function

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

and its called here它在这里被称为

from tqdm.auto import tqdm从 tqdm.auto 导入 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

Thank You in advance先感谢您

I took me a while to find out, but you are initializing your nn.LSTM incorrectly because of positional arguments.我花了一段时间才发现,但是由于位置 arguments,您正在错误地初始化您的nn.LSTM

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

The above will assign self.dropout to the argument named bias :以上将self.dropout分配给名为bias的参数:

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

You may want to use keyword arguments instead:您可能想使用关键字 arguments 代替:

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)

Which will provide the desired result:这将提供所需的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM