I know this topic was previously discussed, however, the proposed solutions didn’t work for me.
I am trying to perform classification of precomputed features into 7 categories using logistic regression.
I got the following error when training the classifier:
ValueError: Expected target size (32, 7), got torch.Size()
My target shape was ([768,1]) and squeezing it didn’t solve the problem.
The input shape also is
torch.Size([768, 1, 221])
By squeezing it, I got this error:
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'
To train the logistic regression model, I used this piece of code which works steadily with another dataset:
#define classifier num_input = trainingData.shape[-1] num_classes = trainingLabels.cpu().unique().numel() model = Sequential(Linear(num_input, num_classes), LogSoftmax(dim=1)) optimizer = Adam(model.parameters()) criterion = NLLLoss() batch_size = 32 num_epochs = 50 #learning rate lr = 1e-4 nsamples = trainingData.shape nbatches = nsamples // batch_size for e in range(num_epochs): perm = torch.randperm(nsamples) for i in range(nbatches): idx = perm[i * batch_size : (i+1) * batch_size] model.zero_grad() resp = model.forward(trainingData[idx]) trainingLabels = trainingLabels.squeeze() loss = criterion(resp, trainingLabels[idx]) loss.backward() optimizer.step() resp = model.forward(trainingData) avg_loss = criterion(resp, trainingLabels)
Obviously, my problem is in the data shape but I can not fix it may be because I am new to pytorch.
Any help will be appreciated.