The error is raised in nn.CrossEntropyLoss or nn.NLLLoss, if an invalid target shape is provided.
For a multi-class classification these loss functions expect a model output in the shape [batch_size, nb_classes] and a target in the shape [batch_size] containing values in the range [0, nb_classes-1].
Based on the error I guess your target has more dimensions, so you would need to remove them (e.g. via squeeze).