AI Hints
Home›Deep Learning Tutorial›Using Pretrained Transformers
Deep Learning · Lesson 55 of 92

Using Pretrained Transformers

The Hugging Face toolkit.

⏱ 2 min read▶ Video at 1:47:50🎓 Free
Watch this lesson in the Deep Learning courseWatch on YouTube ↗Full Deep Learning course

You could spend millions training a language model. Or you could download one, for free, with one line. Hugging Face hosts well over a million models, and it's become the place where the A I world shares its work. Knowing your way around it is a core skill.

Using Pretrained Transformers
From the video: Using Pretrained Transformers
  • Millions to train one
  • Or one line to download
  • The GitHub of AI

One line to a model

The pipeline function gets you from nothing to a working model, in a single line. Here's a sentiment model. We pass in two sentences. Run it. It downloads the weights, tokenises the text, runs the model, and gives you a label with a score. There are pipelines for translation, summarising, speech, and many more. This is the fastest way to find out whether a task is even possible.

pipeline.pypython
from transformers import pipeline

clf = pipeline("sentiment-analysis", device="cpu",
               model="distilbert-base-uncased-finetuned-sst-2-english")

for text in ["This course is brilliant.", "The battery died after an hour."]:
    r = clf(text)[0]
    print(f"{r['label']:<9} {r['score']:.3f}  {text}")
Output
POSITIVE  1.000  This course is brilliant.
NEGATIVE  1.000  The battery died after an hour.

Looking inside

For more control, load the parts yourself. The tokeniser turns text into numbers. And the model gives one vector per token. Run it. Six tokens, each with seven hundred and sixty eight numbers, from a model with around twenty six million weights. It's the same design that you built by hand, just bigger. And this hidden output is what you'd attach a new head to.

inside.pypython
from transformers import AutoModel, AutoTokenizer
import torch

name = "distilbert-base-uncased"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModel.from_pretrained(name)

inputs = tok(["Deep learning is fun"], return_tensors="pt")
with torch.no_grad():
    out = model(**inputs).last_hidden_state

print("Tokens:", inputs["input_ids"].shape[1])
print("Output:", list(out.shape))
print("Weights:", sum(p.numel() for p in model.parameters()))
Output
Tokens: 6
Output: [1, 6, 768]
Weights: 66362880

[transformers] DistilBertModel LOAD REPORT from: distilbert-base-uncased
Key                     | Status     |  |
------------------------+------------+--+-
vocab_layer_norm.bias   | UNEXPECTED |  |
vocab_transform.weight  | UNEXPECTED |  |
vocab_layer_norm.weight | UNEXPECTED |  |
vocab_transform.bias    | UNEXPECTED |  |
vocab_projector.bias    | UNEXPECTED |  |

Notes:
- UNEXPECTED:	can be ignored when loading from different task/architecture; not ok if you expect identical arch.

Summary

Pipelines for speed, and models and tokenisers for control. So how do we teach one of these a brand new task? The very same way that we did with images. Replace the head.

Key takeaways
  • pipeline gets you started
  • Tokeniser plus model
  • Output: one vector per token
Next up: Fine-Tuning a Transformer — transfer learning for language