Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 25

BERT Tokenization with Hugging Face: Padding and Truncation

Recommended resources

Welcome back! In our previous lesson, we established the conceptual foundation for fine-tuning pre-trained language models like BERT. You learned how we can adapt these powerful, general-purpose models for a specific task—like your complaint classification—by adding a new "head" and gently adjusting the model's weights.

Today, we'll take the first practical step in that process. Before we can feed any text to our model, we must convert it into a numerical format that the model can understand. This crucial preprocessing step is called tokenization. This lesson will guide you through using the Hugging Face transformers library to tokenize your complaint texts, ensuring they are perfectly formatted for input into a BERT-style model. We will specifically focus on handling batches of text with varying lengths by using padding and truncation, which are essential for efficient model training.

By the end of this lesson, you will have the skills to transform your raw text data into PyTorch tensors ready for fine-tuning.

1. The Tokenization Pipeline: From Text to Numbers

At its core, a Transformer model is a collection of mathematical operations performed on tensors. It has no inherent understanding of words or characters. The goal of tokenization is to bridge this gap by converting a raw string of text into a set of numerical inputs. This isn't a single step, but a pipeline:

  1. Splitting into Tokens: The input text is first broken down into smaller units called tokens. These can be words, but modern tokenizers often use a subword approach. For example, an uncommon word like "rumination" might be split into more common subwords like "rum" and "##ination". This allows the model to handle a vast vocabulary and even words it has never seen before.
  2. Adding Special Tokens: Models like BERT require special tokens to understand the structure of the input. The most common are [CLS] (Classification), which is added to the beginning of a sequence and used for classification tasks, and [SEP] (Separator), which is used to separate sentences.
  3. Converting to IDs: Each token (including the special ones) is then mapped to a unique integer ID from the tokenizer's vocabulary. This vocabulary was created during the model's pre-training phase.

The result is a sequence of numbers that can be fed into the model.

The following diagram provides a perfect visual summary of this entire process for a model similar to BERT (DistilBERT).

This diagram shows the three main steps of tokenization: (1) breaking words into tokens (including subwords like "##ination"), (2) adding special `[CLS]` and `[SEP]` tokens, and (3) converting the final sequence of tokens into their corresponding numerical IDs.

2. Tokenizing in Practice with Hugging Face

The Hugging Face transformers library simplifies this entire process immensely. The standard tool for this is the AutoTokenizer class, which automatically downloads the correct tokenizer associated with a specific pre-trained model. This ensures that your text is processed in exactly the same way the model was originally trained.

Let's see how this works. First, we load a tokenizer. For your baseline, we'll use distilbert-base-uncased, a smaller and faster version of BERT, which is ideal for rapid experimentation.

from transformers import AutoTokenizer

model_checkpoint = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)

Now, we can apply this tokenizer to a single sentence.

complaint = "The dal was watery and had no salt."
tokenized_complaint = tokenizer(complaint)

print(tokenized_complaint)



# Output:
# {'input_ids': [101, 1996, 11503, 2001, 23010, 2038, 1998, 2018, 2053, 102], 
#  'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

The output is a dictionary containing two key items:

  • input_ids: These are the numerical representations of our tokens, including the [CLS] token at the beginning (ID 101) and the [SEP] token at the end (ID 102).
  • attention_mask: This is a list of 1s and 0s that tells the model which tokens to pay attention to. Since all tokens here are real, the mask is all 1s. We'll see why this is important next.

3. Handling Batches: Padding and Truncation

In a real project, you won't process sentences one by one. You'll process them in batches to leverage your GPU. However, this introduces a problem: sentences in a batch have different lengths. Neural networks require inputs in a batch to have a uniform shape.

The solution involves two key techniques:

  • Padding: Shorter sentences are "padded" with a special [PAD] token until they are as long as the longest sentence in the batch. The attention_mask becomes crucial here: it will have a 0 for every [PAD] token, telling the self-attention mechanism to ignore them.
  • Truncation: Sentences longer than the model's maximum acceptable length (e.g., 512 tokens for BERT) must be cut short, or "truncated".

