Transformers is a Python library for loading pretrained transformer models, tokenizing inputs, fine-tuning them, and running inference. It provides high-level components such as AutoTokenizer, model classes, data collators, TrainingArguments, and Trainer, while integrating with datasets, PyTorch, Accelerate, and Optuna. section 2.3 section 3.2
Use the library to:
- Fine-tune a pretrained model on domain-specific data by splitting datasets, preprocessing and tokenizing examples, collating batches, configuring training, and calling Trainer.train(). section 2.3
- Automate training choices with hyperparameter search, checkpoint management, evaluation strategies, mixed precision, and early stopping. section 3.2
- Run inference across general-purpose and domain-specific models, balancing memory, latency, throughput, accuracy, and hardware constraints. section 4.1 section 4.4
- Deploy supported models locally on Android through Termux, Python virtual environments, PyTorch, and Transformers. section 11.4.3
Question-answering fine-tuning
Load a dataset, split it into training and test sets, tokenize questions and contexts with DistilBERT, map character answer spans to token positions, and train AutoModelForQuestionAnswering with Trainer. After training, tokenize a question about Juventus and extract the answer from the model’s start and end logits. section 2.3
import torch
from transformers import BertTokenizer, BertForSequenceClassification
model_id = 'google-bert/bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(model_id)
model = BertForSequenceClassification.from_pretrained(model_id)
<h1>Sample text</h1>
text = "That movie is a blockbuster"
<h1>Tokenize the text</h1>
inputs = tokenizer(text, return_tensors='pt')
<h1>Perform classification</h1>
with torch.no_grad():
outputs = model(<strong>inputs)
<h1>Get the predicted class</h1>
predicted_class = torch.argmax(outputs.logits)
print(f"Predicted class: {predicted_class}")
Shows a minimal on-device classification script: load BERT and its tokenizer, tokenize text, run inference without gradients, and select the predicted class. section 11.4.3





