Skip to content

Cheatsheet: Run a model in a few lines

from transformers import pipeline
classifier = pipeline("sentiment-analysis")
classifier("I love this!")

Picks a default model, downloads and caches it, runs all three steps. Pass model="checkpoint-name" to override the default.

Task stringWhat it does
sentiment-analysisClassify text as positive or negative
zero-shot-classificationClassify into labels you supply at call time
text-generationContinue a prompt (decoder-only)
fill-maskFill in a blanked token (encoder-only)
nerNamed-entity recognition (label tokens)
question-answeringExtract an answer span from a context
summarizationShorten a long text
translationTranslate between languages
StepWhat happensClass
1. PreprocessText becomes input_ids + attention_maskAutoTokenizer
2. ModelNumbers run through the network, output logitsAutoModelFor<Task>
3. PostprocessLogits become probabilities and labelssoftmax + id2label
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
# inputs -> {"input_ids": ..., "attention_mask": ...}
  • padding=True: pad a batch to equal length
  • truncation=True: cut over-long inputs
  • return_tensors="pt": return PyTorch tensors

The model step: pick the head by class name

Section titled “The model step: pick the head by class name”
ClassHead / output
AutoModelNone; raw hidden states (a vector per token)
AutoModelForSequenceClassificationClassify the whole input
AutoModelForTokenClassificationLabel each token (NER)
AutoModelForQuestionAnsweringAnswer-span start/end
AutoModelForCausalLMNext-token generation (decoder-only)
AutoModelForMaskedLMFill blanks (encoder-only)
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
outputs = model(**inputs) # outputs.logits
import torch
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
labels = model.config.id2label # {0: 'NEGATIVE', 1: 'POSITIVE'}

Models output logits (raw scores), not probabilities. Softmax normalizes; id2label names the columns.

SomeAutoClass.from_pretrained(checkpoint)

Loads tokenizers, base models, and task heads alike. Change the checkpoint string to change the model; change the head class to change the task.

  • Checkpoint: a model’s name on the Hub (e.g. distilbert-base-uncased-finetuned-sst-2-english), passed to from_pretrained.
  • Hidden states / features: the base transformer’s per-token output vectors, before any task head.
  • Head: the small layer(s) on top of the base model that produce task-specific output.
  • Logits: raw unnormalized model scores; softmax turns them into probabilities.
  • Hugging Face LLM Course, Chapter 2: “Using Transformers.” huggingface.co/learn/llm-course/chapter2. Released under Apache 2.0; this lesson mirrors its structure with original prose.