How to use Hugging Face Transformers
· Category: AI & Machine Learning
Short answer
Hugging Face Transformers provides pretrained models and easy-to-use APIs for natural language processing, computer vision, and audio tasks.
Steps
- Install the transformers and datasets libraries from pip.
- Load a pretrained model and tokenizer using AutoModel and AutoTokenizer.
- Prepare datasets using the datasets library or custom data loaders.
- Fine-tune with the Trainer API or by writing a custom PyTorch training loop.
- Push models to the Hugging Face Hub for versioning and collaboration.
Tips
- Use AutoClasses to simplify model selection without hardcoding architecture names.
- Apply data collators for dynamic padding during training.
- Use accelerate for distributed and mixed-precision training with minimal code changes.
- Explore the model hub for task-specific fine-tuned variants.
Common issues
- Tokenizer vocabulary mismatches when switching between model families.
- Out-of-memory errors from loading full model weights on limited GPU RAM.
- Slow inference when not using optimal batching or ONNX conversion.
- Difficulty customizing Trainer behavior for advanced training objectives.
Example
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')
inputs = tokenizer('Hello world', return_tensors='pt')
outputs = model(**inputs)
This example loads a pretrained BERT model and tokenizer, then runs a forward pass on sample text using PyTorch tensors.