makhan:

My question here is, clearly the output shape of the model is different than what is being passed to the target and it still trains (!) so how is that possible? shouldn’t I have to convert the targets to one-hot encoding format for the loss computation to work?
To be specific, the target is just 1 integer while the softmax layer is expected to get a one-hot encoded version of the target integer. Is pytorch internally managing this or am I missing something?


So the answer to your questions are 1. No. Torch internally converts indexes to one-hot encoded vectors so you don’t have to. 2. Yes. For example:
loss = NLLLoss()
y = torch.LongTensor([2, 1])
y_pred = torch.Tensor([[0.1, 0, 0.9], [0.0, 0.5, 0.5]])
print(loss(torch.log(y_pred), y))

y and y_pred does not have the same dimensions, but torch gets it. This is very convenient for single-class classification problems. Your problem is a single-class classification problem.