Negative label in CrossEntropyLoss

Hi yzx!

From the documentation for CrossEntropyLoss:

ignore_index=- 100

and

ignore_index (int, optional) – Specifies a target value that is ignored
and does not contribute to the input gradient. When size_average
is True, the loss is averaged over non-ignored targets. Note that
ignore_index is only applicable when the target contains class indices.

That is, -100 is the “magic value” that ignore_index is set to by default, so
those targets are ignored (including the fact that the label would otherwise
be an invalid negative value).

Best.

K. Frank

1 Like