Hugging Face Transformers Cheat Sheet
Hugging Face Transformers reference covering the pipeline API, AutoTokenizer/AutoModel classes, and fine-tuning with the Trainer API.
2 PagesIntermediateApr 2, 2026
Pipeline API
Zero-setup inference for common tasks.
python
from transformers import pipelineclassifier = pipeline("sentiment-analysis")result = classifier("I love using transformers!")# [{'label': 'POSITIVE', 'score': 0.9998}]generator = pipeline("text-generation", model="gpt2")generator("Once upon a time", max_length=30, num_return_sequences=1)qa = pipeline("question-answering")qa(question="Who built the library?", context="Hugging Face built Transformers.")
Tokenizer & Model
Lower-level access for custom workflows.
python
from transformers import AutoTokenizer, AutoModelForSequenceClassificationimport torchtokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)inputs = tokenizer("Hello world!", return_tensors="pt", padding=True, truncation=True)with torch.no_grad(): outputs = model(**inputs)logits = outputs.logitsprobs = torch.softmax(logits, dim=-1)
Fine-Tuning with Trainer
Train on a custom dataset.
python
from transformers import TrainingArguments, Trainertraining_args = TrainingArguments( output_dir="./results", per_device_train_batch_size=16, num_train_epochs=3, learning_rate=2e-5, logging_steps=50,)trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset,)trainer.train()metrics = trainer.evaluate()
Key Classes
Core building blocks of the library.
- AutoTokenizer- loads the correct tokenizer for any model checkpoint
- AutoModel / AutoModelForXxx- loads architecture + weights matched to a task
- pipeline()- highest-level API for inference in a few lines
- Trainer / TrainingArguments- training loop with logging, checkpointing, evaluation
- Dataset (datasets library)- efficient, memory-mapped dataset loading
- save_pretrained() / from_pretrained()- persist and reload models/tokenizers/configs
Pro Tip
Always load the tokenizer and model with the same checkpoint name via from_pretrained() — mismatched tokenizer/model vocabularies silently produce garbage predictions instead of raising an error.
Was this cheat sheet helpful?
Explore Topics
#HuggingFaceTransformers#HuggingFaceTransformersCheatSheet#DataScience#Intermediate#PipelineAPI#TokenizerModel#FineTuningWithTrainer#KeyClasses#OOP#MachineLearning#APIs#DevOps#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance