Use Pretrained Transformers with Hugging Face
Use pretrained transformers with Hugging Face — Applied AI engineering tutorial, lesson 57.
Focus: use pretrained transformers with hugging face
You’ve got a great NLP idea — sentiment analysis for customer reviews, maybe a chatbot, perhaps document summarization — but the thought of training a transformer from scratch stops you cold. Who has the GPU budget for that? The good news: you don’t need to. With Hugging Face’s transformers library, you can use pretrained transformers with Hugging Face in a few lines of Python — state-of-the-art models that are already trained and ready to serve. This lesson shows you exactly how to load, run, and integrate those models into your own projects, so you can ship production-ready AI without the training grind.
The problem this lesson solves
Training a transformer from scratch is a massive undertaking. Models like BERT and GPT-3 have hundreds of millions to billions of parameters, trained on terabytes of text over weeks on specialized hardware. For most real-world applications, that’s overkill and infeasible. The pain point: you need a powerful NLP model now, for sentiment analysis, text generation, summarization, or more — and you don’t have a research-grade cluster.
Even if you did have the compute, you’d need to gather a huge dataset, design a training loop, manage hyperparameters, and debug convergence issues. That’s months of work for something that already exists. The solution is transfer learning: take a pretrained transformer (weights already learned), and use it directly or fine-tune it on your smaller task-specific dataset.
Hugging Face makes this not just possible, but practical — with a unified API that downloads model weights, loads tokenizers, and runs inference in a few lines. In this lesson, you’ll see how to use pretrained transformers with Hugging Face for both text classification and generation, and you’ll know exactly when to use a pretrained model versus fine-tuning versus training from scratch.
Core concept / mental model
Think of a pretrained transformer as a highly educated intern that knows the structure of language deeply — grammar, syntax, facts, even some reasoning — but hasn’t yet learned your specific task. For example, BERT has absorbed hundreds of millions of English sentences, so it “knows” that "the movie was great" is positive. But it hasn’t been told that your particular dataset uses star ratings.
Hugging Face (HF) is the talent agency. The Hugging Face Hub hosts thousands of these pretrained models, along with their tokenizers and configuration files. The transformers library is the interface — it lets you call a single function, say pipeline() or AutoModelForSequenceClassification.from_pretrained("model-name"), and HF downloads the necessary weights and sets everything up.
This uses transfer learning: you take the knowledge stored in the pretrained weights and apply it to a new, related problem. The core components you’ll juggle are:
- Model: the neural network architecture, like
bert-base-uncased(classification) orgpt2(generation). - Tokenizer: converts text into numbers (token IDs) that the model can digest.
- Config: holds hyperparameters and model settings.
- Pipeline: a high-level abstraction that bundles tokenizer + model + post-processing into one call.
The mental model: you're not building the engine; you're renting a top-of-the-line engine and just bolting it onto your project's chassis.
How it works step by step
Here’s the logical flow to use pretrained transformers with Hugging Face in any application:
- Install the library:
pip install transformers(plustorchortensorflowas the backend). - Choose a model: Pick from the Hugging Face Hub —
model_typeand a specific checkpoint. Common choices:distilbert-base-uncasedfor speed,bert-base-uncasedfor accuracy,gpt2for generation. - Load the tokenizer: Use
AutoTokenizer.from_pretrained("your-model")to convert raw text intoinput_ids,attention_mask, etc. - Load the model: Use
AutoModelForSequenceClassification.from_pretrained("your-model")(or a generation class) to get the pretrained weights. - Preprocess: Tokenize your input text — with
max_length,padding, andtruncation— to match model expectations. - Run inference: Feed the tokenized inputs through the model; get raw logits or generated text.
- Post-process: Convert logits into probabilities (softmax), map to labels, or decode generated tokens into readable text.
Optionally, you can move the model to a GPU (model.to('cuda')) for speed, especially with larger models.
A common mistake beginners make is skipping the tokenizer and passing raw strings directly to the model — that won't work. The tokenizer is essential. Also, remember that around 95% of NLP tasks can be solved with a pretrained model; you only fine-tune when you need higher accuracy on a domain-specific language.
Hands-on walkthrough
Let's walk through a real example. First, install the necessary packages.
pip install transformers torch
Now, let's do a text classification — sentiment analysis — using a built-in pipeline for maximum simplicity.
from transformers import pipeline
# Load the sentiment analysis pipeline
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
text = "I absolutely loved the movie — the plot was thrilling!"
result = classifier(text)
print(result)
# Output: [{'label': 'POSITIVE', 'score': 0.999…}]
That's it — three lines to get a state-of-the-art sentiment classifier. But you're not limited to the built-in pipelines. For more control, use the AutoTokenizer and AutoModelForSequenceClassification classes directly.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Choose a model
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Preprocess the input
inputs = tokenizer("The food at this restaurant was terrible!", return_tensors="pt", truncation=True, max_length=128)
# Run inference
with torch.no_grad():
outputs = model(**inputs)
# Get probabilities
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
print(probs)
# Output: tensor([[0.0002, 0.9998]]) # label 0: NEGATIVE, label 1: POSITIVE
# Map to label
label_id = torch.argmax(probs, dim=-1).item()
print("Label:", model.config.id2label[label_id])
# Output: Label: NEGATIVE
Finally, let's try text generation with GPT-2. Note that generation uses a different model class and tokenizer.
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Set padding token for batched generation
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
prompt = "The future of artificial intelligence is"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=50,
do_sample=True,
temperature=0.7,
pad_token_id=tokenizer.pad_token_id
)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(text)
# Output: "The future of artificial intelligence is a fascinating one. It will transform..." (varies)
These examples are your blueprint. You can swap model_name to any of the thousands on the Hub, and the same pattern applies — class-specific loading, tokenization, forward pass.
Compare options / when to choose what
Not all pretrained transformers are equal. Here’s when to pick a specific one.
| Model | Task | Strengths | Weaknesses | Best For |
|---|---|---|---|---|
distilbert-base-uncased |
Classification, QA | Faster, smaller, 95% of BERT's accuracy | Less accurate than full BERT | Low-latency production, mobile |
bert-base-uncased |
Classification, QA, NER | Robust, well-documented | Slower, bigger | Feature extraction, fine-tuning |
gpt2 |
Generation | Good for text completion, creative writing | Can be verbose, biased | Buttons, chatbot personas |
t5-base |
Summarization, translation | Multi-tasking, controllable | Requires task prefix | Summarization, translation |
roberta-base |
Classification, NER | Better than BERT on benchmarks | Larger | Accuracy-critical tasks |
Pro tip: Always try the smallest model that meets your accuracy target. DistilBERT is ~60% smaller than BERT and runs 60% faster — great for production where latency matters.
When to use a pretrained model as-is: your task aligns with the model's training objective (e.g., sentiment analysis with a sentiment-finetuned model). You need quick results and have no domain-specific dataset.
When to fine-tune: your task is slightly different (e.g., classify legal documents) but you have a small labeled dataset. You take the pretrained weights, then continue training on your own data for a few epochs.
When to train from scratch: almost never — only when you have a truly novel architecture or enormous labeled data. For 99% of projects, transfer learning is the way.
Troubleshooting & edge cases
Here are the most common struggles when you use pretrained transformers with Hugging Face, and how to fix them.
1. Out-of-memory (OOM) errors.
- Symptom: CUDA out of memory on your GPU, or memory ballooning on CPU.
- Fix: Use a smaller model like distilbert-base instead of bert-large. Also, batch your inputs — process one at a time or set batch_size=1.
2. Tokenizer mismatch.
- Symptom: You get nonsensical results or errors about tokenizer/model mismatch.
- Fix: Always load the tokenizer from the same model name you use for the model. If you use AutoTokenizer.from_pretrained("bert-base-uncased"), you must load the model with the same name. Don't mix tokenizers.
3. Sequence length too long.
- Symptom: Token indices sequence length is longer than the specified maximum sequence length.
- Fix: Set truncation=True and max_length=512 (or lower) in the tokenizer call. This now happens automatically if you specify truncation=True.
4. No output or random gibberish for generation.
- Symptom: GPT-2 returns empty or nonsense text.
- Fix: Ensure you set pad_token_id=tokenizer.eos_token_id if the model has no pad token. Also check that you use max_new_tokens instead of max_length to avoid influencing the prompt.
5. Slow inference on CPU.
- Symptom: Model takes seconds per prediction.
- Fix: Move to GPU if available: model.to("cuda") and send inputs to cuda. Otherwise, use a quantization method like torch.quantization or simply choose a smaller model.
6. Missing padding token.
- Symptom: Error when batching with generated/pad token.
- Fix: Set tokenizer.pad_token = tokenizer.eos_token (if left None) or pass pad_token_id to the model during generation.
Pro tip: When in doubt, look up the model card on the Hugging Face Hub — it often solves the exact error you're seeing.
What you learned & what's next
You now know how to use pretrained transformers with Hugging Face to solve real-world NLP tasks without training a model from scratch. You can:
- Explain the concept of pretrained transformers and transfer learning.
- Load any model from the Hub with
AutoTokenizerandAutoModelFor*. - Run inference for classification and generation.
- Choose the right model for your task based on trade-offs.
- Debug common issues like tokenizer mismatches and OOM errors.
The next lesson in this track will build on this foundation — move on to the next step in the Applied AI engineering path to learn how to fine-tune a pretrained transformer on your own dataset. That's where you'll see full power: taking a general model and adapting it to your unique data. You've taken the critical first step; now keep going!
Practice recap
Try this: load the distilbert-base-uncased-finetuned-sst-2-english model with pipeline("sentiment-analysis") and run it on three new sentences of your own. Then, switch to using AutoTokenizer and AutoModelForSequenceClassification to see how to get raw logits and probabilities. When you're comfortable, move to the next lesson on fine-tuning to adapt a model to your own dataset.
Common mistakes
- Passing raw text to the model without tokenizing — results in cryptic errors or wrong shapes.
- Using a tokenizer from a different model than the one you loaded — leads to subpar performance.
- Ignoring memory constraints — running a huge model like
bert-largeon a laptop CPU can OOM or take forever. - Forgetting to set
truncation=Truewith long inputs — causes automatic truncation to the first 512 tokens? (actually it will error without truncation flag) - Assuming you must fine-tune; for many tasks a pretrained model already works well.
Variations
- Use TensorFlow instead of PyTorch — the
transformerslibrary supports both. - Use the
pipelineAPI for simplicity, versus lower-levelAutoModelclasses for full control. - Use quantization or ONNX to optimize pretrained models for deployment on CPU.
Real-world use cases
- Sentiment analysis for product reviews in an e-commerce dashboard — classify customer feedback automatically.
- Spam or abusive content detection in a community platform — reject harmful posts in real time.
- Text summarization for news articles to generate quick snippets for a content aggregation app.
Key takeaways
- Pretrained transformers from Hugging Face give you state-of-the-art NLP with minimal code.
- The
pipelineAPI is the fastest way to get started; theAutoModelclasses offer more control. - Always match tokenizer and model — mixing them is a common bug.
- You don't need to train from scratch — transfer learning with pretrained weights is the norm.
- Choose smaller models like DistilBERT for production latency; larger ones only when accuracy demands it.
- Respect memory limits, use truncation, and set pad tokens for generation.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.