The shape error is thrown, as for a multi-class classification nn.CrossEntropyLoss expects the target to be a LongTensor in the shape [batch_size] containing the class indices.
I assume you passed labels as a [batch_size, 1] tensor, which will raise this error.
Using labels.squeeze(1) should work.



 rai24:

can you please provide me solution for decreasing the training time


The training time depends highly on your model, machine, libs etc.
I would recommend to use the latest PyTorch version as well as updated libraries (CUDA, cudnn etc.).
Also, make sure to use multiple workers in your DataLoader (via num_workers).