# Exclude words from GPT-2 generate( )

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741%3E%5C
**Category:** 🤗Transformers\
**Created:** [March 19, 2021, 7:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741 "2021-03-19T19:27:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hfnlpmb](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hfnlpmb](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhfnlpmb)%5C
**Post date:** [March 19, 2021, 7:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741%2F1 "2021-03-19T19:27:43Z")

</div>

Hello,

I want to exclude some ids of the GPT-2 vocabulary from the `generate()` function, e.i. I want when the model generates the next word, not to be able to use a word from a list of words. How can I achieve that?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![deathcrush](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@deathcrush](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fdeathcrush)%5C
**Post date:** [March 19, 2021, 7:32pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741%2F2 "2021-03-19T19:32:07Z")

</div>

Hello!

I seems that this functionality is supported using the `bad_words_ids` input to the `generate` API. The [docs](https://huggingface.co/transformers/main_classes/model.html?highlight=generate) briefly describe that you need to find a list of integers for the words you care about using the `tokenizer` and then simply pass those to `generate`:

```auto
**bad_words_ids** ( `List[List[int]]` , optional) – List of token ids that are not allowed to be generated. In order to get the tokens of the words that should not appear in the generated text, use `tokenizer(bad_word, add_prefix_space=True).input_ids` .

I hope this helps!
``
```

---

<div class="post-metadata">

**Author:** ![hfnlpmb](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hfnlpmb](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhfnlpmb)%5C
**Post date:** [March 19, 2021, 8:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741%2F3 "2021-03-19T20:04:15Z")

</div>

Thank you @deathcrush , it appears that this is exactly what I want.

---

<div class="post-metadata">

**Author:** ![ignacioiglesias](https://avatars.discourse-cdn.com/v4/letter/i/ac8455/32.png) [@ignacioiglesias](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fignacioiglesias)%5C
**Post date:** [April 26, 2023, 3:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fexclude-words-from-gpt-2-generate%2F4741%2F4 "2023-04-26T03:57:52Z")

</div>

Hey, @hfnlpmb, in case you want to check it: for something similar to the `logits_bias` parameter in GPT-3, I proposed a extension to the NoBadWords logits processor in [this](https://github.com/huggingface/transformers/issues/22168#issuecomment-1477998997) feature request. Feel free to react/comment in the issue if you think that helps.
