liveBook · Manning

Transformers

As explained in Domain-Specific Small Language Models

The Hugging Face library for working with pretrained transformer models, tokenizers, pipelines, and related training and inference utilities.

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

How Domain-Specific Small Language Models covers it

How deep it goes
Explains operational workflows and performance constraints, while deferring transformer internals and implementation details.
Examples
Uses DistilBERT question answering, GPT-Neo text generation, and BERT classification on an Android device.
What it assumes
Assumes Python and basic model-training concepts, while demonstrating tokenization, dataset preparation, batching, training, and inference workflows.
What you can do after
Apply it via Python to fine-tune, evaluate, generate text, classify inputs, and deploy supported models locally.
How it connects
Connects tokenizers, datasets, PyTorch, Accelerate, Optuna, quantization, hyperparameter search, and model deployment.

Also explained in

Compare how 6 books explain transformers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.