NLL loss with K dimensional input

I am trying to rewrite following python sample to C++ (use of nll loss)

>>> # 2D loss example (used, for example, with image inputs)
>>> N, C = 5, 4
>>> loss = nn.NLLLoss()
>>> # input is of size N x C x height x width
>>> data = torch.randn(N, 16, 10, 10)
>>> conv = nn.Conv2d(16, C, (3, 3))
>>> m = nn.LogSoftmax(dim=1)
>>> # each element in target has to have 0 <= value < C
>>> target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C)
>>> output = loss(m(conv(data)), target)
>>> output.backward()

I have the following code

long long N = 5;
long long C = 4;

auto data = torch::randn({ N, 16, 10, 10 });
auto conv = torch::nn::Conv2d(torch::nn::Conv2dOptions(16, C, 3));
auto target = torch::empty({ N, 8, 8 }, torch::kLong).random_(0, C);

data = conv->forward(data);
data = torch::log_softmax(data, 1);
auto output = torch::nll_loss(data, target);

It crashes with error: multi-target not supported at …\aten\src\THNN/generic/ClassNLLCriterion.c:20
Any ideas ? Thanks

The code and shapes of your output and target are generally alright.
However, since you are trying to calculate a loss using spatial inputs, you would have to use torch::nll_loss2d as the loss function.

Thanks for the reply!

However, I am actually working with 3D data (I don’t see a nll_loss3d function), so I will have to find a workaround.

Hi Matej,

check out the Python implementation of F.nll_loss for inspiration.
Of course, if you moved those bits (and even the dispatch to 2d potentially) into nll_loss on the C++ side in PyTorch itself, you could clean up your code. :wink:

Best regards