The loss functions for classification, e.g. nn.CrossEntropyLoss or nn.NLLLoss, require your target to store the class indices instead of a one-hot encoded tensor.
So if your target looks like:
labels = torch.tensor([[0, 1, 0],
                       [1, 0, 0],
                       [0, 0, 1]])

you would have to get the corresponding indices by:
labels = labels.argmax(1)
print(labels)
> tensor([1, 0, 2])

Now you can use this target tensor for your criterion.
criterion = nn.CrossEntropyLoss()
x = torch.randn(3, 3, requires_grad=True)

loss = criterion(x, labels)
loss.backward()

Your code example calculates just the most probably class and stores its index in predicted.
This is done to calculate the accuracy or just get the most likely class for the current sample.