Python / Python Modern Generative AI and Agents Interview Questions
How do you fine-tune a model using the Hugging Face Trainer API?
The Trainer class encapsulates the standard training loop — batching, gradient accumulation, mixed precision, evaluation, checkpointing, logging to TensorBoard/WandB — behind a clean API. Combined with TrainingArguments, it handles most production training concerns so you can focus on data preparation and model selection rather than boilerplate.
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification,
TrainingArguments, Trainer, DataCollatorWithPadding
)
from datasets import load_dataset
import evaluate
import numpy as np
model_name = 'distilbert-base-uncased'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
model_name, num_labels=2
)
# Tokenise IMDB dataset
ds = load_dataset('imdb')
def tokenize(batch):
return tokenizer(batch['text'], truncation=True, max_length=512)
tokenized = ds.map(tokenize, batched=True, remove_columns=['text'])
# Metric
accuracy_metric = evaluate.load('accuracy')
def compute_metrics(eval_pred):
logits, labels = eval_pred
preds = np.argmax(logits, axis=-1)
return accuracy_metric.compute(predictions=preds, references=labels)
# Training configuration
args = TrainingArguments(
output_dir='./distilbert-imdb',
num_train_epochs=3,
per_device_train_batch_size=32,
per_device_eval_batch_size=64,
learning_rate=2e-5,
weight_decay=0.01,
evaluation_strategy='epoch',
save_strategy='epoch',
load_best_model_at_end=True,
fp16=True, # mixed precision
logging_steps=50,
report_to='none', # or 'wandb' / 'tensorboard'
)
trainer = Trainer(
model=model,
args=args,
train_dataset=tokenized['train'],
eval_dataset=tokenized['test'],
tokenizer=tokenizer,
data_collator=DataCollatorWithPadding(tokenizer), # dynamic padding
compute_metrics=compute_metrics,
)
trainer.train()
trainer.save_model('./final-model')
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
