I use lstm from pytorch in my code to predict time series. 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.
<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
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
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.
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
:
>>> model.lstm
LSTM(1, 128, num_layers=2, bias=0, batch_first=True)
You may want to use keyword arguments instead:
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)