RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [4]
How to get around the following error with nn.CrossEntrophyLoss() ?
Note: I tried using nn.BCELoss(), but it resulted in different error: ValueError: Using a target size (torch.Size([4])) that is different to the input size (torch.Size([4, 3, 32, 32])) is deprecated. Please ensure they have the same size.


The error message is pretty clear: you are using a one-dimensional target tensor while your output prediction has spatial dimensions (a three-channel map).
When using nn.CrossEntropyLoss, your target must be dense (each element is a label id): something with a shape of (batch_size,), where each element in the target belongs to [0, num_classes[. While the output consists of logits: (batch_size, num_classes,), i.e. each class from each batch is assigned a value (this is not yet a probability distribution). In the spatial setting, you will have two additional dimensions (height and width), this is the case for dense predictions such as semantic segmentation. This will make your target have a shape of (batch_size, height, width), and your output prediction (batch_size, num_classes, height, width).
