# Scores in generate()

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%3E%5C
**Category:** Beginners\
**Created:** [January 30, 2021, 8:59pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450 "2021-01-30T20:59:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kylie](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@Kylie](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKylie)%5C
**Post date:** [January 30, 2021, 8:59pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F1 "2021-01-30T20:59:31Z")

</div>

Hi,

I was wondering why the length of the `output_scores` is always +1 longer than the `max_length` in the output of `generate()`? This seems to be not consistent with the documentation [https://huggingface.co/transformers/internal/generation\_utils.html#transformers.generation\_utils.BeamSampleEncoderDecoderOutput](https://huggingface.co/transformers/internal/generation_utils.html#transformers.generation_utils.BeamSampleEncoderDecoderOutput)

I found the `scores` from the output of the `generate()` function when setting `output_scores` to be `True` is `(max_length+1,)`-shaped tensors or shorter due to the early `eos_token_id` with each element of shape `(batch_size*num_beams, config.vocab_size)`. The shape of the `output.sequence` is `(batch_size, max_length).`

---

<div class="post-metadata">

**Author:** ![patrickvonplaten](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/patrickvonplaten/32/2171_2.png) [@patrickvonplaten](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpatrickvonplaten)%5C
**Post date:** [February 4, 2021, 10:22am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F2 "2021-02-04T10:22:00Z")

</div>

Hey @Kylie,

good observation! So `output_scores` should max\_length - 1. The reason is that the first token, the `decoder_start_token_id` is not generated, meaning that no scores can be calculated.

Here an example:

```
#!/usr/bin/env python3
from transformers import AutoModelForSeq2SeqLM
import torch

model = AutoModelForSeq2SeqLM.from_pretrained('facebook/bart-large')

out = model.generate(torch.tensor([10 * [1]]), return_dict_in_generate=True, output_scores=True, max_length = 10)

print("len scores:", len(out.scores)) # should give 9

```

Would you be interested in correcting the documentation in a PR for Transformers?

---

<div class="post-metadata">

**Author:** ![Kylie](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@Kylie](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKylie)%5C
**Post date:** [February 7, 2021, 2:15am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F3 "2021-02-07T02:15:54Z")

</div>

Hi @patrickvonplaten ,

Thanks for your reply! That makes more sense. Sure, I can correct that in a PR.

---

<div class="post-metadata">

**Author:** ![ad26kr](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@ad26kr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fad26kr)%5C
**Post date:** [May 12, 2021, 5:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F4 "2021-05-12T05:18:21Z")

</div>

Hi @patrickvonplaten

Any reason that the `decoder_start_token_id` is concatenated to the beginning of the generated token ids?

---

<div class="post-metadata">

**Author:** ![Kylie](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@Kylie](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKylie)%5C
**Post date:** [May 17, 2021, 9:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F5 "2021-05-17T21:38:17Z")

</div>

Hi @ad26kr, BART uses \<bos\> at the beginning of the decoder input to indicate the start of decoding. This is how the model pretrained (see Figure 1(c) of the [paper](https://arxiv.org/pdf/1910.13461.pdf)).

---

<div class="post-metadata">

**Author:** ![ad26kr](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@ad26kr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fad26kr)%5C
**Post date:** [May 18, 2021, 3:03am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F6 "2021-05-18T03:03:44Z")

</div>

Hi @Kylie  
I know that several seq2seq models such as BART, T5 use some special tokens as the first input token to let the decoder start decoding. However, I can’t understand why ‘the generated text/tokens from the model.generate()’ includes the special tokens (in the beginning of the generated token ids)

---

<div class="post-metadata">

**Author:** ![guotong1988](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/guotong1988/32/16295_2.png) [@guotong1988](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fguotong1988)%5C
**Post date:** [May 26, 2023, 1:29am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fscores-in-generate%2F3450%2F7 "2023-05-26T01:29:56Z")

</div>

What does the `scores` means?

Thank you!
