# Removing tokens from the GPT tokenizer

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fremoving-tokens-from-the-gpt-tokenizer%2F30753%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 30, 2023, 1:53pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fremoving-tokens-from-the-gpt-tokenizer%2F30753 "2023-01-30T13:53:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajesujoba](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@ajesujoba](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fajesujoba)%5C
**Post date:** [January 30, 2023, 1:53pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fremoving-tokens-from-the-gpt-tokenizer%2F30753%2F1 "2023-01-30T13:53:01Z")

</div>

How can I remove unwanted sub-tokens from GPT vocabulary or tokenizer? I have tried an existing approach that was used for a ROBERTa kind of model as shown below ([Removing tokens from the tokenizer · Issue #15032 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/issues/15032)). However it fails at the point of initializing the “model” component of the backend\_tokenizer with the new vocabulary.

```auto
#1. Get your tokenizer and the list of tokens you want to remove

import json
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("gpt2")

# get all tokens with "unused" in target_tokenizer
unwanted_words = ['ply', 'Ġmor','Ġprovide','IC','ung','Ġparty', 'Ġexist', 'Ġmag',]

#2. Get the arguments that allowed to initialize the "model" component of the backend_tokenizer.
model_state = json.loads(tokenizer.backend_tokenizer.model. __getstate__ ())
print(len(model_state["vocab"]))

#3. Modify the initialization arguments, in particular the vocabulary to remove the tokens we don't want

# remove all unwanted tokens from the vocabulary
for word in unwanted_words:
    del model_state["vocab"][word]

print(len(model_state["vocab"]))

#4. Intitialize again the "model" component of the backend_tokenizer with the new vocabulary

from tokenizers import models

model_class = getattr(models, model_state.pop("type"))

tokenizer.backend_tokenizer.model = model_class(**model_state)

print(len(tokenizer.vocab))

```

And below is the error:

```auto
---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-21-fa908d23c419> in <module>
     30 model_class = getattr(models, model_state.pop("type"))
     31 
---> 32 tokenizer.backend_tokenizer.model = model_class(**model_state)
     33 
     34 print(len(tokenizer.vocab))

TypeError: argument 'merges': failed to extract enum PyMerges ('Merges | Filename')
- variant Merges (Merges): TypeError: failed to extract field PyMerges::Merges.0, caused by TypeError: 'str' object cannot be converted to 'PyTuple'
- variant Filename (Filename): TypeError: failed to extract field PyMerges::Filename.0, caused by TypeError: 'list' object cannot be converted to 'PyString'

```

What other methods can I use or refer to? The original script I adapter was used for ROBERTa which uses Sentencepiece but GPT uses BPE.

---

<div class="post-metadata">

**Author:** ![ikkiren](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@ikkiren](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fikkiren)%5C
**Post date:** [July 29, 2024, 7:30pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fremoving-tokens-from-the-gpt-tokenizer%2F30753%2F2 "2024-07-29T19:30:14Z")

</div>

Check this [repo](https://github.com/1kkiRen/Tokenizer-Changer). Probably it solves your problem

---

<div class="post-metadata">

**Author:** ![ikkiren](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@ikkiren](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fikkiren)%5C
**Post date:** [August 20, 2024, 10:18pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fremoving-tokens-from-the-gpt-tokenizer%2F30753%2F3 "2024-08-20T22:18:21Z")

</div>

A more detailed answer

You can check the [`TokenizerChanger`](https://github.com/1kkiRen/Tokenizer-Changer) library.

Use the `TokenizerChanger` class to declarate the changer:

```python
changer = TokenizerChanger(tokenizer)

```

Then use the following:

```python
changer.delete_tokens(list_of_unwanted_tokens, include_substrings)

```

Deletes the unwanted tokens from the tokenizer.

If `include_substrings` is `True`, all token occurrences will be deleted even in other tokens. Defaults to `True`.

You can check other `delete` functions if you need

I am the author of this open source library. The very idea of creating this library appeared due to the fact that in my scientific work I encountered the problem described above: the inability to remove tokens from the dictionary. With my answer, I’m not trying to promote myself, I just want people who are faced with this problem to know that it has a solution.
