SantoshGupta7
SantoshGupta7

Reputation: 6197

Pytorch nn.CrossEntropyLoss giving, ValueError: Expected target size (x, y), got torch.Size([x, z]) for 3d tensor

I am following the example here, where the documentation says:

Input: (N, C) where C = number of classes

Target: (N) where each value is 0 ≤ targets[i] ≤ C−1

And this is the case with the example given for a 2d tensor

loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()

But for a 2d tensor, I am getting an error

import torch.nn as nn
import torch
loss = nn.CrossEntropyLoss(ignore_index=0)

inputs = torch.rand(32, 128, 3)
targets = torch.ones(32, 128)

loss(inputs, targets.long())
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-61e7f03039a6> in <module>
      7 targets = torch.ones(32, 128)
      8 
----> 9 loss(inputs, targets.long())

/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/opt/conda/lib/python3.8/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    959 
    960     def forward(self, input: Tensor, target: Tensor) -> Tensor:
--> 961         return F.cross_entropy(input, target, weight=self.weight,
    962                                ignore_index=self.ignore_index, reduction=self.reduction)
    963 

/opt/conda/lib/python3.8/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2466     if size_average is not None or reduce is not None:
   2467         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2468     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2469 
   2470 

/opt/conda/lib/python3.8/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2271         out_size = (n,) + input.size()[2:]
   2272         if target.size()[1:] != input.size()[2:]:
-> 2273             raise ValueError('Expected target size {}, got {}'.format(
   2274                 out_size, target.size()))
   2275         input = input.contiguous()

ValueError: Expected target size (32, 3), got torch.Size([32, 128])

As far as I can tell, I am doing everything right regarding setting up the dimensions. The error message seems to think that I am giving a 2d vector, but I gave it a 3d vector, the 128 size dimension is missing.

Is there something that I didn't set up correctly for this loss function?

Upvotes: 2

Views: 4181

Answers (1)

Berriel
Berriel

Reputation: 13611

This is what the documentation says about K-dimensional loss:

Can also be used for higher dimension inputs, such as 2D images, by providing an input of size (minibatch, C, d_1, d_2, ..., d_K) with K ≥ 1 , where K is the number of dimensions, and a target of appropriate shape (see below).

The correct input should have a (32, 3, 128) shape, if you have 3 classes:

import torch.nn as nn
import torch
loss = nn.CrossEntropyLoss(ignore_index=0)

inputs = torch.rand(32, 3, 128)
targets = torch.ones(32, 128)

loss(inputs, targets.long())

Or the target should have a (32, 3) shape, if you have 128 classes:

inputs = torch.rand(32, 128, 3)
targets = torch.ones(32, 3)

Upvotes: 3

Related Questions