Error when calculating loss using ignore_index=0

I maybe naively thought that I can specify the “ignore_index” parameter of the loss function (I use orch.nn.NLLLoss(weight=None, size_average=True, ignore_index=0, reduce=True)) to 0, but it seems that I get this error:

“RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes’ failed. at /opt/conda/conda-bld/pytorch_1518243271935/work/torch/lib/THNN/generic/ClassNLLCriterion.c:87”

because my target indices for the actual classes consequently go from 1 to k. It seems that the code for the loss function seems to expect indices 0 to k-1, no matter what the ignore_index, but it also does not complain about the value of ignore_index not being less than 0!

How is this supposed to work, does the ignore index always have to be negative and does the maximum index always have to be k-1 as that error message seems to indicate?

I think it’s okay for ignore index to be positive. However, if your output tensor has k channels, then the loss function expects indices from 0 to k-1 (because it will use these to index into the output tensor).