Stop 09 · Natural Language ProcessingTopics · 6/10

Hugging Face Transformers Library

Part of the AI Engineer Roadmap

In short

Thousands of trained models are one line of code away. The Hugging Face transformers library gives every one of them the same interface, so switching models is often just changing a name.

The standard Python library for using and fine-tuning pretrained transformer models — the tool you'll reach for constantly across NLP, and increasingly vision and audio too.

The blob holds up a library card at a counter in front of tall shelves full of colourful books.

Think of it like this

One library card for every book

A big public library lets you borrow any book with the same card and the same counter. You don't learn a new process per book. The Hugging Face Hub is the library, from_pretrained() is the counter, and the models are the books.

Watch it happen

Loading and running a model

Your code
HF Hub
Local cache
Model

from_pretrained(name)

1/5You ask for a model by its Hub name, like an organisation and model id.

The big ideas

What you need to know

Auto classes pick the right model

AutoTokenizer and AutoModelFor... read the config and build the correct architecture, whether it's BERT, RoBERTa or another.

pipeline() for quick wins

pipeline('task') bundles tokenizer, model and post-processing. Great for prototypes; drop down to Auto classes for control.

Tokenizers must match

Always load the tokenizer from the same name as the model. A mismatched tokenizer gives garbage ids.

Trainer for fine-tuning

The Trainer class handles the training loop, evaluation, checkpoints and mixed precision, with datasets from the datasets library.

See it in code

Walk through the code

python
1from transformers import AutoTokenizer, AutoModelForSequenceClassification2import torch3 4name = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"5tokenizer = AutoTokenizer.from_pretrained(name)        # downloads once, then cached6model = AutoModelForSequenceClassification.from_pretrained(name)7 8inputs = tokenizer("This course is actually fun.", return_tensors="pt")9print(inputs["input_ids"])                             # text -> token ids10 11with torch.no_grad():12    logits = model(**inputs).logits13probs = logits.softmax(dim=-1)14print(model.config.id2label[int(probs.argmax())], probs.max().item())

1/3Load the tokenizer and model by the same name. Both are cached after the first download.

What pipeline() does for you, written out. Seeing the steps makes debugging much easier.

Save yourself the pain

Common mistakes

Oops

Loading a base model like bert-base-uncased for classification and using its outputs directly.

Do this instead

A base model's classifier head starts random. Fine-tune it first, or pick a model already fine-tuned for your task.

Oops

Ignoring the model card.

Do this instead

Read the card for the training data, languages, licence and known limits before using a model in a project.

Your mission

Swap three models

~25 min

Quick check

Why should the tokenizer and the model be loaded from the same name?

Go deeper

  1. 1Work through the Hugging Face course's core sections on pipelines and fine-tuning.
  2. 2Use the `pipeline()` API for a few different tasks before fine-tuning anything.
  3. 3Learn to load, fine-tune and save a model with the `Trainer` API.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge