# Why use tokenizer in Trainer with Tokenized Data

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%3E%5C
**Category:** 🤗Transformers\
**Created:** [September 12, 2024, 8:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433 "2024-09-12T08:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicoberk](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicoberk/32/26983_2.png) [@nicoberk](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicoberk)%5C
**Post date:** [September 12, 2024, 8:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%2F1 "2024-09-12T08:18:21Z")

</div>

Hi there!

I am getting started with the implementation of the SageMaker integration using [this notebook](https://github.com/huggingface/notebooks/tree/main/sagemaker/01_getting_started_pytorch). I realised that this notebook preprocesses the train and test set, such that they are tokenized when calling the estimator later in the notebook. However, assessing the script called, the tokenizer is also provided to the Trainer. Can somebody explain to me why this is necessary?

This is both a question out of curiosity and for this concrete application, as I am a) worried to tokenize twice; and b) have several different tokenizers, so calling the tokenizer in the trainer directly would be less tedious than tokenizing it first and then providing it to the trainer again.

---

<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:** [September 12, 2024, 9:21am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%2F2 "2024-09-12T09:21:29Z")

</div>

I assume you refer to [this script](https://github.com/huggingface/notebooks/blob/main/sagemaker/01_getting_started_pytorch/scripts/train.py), the reason the tokenizer is provided to the Trainer is because the text isn’t tokenized yet. One only provides the `train_dataset` and `eval_dataset` to it, and the Trainer will internally use the tokenizer to tokenize the text.

That said, a tokenizer is often provided to the Trainer even if the dataset is already tokenized so that it saves/pushes its config files locally/to the hub, along with the model weights.

---

<div class="post-metadata">

**Author:** ![nicoberk](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicoberk/32/26983_2.png) [@nicoberk](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicoberk)%5C
**Post date:** [September 12, 2024, 9:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%2F3 "2024-09-12T09:41:36Z")

</div>

Ok, thank you for the quick clarification. I think in this case the data is already tokenized (see section ‘Preprocessing’ in the linked notebook, where the tokenizer is mapped to the datasets and then pushed to the hub). But if I provide tokenized data and a tokenizer, the Trainer works fine as well?

---

<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:** [September 12, 2024, 4:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%2F4 "2024-09-12T16:02:32Z")

</div>

The docs says that if you pass a tokenizer to the [Trainer](https://huggingface.co/docs/transformers/en/main_classes/trainer#api-reference%20%5D%5B%20transformers.Trainer), then the [DataCollatorWithPadding](https://huggingface.co/docs/transformers/en/main_classes/data_collator#transformers.DataCollatorWithPadding) is used. I checked the source code, and all this is going to do is use `tokenizer.pad` to make sure all PyTorch tensors are of the same length. So you’re good 🙂

---

<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:** [September 13, 2024, 5:23am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-use-tokenizer-in-trainer-with-tokenized-data%2F106433%2F5 "2024-09-13T05:23:24Z")

</div>

Closed on September 13, 2024, 5:23am UTC

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