# What is loss function for T5

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%3E%5C
**Category:** Models\
**Created:** [November 13, 2021, 8:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669 "2021-11-13T20:19:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ahmad/32/13322_2.png) [@Ahmad](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAhmad)%5C
**Post date:** [November 13, 2021, 8:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F1 "2021-11-13T20:19:50Z")

</div>

Could you please guide me how loss function for T5 is computed? I mean it is a seq2seq model. Suppose it must map a sequence of X tokens to Y tokens, but it generates Z tokens. How Y and Z are compared to calculate loss?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [November 15, 2021, 8:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F2 "2021-11-15T08:31:22Z")

</div>

T5 uses the regular cross-entropy loss (as any language model).

Suppose that you are fine-tuning T5 for translation, and you have the following training example:

```auto
* source sentence: "hello how are you"
* target sentence: "salut comment ça-va"

```

First, one needs to tokenize the sentences for the model using `T5Tokenizer`. Assuming that every word is tokenized into a single token, and we also add T5’s special token (namely \</s\> - which indicates the end of a sequence), we provide the follow inputs to the model:

```auto
* input tokens = [hello, how, are, you, </s>]
* label tokens = [salut, comment, ça, -, va, </s>]

```

Of course, we don’t provide these tokens as text to the model, but rather as integer IDs, which refer to row indices in an embedding matrix, so the actual inputs will look like:

```auto
* input_ids = [21820, 149, 33, 25, 1]
* labels = [20239, 1670, 3664, 18, 900, 1]

```

In that case, you first provide the `input_ids` to T5’s encoder, which will turn it into a tensor of shape `(batch_size, seq_len, hidden_size)`. Next, T5’s decoder will predict, for each token of the target sequence, the correct next token. This happens as follows:

```auto
      salut comment ça - va </s> => label tokens

      20239 1670 3664 18 900 1 => labels

----------------------------------------------------------------------------------------------                   
                                 DECODER 
----------------------------------------------------------------------------------------------   

         0 20239 1670 3664 18 900 => decoder_input_ids                         

decoder_start_token salut comment ça - va => decoder input tokens

```

In other words, what happens is, we prepend the decoder inputs with a special token (the decoder start token - which for T5 is the padding token, with index 0), and then the decoder needs to predict (in parallel) that:

- the token that follows the decoder start token is “salut”. Here, we compute the cross-entropy loss between the prediction of the model and the target token (which is “salut”).
- the token that follows “salut” is “comment”. Here, we compute the cross-entropy loss between the prediction of the model and the target token (which is “comment”).
- the token that follows “comment” is “ça”. Here, we compute the cross-entropy loss between the prediction of the model and the target token (which is “ça”).
- etc.
- the token that follows “va” is “\</s\>” (meaning, the end-of-sequence or EOS token). Here, we compute the cross-entropy loss between the prediction of the model and the target token (which is “\</s\>”).

In the [code](https://github.com/huggingface/transformers/blob/29dfb2dbb10cdba6327ff287db56b182c1db29b1/src/transformers/models/t5/modeling_t5.py#L1641), this is done in one go, namely by comparing the logits of the model - which are of shape (batch\_size, seq\_len, vocab\_size) - to the ground truth labels:

`loss = loss_fct(lm_logits.view(-1, lm_logits.size(-1)), labels.view(-1))`

---

<div class="post-metadata">

**Author:** ![Ahmad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ahmad/32/13322_2.png) [@Ahmad](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAhmad)%5C
**Post date:** [November 15, 2021, 9:43am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F3 "2021-11-15T09:43:48Z")

</div>

Thank you very much!

I read that for evaluation of sequences like machine translation, BLEU and ROUGE are used. Can’t this metric be used here too?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [November 15, 2021, 5:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F4 "2021-11-15T17:56:53Z")

</div>

Yes, you can use it here too.

You can leverage HuggingFace Datasets for this.

---

<div class="post-metadata">

**Author:** ![Ahmad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ahmad/32/13322_2.png) [@Ahmad](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAhmad)%5C
**Post date:** [November 15, 2021, 6:24pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F5 "2021-11-15T18:24:42Z")

</div>

Thanks,  
Also, I have another minor question about T5 which could be obvious!  
Does the embedding of words change during supervised fine-tuning of the model? I mean is it the change in embedding of words what the model learns from examples, or the learning is stored in another parts of the model?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [November 16, 2021, 3:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F6 "2021-11-16T15:06:29Z")

</div>

Hi,

Yes, all parameters of the model can be slightly updated when fine-tuning the model. The parameters include the token embeddings, but also the weights of the self-attention layers, the language modeling head, etc.

---

<div class="post-metadata">

**Author:** ![codehugger](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@codehugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fcodehugger)%5C
**Post date:** [May 7, 2023, 2:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F7 "2023-05-07T02:52:56Z")

</div>

Naive doubt ? 🙂 for task of fine tuning for text summarization, does same cross-entropy loss be used by default by huggingface seq2seq trainer on t5 versions ( small, base …) ?  
Thank you

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [May 7, 2023, 5:23pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F8 "2023-05-07T17:23:08Z")

</div>

Yes, T5 uses the cross-entropy loss by default for language modeling as seen [here](https://github.com/huggingface/transformers/blob/ef42c2c487260c2a0111fa9d17f2507d84ddedea/src/transformers/models/t5/modeling_t5.py#L1748)

---

<div class="post-metadata">

**Author:** ![codehugger](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@codehugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fcodehugger)%5C
**Post date:** [May 8, 2023, 6:10am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F9 "2023-05-08T06:10:27Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![mepster](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mepster](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmepster)%5C
**Post date:** [November 14, 2023, 7:46pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F10 "2023-11-14T19:46:08Z")

</div>

What if the prediction adds an extra word at the beginning, like:

```auto
Foo salut comment ça-va

```

Does that mean the prediction is just entirely wrong? No partial credit for getting the sentence correct, but off-by-one?

Similarly, often there is more than one correct/reasonable translation, with a slightly different number of words. No partial credit?

If so, seems like a lot of potentially useful information is being wasted.

Even getting some of the single-word translations right could be partial credit.

---

<div class="post-metadata">

**Author:** ![Chhabi](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@Chhabi](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FChhabi)%5C
**Post date:** [November 18, 2023, 9:11am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F11 "2023-11-18T09:11:42Z")

</div>

In the context of sequence-to-sequence tasks like translation, where the model generates an entire sequence of tokens, evaluation can indeed be sensitive to the alignment of tokens between the predicted and reference sequences. This issue can arise when there are variations in the length or structure of the generated sequence compared to the reference sequence.

Here are some considerations:

1. **Exact Match Evaluation:** If the evaluation metric considers only exact matches, then even a small deviation, such as an extra word at the beginning or a missing word, could result in the entire prediction being marked as incorrect. This approach is strict and does not provide partial credit for partially correct predictions.
2. **Token-Level Metrics:** Some evaluation metrics take into account individual tokens and provide partial credit for correct tokens, even if the overall sequence is not an exact match. This is more forgiving and acknowledges that a model may produce a mostly correct translation with some deviations.
3. **BLEU and Similar Metrics:** Metrics like BLEU (Bilingual Evaluation Understudy) are commonly used for machine translation. BLEU considers not only exact matches but also partial matches and provides partial credit for partially correct translations. It uses precision at the n-gram level, where n can be 1, 2, 3, etc., capturing partial matches.
4. **Semantic Evaluation:** In some cases, semantic similarity metrics are used to evaluate the meaning of the generated text, allowing for more flexibility in word choice and sentence structure.

It’s important to choose or design an evaluation metric that aligns with the specific requirements and expectations of the task. While exact match metrics provide a clear measure of correctness, token-level and semantic metrics can offer a more nuanced assessment of the model’s performance, taking into account variations in length and word choice. The choice of evaluation metric depends on the goals of the task and the desired balance between precision and flexibility.  
credit: GPT3.5

---

<div class="post-metadata">

**Author:** ![hwaseem04](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hwaseem04/32/26899_2.png) [@hwaseem04](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhwaseem04)%5C
**Post date:** [February 24, 2024, 9:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F12 "2024-02-24T09:59:54Z")

</div>

> In other words, what happens is, we prepend the decoder inputs with a special token (the decoder start token - which for T5 is the padding token, with index 0), and then the decoder needs to predict (in parallel) that:

do we have to manually prepend the `decoder_start_token`? After applying T5Tokenizer I can see the end token 's integer value, i.e 1 present at the end in each input\_ids and labels. But I dont see integer value 0 (corresponding to start token) at the of start of tokenized labels. Is it implicitly added while doing the inference/training?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [February 24, 2024, 11:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F13 "2024-02-24T11:04:32Z")

</div>

Hi,

No the model does that automatically as seen [here](https://github.com/huggingface/transformers/blob/c8d98405a8f7b0e5d07391b671dcc61bb9d7bad5/src/transformers/models/t5/modeling_t5.py#L898). The decoder\_input\_ids are created automatically by the model, based on the labels (by shifting them one position to the right + prepending the `decoder_start_token_id`).

---

<div class="post-metadata">

**Author:** ![hwaseem04](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hwaseem04/32/26899_2.png) [@hwaseem04](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhwaseem04)%5C
**Post date:** [February 25, 2024, 8:30am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhat-is-loss-function-for-t5%2F11669%2F14 "2024-02-25T08:30:52Z")

</div>

Thank you for the clarification
