Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Loading Pre-trained Models and Tokenizers

Hello! Welcome to the next step in our journey into Transformers.

In our previous lesson, we got our hands dirty by implementing the scaled dot-product attention mechanism from scratch. This gave you a fundamental understanding of the engine that powers modern NLP. Building from the ground up is invaluable for deep comprehension, but in practice, AI engineers leverage powerful libraries that handle this complexity for us.

Today, we'll move up a level of abstraction and learn how to use the industry-standard Hugging Face transformers library. Our goal is to load a pre-trained model and its corresponding tokenizer from the Hugging Face Hub. This is the foundational skill for applying state-of-the-art models to real-world tasks, moving us from implementing the mechanics to applying the full power of Transformers.

Welcome to the Hugging Face Ecosystem

The Hugging Face ecosystem consists of two main parts:

  1. The Hub: A massive online repository where researchers and organizations share thousands of pre-trained models, datasets, and demos. It's like GitHub for machine learning.
  2. The transformers library: A Python library that provides a simple and unified API to download and use the models from the Hub.

Let's start with a brief video that introduces the library and shows you how to navigate the Hub to find models.

Stanford CS224N NLP with Deep Learning | 2023 | Hugging Face Tutorial, Eric Frankel

This video from a Stanford NLP course provides a concise introduction to the Hugging Face library and demonstrates how to find different types of models on the Hub.

Watch from the beginning to 00:03:08. Pay attention to the distinction between the library (transformers) and the Hub (the collection of models), and see how models for various tasks can be found online.

As you saw, the Hub contains an incredible variety of models. The key is knowing how to get them from the Hub into your Python code.

The Auto Classes: One API to Rule Them All

With hundreds of different Transformer architectures available (BERT, GPT-2, T5, etc.), loading a model could be complicated. You would need to import the specific class for each model type, like BertModel or GPT2Model.

Hugging Face simplifies this with a brilliant abstraction: the Auto classes.

  • AutoTokenizer
  • AutoModel (and its task-specific variants like AutoModelForSequenceClassification)

These classes are designed to automatically detect the architecture of a pre-trained model from its name and instantiate the correct tokenizer and model classes for you. This allows you to switch between different models with minimal code changes.

Let's read a short explanation of this concept.

Introduction to Hugging Face

This article from CodeSignal Learn explains the purpose of the Auto classes and how they provide flexibility.

Please read the section titled 'Understanding Auto Classes: Flexible Model Loading'. It clearly explains why this abstraction is so useful.

Loading a Model and Tokenizer in Practice

The primary method you'll use to load models and tokenizers is .from_pretrained(). This powerful method handles everything:

  1. It checks if the model files are already downloaded to a local cache.
  2. If not, it downloads the necessary files from the Hugging Face Hub.
  3. It loads the model's configuration, weights, and tokenizer vocabulary into memory.

Let's see a practical demonstration of using AutoTokenizer and AutoModelForSequenceClassification with the .from_pretrained() method.

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

This video from AssemblyAI demonstrates the exact code needed to load a tokenizer and a model for a classification task.

Watch from 00:04:39 to 00:06:25. The presenter shows how to import the Auto classes and use the .from_pretrained(model_name) method, which is the core of today's lesson.

Now, let's write the code ourselves. First, make sure you have the library installed:
pip install transformers torch

Here is how you would load distilbert-base-uncased, a smaller and faster version of BERT, along with its tokenizer.

from transformers import AutoTokenizer, AutoModel




# 1. Define the model name from the Hugging Face Hub
model_name = "distilbert-base-uncased"




# 2. Load the tokenizer
# The tokenizer is responsible for converting raw text into numerical IDs.
# It's crucial to use the exact same tokenizer the model was trained with.
tokenizer = AutoTokenizer.from_pretrained(model_name)
print(f"Loaded tokenizer for {model_name}.")




# 3. Load the model
# AutoModel loads the base Transformer model without any specific head on top.
# The output of this model is the raw hidden states (the embeddings for each token).
model = AutoModel.from_pretrained(model_name)
print(f"Loaded base model for {model_name}.")




# Let's inspect the model
print("\nModel configuration:")
print(model.config)

Running this code will trigger a download on the first run, and you'll see progress bars as the files are fetched. Subsequent runs will be much faster as they will load from the local cache.

What's Being Downloaded?

When you call .from_pretrained(), you are downloading the model's repository from the Hub. This includes several files that define the model's architecture, its learned weights, and its tokenizer.

HuggingFace Model Repository Files
This image shows the typical files in a model repository on the Hugging Face Hub. `pytorch_model.bin` contains the trained weights, `config.json` defines the architecture, and files like `vocab.txt` and `tokenizer.json` define the tokenizer.

Base Models vs. Models with Heads

In the code above, we loaded AutoModel. This gives you the core Transformer layers, which are great for feature extraction (getting contextual embeddings). However, for specific tasks like text classification or question answering, you need a model with an appropriate task-specific head.

A "head" is one or more extra layers added on top of the base Transformer to adapt it for a specific task. For example, a sequence classification head is typically a linear layer that takes the final Transformer output and projects it to the number of class labels.

Hugging Face provides Auto classes for these common tasks:

  • AutoModelForSequenceClassification
  • AutoModelForQuestionAnswering
  • AutoModelForTokenClassification
  • And many more...

Let's modify our code to load a model suitable for classification:

from transformers import AutoModelForSequenceClassification




# Use the same model name
model_name = "distilbert-base-uncased-finetuned-sst-2-english"




# The tokenizer remains the same
tokenizer = AutoTokenizer.from_pretrained(model_name)




# Load the model with a sequence classification head
# This model has been pre-trained AND fine-tuned for sentiment analysis.
model_with_head = AutoModelForSequenceClassification.from_pretrained(model_name)

print(f"Loaded model with classification head: {model_name}")




# Notice the config now specifies the labels
print("\nModel configuration:")
print(model_with_head.config)

If you inspect the config of this new model, you'll see id2label and label2id mappings for "NEGATIVE" and "POSITIVE", because it has been fine-tuned on a sentiment analysis dataset.

The Full Picture

The tokenizer and the model are two key components of a larger pipeline that goes from raw text to a prediction.

Transformer Model Inference Pipeline
This diagram shows the complete workflow. Our raw text goes into the **Tokenizer** (which we just loaded). The resulting numerical IDs go into the **Model** (which we also loaded). The model produces logits, which are then post-processed into a final prediction.

By learning to load a tokenizer and a model using Auto classes, you have mastered the first and most critical step in using the Hugging Face ecosystem.

Conclusion

In this lesson, we bridged the gap from theory to practice by learning how to leverage the Hugging Face transformers library. Instead of building models from scratch, we can now load powerful, pre-trained models with just two lines of code.

Key Takeaways:

  • Hugging Face Hub is a repository of pre-trained models, and the transformers library is the tool to use them.
  • The Auto classes (AutoTokenizer, AutoModel) provide a simple, unified interface to load any Transformer model without needing to know its specific architecture beforehand.
  • The .from_pretrained(model_name) method is the workhorse function that downloads and caches model files.
  • Every model has a corresponding tokenizer that must be used with it.
  • Models come in two main flavors: base models (for feature extraction) and models with task-specific heads (for end-to-end tasks like classification).

Preview of the Next Lesson

Now that we know how to load a model and a tokenizer, what's next? In our next lesson, we will use an even higher-level abstraction, the Hugging Face pipeline, to perform text classification with just a few lines of code. We will take the components we learned about today and see how pipeline combines them to deliver results with minimal effort.

Can't find a good explanation? Sign up and we'll make it for you

Sign up