Hugging Face
Hugging Face transformers + datasets + accelerate make PyTorch the lingua franca of NLP. Pretrained models in three lines, fine-tuning with sensible defaults, evaluation on standard benchmarks, push to the Hub for sharing.
Pipeline, tokenizer, fine-tune, push
EXAMPLE
# pip install transformers datasets accelerate evaluate peft
import torch
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM,
Trainer, TrainingArguments, DataCollatorWithPadding, pipeline,
)
from datasets import load_dataset
import evaluate
# 1) Inference in 3 lines
classifier = pipeline('sentiment-analysis')
classifier('I love using PyTorch with Hugging Face!')
# [{'label': 'POSITIVE', 'score': 0.9998}]
# Many pipelines: 'text-classification', 'token-classification', 'question-answering',
# 'translation_xx_to_yy', 'summarization', 'fill-mask', 'text-generation',
# 'zero-shot-classification', 'image-classification', 'automatic-speech-recognition'
# 2) Custom model + tokenizer
tok = AutoTokenizer.from_pretrained('distilbert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
# 3) Tokenisation
batch = tok(['Hello world', 'Hugging Face is great.'], padding=True, truncation=True, return_tensors='pt')
logits = model(**batch).logits
probs = logits.softmax(dim=-1)
# 4) Load a dataset from the Hub
ds = load_dataset('imdb')
print(ds)
# DatasetDict { train, test, unsupervised }
# 5) Preprocess + map
def tokenize(batch):
return tok(batch['text'], truncation=True, max_length=256)
train = ds['train'].shuffle(seed=42).select(range(5_000)).map(tokenize, batched=True)
test = ds['test'].shuffle(seed=42).select(range(2_000)).map(tokenize, batched=True)
train = train.rename_column('label', 'labels')
test = test.rename_column('label', 'labels')
train.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
test.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
# 6) Trainer — sensible defaults
metric = evaluate.load('accuracy')
def compute_metrics(pred):
preds = pred.predictions.argmax(axis=-1)
return metric.compute(predictions=preds, references=pred.label_ids)
args = TrainingArguments(
output_dir='./out',
num_train_epochs=2,
per_device_train_batch_size=16,
per_device_eval_batch_size=32,
learning_rate=2e-5,
warmup_ratio=0.1,
weight_decay=0.01,
eval_strategy='epoch',
save_strategy='epoch',
load_best_model_at_end=True,
fp16=torch.cuda.is_available(),
push_to_hub=False, # set True to push final model
report_to=['none'], # or ['wandb','tensorboard']
)
trainer = Trainer(
model=model,
args=args,
train_dataset=train,
eval_dataset=test,
tokenizer=tok,
data_collator=DataCollatorWithPadding(tok),
compute_metrics=compute_metrics,
)
trainer.train()
trainer.evaluate()
trainer.save_model('./out/final')
# 7) Inference after fine-tuning
from transformers import pipeline
cls = pipeline('sentiment-analysis', model='./out/final', tokenizer='./out/final')
cls('What a fantastic movie!')
# 8) Push to the Hub
from huggingface_hub import login
login(token='hf_...')
trainer.push_to_hub('my-name/imdb-distilbert')
tok.push_to_hub('my-name/imdb-distilbert')
# 9) Working with bigger models — accelerate + bitsandbytes
from transformers import BitsAndBytesConfig
from peft import LoraConfig, get_peft_model
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
base = AutoModelForCausalLM.from_pretrained('meta-llama/Llama-3.1-8B', quantization_config=bnb, device_map='auto')
lora = LoraConfig(r=8, lora_alpha=16, target_modules=['q_proj', 'v_proj'], lora_dropout=0.05, task_type='CAUSAL_LM')
model = get_peft_model(base, lora)
model.print_trainable_parameters() # < 1% of weights
# 10) Generation
from transformers import AutoTokenizer, AutoModelForCausalLM
tok = AutoTokenizer.from_pretrained('gpt2')
mod = AutoModelForCausalLM.from_pretrained('gpt2')
inputs = tok('Once upon a time', return_tensors='pt')
out = mod.generate(
**inputs,
max_new_tokens=60,
do_sample=True,
temperature=0.7,
top_p=0.95,
repetition_penalty=1.2,
pad_token_id=tok.eos_token_id,
)
print(tok.decode(out[0], skip_special_tokens=True))
# 11) Datasets streaming for big data
stream = load_dataset('c4', 'en', split='train', streaming=True)
for i, ex in enumerate(stream):
if i >= 5: break
print(ex['text'][:200])
# 12) Audio + vision
from transformers import pipeline
asr = pipeline('automatic-speech-recognition', model='openai/whisper-tiny.en')
asr('audio.wav')
from transformers import ViTImageProcessor, ViTForImageClassification
proc = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224')
vit = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
# 13) Saving + loading
model.save_pretrained('./model')
tok.save_pretrained('./model')
AutoModelForSequenceClassification.from_pretrained('./model')
# 14) Tokenizer subtleties
# • truncation=True, max_length=N — required for batch processing
# • padding='max_length' for static shapes; padding=True (longest) for dynamic batches
// • return_overflowing_tokens=True — for long docs that exceed model max
// • Fast tokenisers (Rust-backed) are 10x faster — set use_fast=True (default)
# 15) Common bugs
# • Mismatched tokenizer + model — same name, both, always
# • Padding token missing on causal LMs (GPT-2) — set tok.pad_token = tok.eos_token
# • fp16 NaNs — try bf16 instead; or lower LR
# • OOM on small GPU — gradient_accumulation_steps + per_device_batch_size=1
# • Hub auth missing — `huggingface-cli login`
# • Trainer dataset NOT torch-formatted — set_format('torch', columns=[...])
# • Forgetting to call .eval() / model.train() at inference / training — Dropout/BatchNorm matter
# • Using `pipeline` for production at scale — load model + tokenizer once and write a thin server
Why it matters
Hugging Face is the path of least resistance for PyTorch NLP: pipeline for instant inference, Trainer + datasets for fine-tuning, peft + bitsandbytes for huge models on small GPUs, and the Hub for sharing. Match tokenizer and model checkpoints exactly, lean on the fast tokenisers, and graduate from pipeline to a slim server when latency matters.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tok = AutoTokenizer.from_pretrained('distilbert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
out = model(**tok('I love it', return_tensors='pt'))
Try it Yourself »
Discussion
Loading…