Hey there,
I have a similar issue and cannot find the solution:
# tested with python 3.7.5
from torch.utils.data import Dataset
import ast
import torch
import torch.nn as nn
from torch.autograd import Variable
# Links:
# https://www.kaggle.com/danieldagnino/training-a-classifier-with-pytorch
class my_model(nn.Module):
def __init__(self,n_in=60, n_hidden=10, n_out=60):
super(my_model,self).__init__()
self.n_in = n_in
self.n_out = n_out
self.linearlinear = nn.Sequential(
nn.Linear(self.n_in, self.n_out, bias=True), # Hidden layer.
)
self.logprob = nn.LogSoftmax(dim=1) # -Log(Softmax probability).
def forward(self,x):
x = self.linearlinear(x)
print("In forward:", x.shape)
x = self.logprob(x)
print("In forward:", x.shape)
return x
class TESNamesDataset(Dataset):
def __init__(self, data_root):
self.samples = []
with open(data_root,"r") as f:
self.samples = [ [ast.literal_eval(ast.literal_eval(elem)[0]), ast.literal_eval(ast.literal_eval(elem)[1])] for elem in f.read().split('\n') if elem]
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
# Function that returns one input and one output (label)
return torch.Tensor(self.samples[idx][0]), torch.Tensor(self.samples[idx][1])
if __name__ == '__main__':
from torch.utils.data import DataLoader
my_data = TESNamesDataset('first_move.txt')
my_loader = DataLoader(my_data, batch_size=1, num_workers=0)
model = my_model()
criterium = nn.NLLLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.1,weight_decay=1e-4)
# Taining.
for k, (data, target) in enumerate(my_loader):
print("\nEpoch:", k)
print(data)
print(target)
#print(data.view(-1).shape) # 1x60
data = Variable(data, requires_grad=False) # input
target = Variable(target.long(), requires_grad=False) # output
# Set gradient to 0.
optimizer.zero_grad()
# Feed forward.
pred = model(data)
print("Pred.shape:", pred.shape)
print("target.shape:", target.shape)
print(target.view(-1).shape)
#print(eee)
loss = criterium(pred, target.view(-1))
# Gradient calculation.
loss.backward()
# Print loss every 10 iterations.
if k%10==0:
print('Loss {:.4f} at iter {:d}'.format(loss.item(),k))
# Model weight modification based on the optimizer.
optimizer.step()
Output:
Epoch: 0
tensor([[0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 1., 0.,
0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1.,
1., 0., 1., 0., 1., 0.]])
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.]])
In forward: torch.Size([1, 60])
In forward: torch.Size([1, 60])
Pred.shape: torch.Size([1, 60])
target.shape: torch.Size([1, 60])
torch.Size([60])
Any Idea why I get this error:
ValueError: Expected input batch_size (1) to match target batch_size (60).
Solution:
I found a solution, my labels were binary 60x1 which is also how I wanted them to be! But with this I always got the above error. For this reason I made the binary to an int and then it worked. I do not know however if I still need the 60 outputs now, actually I just need one number (showing the binary index).