liveBook · Manning

Transformers

As explained in Natural Language Processing in Action, Second Edition

Hugging Face Transformers is an open-source library providing pretrained transformer models and utilities for natural-language and other machine-learning tasks.

Transformers is an open-source Python library for loading pretrained transformer models, tokenizers, and training utilities. It turns large-model capabilities into practical workflows for classification, sentiment analysis, moderation, semantic search, question answering, and text generation. section 9.3.3 section 10.2.1

A typical workflow starts with a pretrained model and its matching tokenizer. The tokenizer converts text into model-specific token IDs; the model returns encodings, logits, or generated token IDs that can be decoded back into text. section 10.2.1 section 9.3.3

Common library workflows include:

  • Load a pretrained BERT model with BertModel.from_pretrained('bert-base-uncased') and inspect its architecture. section 9.3.3
  • Fine-tune GPT-2 by wrapping tokenized lines in a PyTorch Dataset, splitting training and evaluation data, batching with a data collator, and training with Trainer. section 10.2.2

A minimal GPT-2 generation workflow

from transformers import GPT2Tokenizer, GPT2LMHeadModel tokenizer = GPT2Tokenizer.from_pretrained('gpt2') model = GPT2LMHeadModel.from_pretrained('gpt2') inputs = tokenizer.encode('NLP is', return_tensors='pt') outputs = model.generate(inputs, max_length=50) print(tokenizer.decode(outputs.squeeze(), skip_special_tokens=True)) The generated continuation may be fluent yet incorrect or repetitive, so outputs require evaluation rather than blind trust.

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Demonstrates installation, model loading, tokenization, generation, architecture inspection, and fine-tuning, but defers exhaustive compute planning.
Examples
Uses BERT architecture inspection, GPT-2 prompt generation, probability selection, nucleus sampling, and manuscript-line fine-tuning.
What it assumes
Assumes Python and PyTorch familiarity while teaching model-specific tokenization, dataset preparation, batching, and Trainer configuration.
What you can do after
Builds inference and fine-tuning pipelines that load pretrained models, generate text, inspect outputs, and train on custom datasets.
How it connects
Connects pretrained models with tokenization, PyTorch, attention, embeddings, inference, text generation, semantic search, and fine-tuning.

Also explained in

Compare how 6 books explain transformers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.