# Will Trainer loss functions automatically ignore -100?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwill-trainer-loss-functions-automatically-ignore-100%2F36134%3E%5C
**Category:** 🤗Transformers\
**Created:** [April 10, 2023, 1:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwill-trainer-loss-functions-automatically-ignore-100%2F36134 "2023-04-10T13:01:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PolarisRisingWar](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/polarisrisingwar/32/27355_2.png) [@PolarisRisingWar](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FPolarisRisingWar)%5C
**Post date:** [April 10, 2023, 1:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwill-trainer-loss-functions-automatically-ignore-100%2F36134%2F1 "2023-04-10T13:01:43Z")

</div>

I’m learning the tutorial [Token classification](https://huggingface.co/docs/transformers/tasks/token_classification).  
And I have a question:  
In preprocessing part, it converted the label of special tokens and the tokens that are not the first token in the word to -100.  
It said that this will show its usefulness in loss function, but the code hasn’t use something like `ignore_index` in loss function. The -100 seems only work in metric evaluation.  
So I wonder if loss function in Trainer will automatically ignore -100?  
Or it works in num\_labels, id2label and label2id parameter? Does it mean that I set these parameters, other labels will be ignored?

---

<div class="post-metadata">

**Author:** ![mdrpanwar](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@mdrpanwar](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmdrpanwar)%5C
**Post date:** [June 29, 2023, 10:54am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwill-trainer-loss-functions-automatically-ignore-100%2F36134%2F2 "2023-06-29T10:54:01Z")

</div>

I have the same question for `GPT2LMHeadModel`.

Edit: `-100` is the default `ignore_index` in [PyTorch’s CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html). So, any token with a label of `-100` will be ignored in loss computation.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [May 25, 2024, 6:17am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwill-trainer-loss-functions-automatically-ignore-100%2F36134%2F3 "2024-05-25T06:17:14Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
