Reputation: 68
I'm facing error in LSTM input dimensions with following model code:
class Model(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_keys):
super(Model, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_keys)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out
Input sequence shape: 2048, 10,1 -> (#batch, window_size, #input)
Input label shape: 2048
Following code I have for training model:
for epoch in range(num_epochs): # Loop over the dataset multiple times
train_loss = 0
for step, (seq, label) in enumerate(dataloader):
# Forward pass
seq = seq.clone().detach().view(-1, window_size, input_size).to(device)
output = model(seq)
loss = criterion(output, label.to(device))
print('step: ',step,'sequence: ', seq.shape, 'Label: ', label.shape, 'model output: ', output.shape)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
train_loss += loss.item()
optimizer.step()
writer.add_graph(model, seq)
I get following error:
RuntimeError: input must have 3 dimensions, got 2
Can anyone tell what is the problem should I fixed, I used seq.unsequeeze(-1)
but its not working.
Upvotes: 0
Views: 255
Reputation: 68
I got it, the input data containing -1
values. I have used map function to convert negative integers into positive as following:
line = tuple(map(int, line.strip().split()))
Please make it sure for classification your data should contain positive number!
Upvotes: 1