The Hugging Face tokenizer can handle both of these automatically. Furthermore, to feed the output directly into PyTorch, we can ask it to return PyTorch tensors instead of Python lists using return_tensors="pt".

The following video provides an excellent, concise demonstration of how to tokenize a batch of sentences using these crucial parameters.

Getting Started With Hugging Face in 15 Minutes | Transformers, Pipeline, Tokenizer, Models

This clip from AssemblyAI shows how to use the tokenizer on a batch of sentences and configure padding, truncation, and tensor output.

Watch the section from combining with PyTorch. Pay close attention to the call to the tokenizer and the arguments padding=True, truncation=True, and return_tensors='pt'. Notice how the resulting input_ids tensor has a uniform length and how the attention_mask has zeros for the padded tokens.


4. Applying Tokenization to Your Entire Dataset

For your project, you'll need to apply this tokenization process to all the complaints in your dataset. The Hugging Face datasets library provides a highly efficient method for this called .map(). This method applies a function across an entire dataset, processing examples in batches for speed.

The workflow looks like this:

  1. Load your text data into a Dataset object.
  2. Create a tokenization function that takes a batch of examples and runs them through the tokenizer.
  3. Apply this function to your dataset using .map(tokenize_function, batched=True).

The following resources walk you through this exact process. The first is a tutorial document that serves as a great reference, and the second is a practical guide showing the code in action.

[PDF] Hugging Face Transformers Tutorial

This tutorial from a Stanford CS224N course provides a clear, step-by-step breakdown of the tokenization pipeline and how to handle batching.

Please start by reading the section Tokenizer: Step by Step to see the individual operations that the tokenizer performs. Then, focus on the part labeled Padding, Truncation & Batching, which directly explains how the tokenizer handles multiple sentences of different lengths to create a uniform batch.

Now, let's look at how to apply this to a full dataset using the .map() method.

Building a Text Classifier App with Hugging Face, BERT, and ...

This blog post provides a practical example of preprocessing a dataset for text classification.

Read "Step 5. Data Preprocessing". Focus on how they define a preprocessing function that calls the tokenizer with truncation=True, and then how they apply this function to the entire dataset using the map method with batched=True. This is the exact pattern you will use for your project.

Finally, for a video walkthrough of integrating a tokenizer with the datasets.map() function, the following clip is very helpful.

Build NLP Pipelines with HuggingFace Datasets

This video from James Briggs demonstrates how to apply a tokenizer to a Dataset object using the .map() function with batching.

Watch the segment from adding batching to mapping. The presenter shows how to tokenize the question and context fields for a question-answering task. The principle is identical for your classification task: you will pass your "complaint" column to the tokenizer within a .map() call. Note the use of batched=True and batch_size for efficient processing.

By using this .map() approach, you can preprocess thousands of complaints in a matter of seconds, creating a dataset that is perfectly prepared for the fine-tuning stage.


Conclusion

In this lesson, you've moved from the theory of fine-tuning to the practical necessity of data preparation. You now know how to convert raw text complaints into the numerical format required by BERT-style models.

Here are the key takeaways:

  • Tokenization is a multi-step pipeline that converts text into input_ids and an attention_mask.
  • Subword tokenization allows models to handle large vocabularies and out-of-vocabulary words.
  • The Hugging Face AutoTokenizer is the standard tool to ensure your text preprocessing matches the pre-trained model.
  • When tokenizing batches of text, you must use padding (for short sentences) and truncation (for long sentences) to create uniformly sized tensors. The attention_mask is used to tell the model to ignore padded tokens.
  • The most efficient way to tokenize an entire collection of texts is by loading it into a Hugging Face Dataset object and using the .map() method with batched=True.

You are now fully equipped to prepare your text data. In our next lesson, we will take these tokenized inputs and use the high-level Hugging Face Trainer API to fine-tune a pre-trained model, building your text-only complaint classification baseline.