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

  1. Install the transformers and datasets libraries from pip.
  2. Load a pretrained model and tokenizer using AutoModel and AutoTokenizer.
  3. Prepare datasets using the datasets library or custom data loaders.
  4. Fine-tune with the Trainer API or by writing a custom PyTorch training loop.
  5. 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.