Work with state-of-the-art machine learning models for NLP, computer vision, audio, and multimodal tasks using HuggingFace Transformers. This skill should be used when fine-tuning pre-trained models, performing inference with pipelines, generating text, training sequence models, or working with BERT, GPT, T5, ViT, and other transformer architectures. Covers model loading, tokenization, training with Trainer API, text generation strategies, and task-specific patterns for classification, NER, QA, summarization, translation, and image tasks. (plugin:scientific-packages@claude-scientific-skills)
Work with state-of-the-art machine learning models for NLP, computer vision, audio, and multimodal tasks using HuggingFace Transformers. This skill should be used when fine-tuning pre-trained models, performing inference with pipelines, generating text, training sequence models, or working with BERT, GPT, T5, ViT, and other transformer architectures. Covers model loading, tokenization, training with Trainer API, text generation strategies, and task-specific patterns for classification, NER, QA, summarization, translation, and image tasks. (plugin:scientific-packages@claude-scientific-skills)
Transformers
Overview
The Transformers library provides state-of-the-art machine learning models for NLP, computer vision, audio, and multimodal tasks. Apply this skill for quick inference through pipelines, comprehensive training via the Trainer API, and flexible text generation with various decoding strategies.
Core Capabilities
1. Quick Inference with Pipelines
For rapid inference without complex setup, use the pipeline() API. Pipelines abstract away tokenization, model invocation, and post-processing.
from transformers import pipeline
# Text classification
classifier = pipeline("text-classification")
result = classifier("This product is amazing!")
# Named entity recognition
ner = pipeline("token-classification")
entities = ner("Sarah works at Microsoft in Seattle")
# Question answering
qa = pipeline("question-answering")
answer = qa(question="What is the capital?", context="Paris is the capital of France.")
generator = pipeline(, model=)
text = generator(, max_length=)
image_classifier = pipeline()
predictions = image_classifier()
For comprehensive generation documentation, see references/generation_strategies.md.
4. Task-Specific Patterns
Common task patterns with appropriate model classes:
Text Classification:
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(
"bert-base-uncased",
num_labels=3,
id2label={0: "negative", 1: "neutral", 2: "positive"}
)
Named Entity Recognition (Token Classification):
from transformers import AutoModelForTokenClassification
model = AutoModelForTokenClassification.from_pretrained(
"bert-base-uncased",
num_labels=9# Number of entity types
)
Question Answering:
from transformers import AutoModelForQuestionAnswering
model = AutoModelForQuestionAnswering.from_pretrained("bert-base-uncased")
Summarization and Translation (Seq2Seq):
from transformers import AutoModelForSeq2SeqLM
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
Image Classification:
from transformers import AutoModelForImageClassification
model = AutoModelForImageClassification.from_pretrained(
"google/vit-base-patch16-224",
num_labels=num_classes
)
For detailed task-specific workflows including data preprocessing, training, and evaluation, see references/task_patterns.md.
Auto Classes
Use Auto classes for automatic architecture selection based on model checkpoints:
from transformers import (
AutoTokenizer, # Tokenization
AutoModel, # Base model (hidden states)
AutoModelForSequenceClassification,
AutoModelForTokenClassification,
AutoModelForQuestionAnswering,
AutoModelForCausalLM, # GPT-style
AutoModelForMaskedLM, # BERT-style
AutoModelForSeq2SeqLM, # T5, BART
AutoProcessor, # For multimodal models
AutoImageProcessor, # For vision models
)
# Load any model by name
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
For comprehensive API documentation, see references/api_reference.md.
Model Loading and Optimization
Device placement:
model = AutoModel.from_pretrained("bert-base-uncased", device_map="auto")
Mixed precision:
model = AutoModel.from_pretrained(
"model-name",
torch_dtype=torch.float16 # or torch.bfloat16
)
Quantization:
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16
)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=quantization_config,
device_map="auto"
)
Common Workflows
Quick Inference Workflow
Choose appropriate pipeline for task
Load pipeline with optional model specification
Pass inputs and get results
For batch processing, pass list of inputs
See:scripts/quick_inference.py for comprehensive pipeline examples
Training Workflow
Load and preprocess dataset using 🤗 Datasets
Tokenize data with appropriate tokenizer
Load pre-trained model for specific task
Configure TrainingArguments
Create Trainer with model, data, and compute_metrics
Train with trainer.train()
Evaluate with trainer.evaluate()
Save model and optionally push to Hub
See:scripts/fine_tune_classifier.py for complete training example