Natural Language Processing with Transformers, Revised Edition

Book description

Since their introduction in 2017, transformers have quickly become the dominant architecture for achieving state-of-the-art results on a variety of natural language processing tasks. If you're a data scientist or coder, this practical book -now revised in full color- shows you how to train and scale these large models using Hugging Face Transformers, a Python-based deep learning library.

Transformers have been used to write realistic news stories, improve Google Search queries, and even create chatbots that tell corny jokes. In this guide, authors Lewis Tunstall, Leandro von Werra, and Thomas Wolf, among the creators of Hugging Face Transformers, use a hands-on approach to teach you how transformers work and how to integrate them in your applications. You'll quickly learn a variety of tasks they can help you solve.

  • Build, debug, and optimize transformer models for core NLP tasks, such as text classification, named entity recognition, and question answering
  • Learn how transformers can be used for cross-lingual transfer learning
  • Apply transformers in real-world scenarios where labeled data is scarce
  • Make transformer models efficient for deployment using techniques such as distillation, pruning, and quantization
  • Train transformers from scratch and learn how to scale to multiple GPUs and distributed environments

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Who Is This Book For?
    2. What You Will Learn
    3. Software and Hardware Requirements
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
      1. Lewis
      2. Leandro
      3. Thomas
  3. 1. Hello Transformers
    1. The Encoder-Decoder Framework
    2. Attention Mechanisms
    3. Transfer Learning in NLP
    4. Hugging Face Transformers: Bridging the Gap
    5. A Tour of Transformer Applications
      1. Text Classification
      2. Named Entity Recognition
      3. Question Answering
      4. Summarization
      5. Translation
      6. Text Generation
    6. The Hugging Face Ecosystem
      1. The Hugging Face Hub
      2. Hugging Face Tokenizers
      3. Hugging Face Datasets
      4. Hugging Face Accelerate
    7. Main Challenges with Transformers
    8. Conclusion
  4. 2. Text Classification
    1. The Dataset
      1. A First Look at Hugging Face Datasets
      2. From Datasets to DataFrames
      3. Looking at the Class Distribution
      4. How Long Are Our Tweets?
    2. From Text to Tokens
      1. Character Tokenization
      2. Word Tokenization
      3. Subword Tokenization
      4. Tokenizing the Whole Dataset
    3. Training a Text Classifier
      1. Transformers as Feature Extractors
      2. Fine-Tuning Transformers
    4. Conclusion
  5. 3. Transformer Anatomy
    1. The Transformer Architecture
    2. The Encoder
      1. Self-Attention
      2. The Feed-Forward Layer
      3. Adding Layer Normalization
      4. Positional Embeddings
      5. Adding a Classification Head
    3. The Decoder
    4. Meet the Transformers
      1. The Transformer Tree of Life
      2. The Encoder Branch
      3. The Decoder Branch
      4. The Encoder-Decoder Branch
    5. Conclusion
  6. 4. Multilingual Named Entity Recognition
    1. The Dataset
    2. Multilingual Transformers
    3. A Closer Look at Tokenization
      1. The Tokenizer Pipeline
      2. The SentencePiece Tokenizer
    4. Transformers for Named Entity Recognition
    5. The Anatomy of the Transformers Model Class
      1. Bodies and Heads
      2. Creating a Custom Model for Token Classification
      3. Loading a Custom Model
    6. Tokenizing Texts for NER
    7. Performance Measures
    8. Fine-Tuning XLM-RoBERTa
    9. Error Analysis
    10. Cross-Lingual Transfer
      1. When Does Zero-Shot Transfer Make Sense?
      2. Fine-Tuning on Multiple Languages at Once
    11. Interacting with Model Widgets
    12. Conclusion
  7. 5. Text Generation
    1. The Challenge with Generating Coherent Text
    2. Greedy Search Decoding
    3. Beam Search Decoding
    4. Sampling Methods
    5. Top-k and Nucleus Sampling
    6. Which Decoding Method Is Best?
    7. Conclusion
  8. 6. Summarization
    1. The CNN/DailyMail Dataset
    2. Text Summarization Pipelines
      1. Summarization Baseline
      2. GPT-2
      3. T5
      4. BART
      5. PEGASUS
    3. Comparing Different Summaries
    4. Measuring the Quality of Generated Text
      1. BLEU
      2. ROUGE
    5. Evaluating PEGASUS on the CNN/DailyMail Dataset
    6. Training a Summarization Model
      1. Evaluating PEGASUS on SAMSum
      2. Fine-Tuning PEGASUS
      3. Generating Dialogue Summaries
    7. Conclusion
  9. 7. Question Answering
    1. Building a Review-Based QA System
      1. The Dataset
      2. Extracting Answers from Text
      3. Using Haystack to Build a QA Pipeline
    2. Improving Our QA Pipeline
      1. Evaluating the Retriever
      2. Evaluating the Reader
      3. Domain Adaptation
      4. Evaluating the Whole QA Pipeline
    3. Going Beyond Extractive QA
    4. Conclusion
  10. 8. Making Transformers Efficient in Production
    1. Intent Detection as a Case Study
    2. Creating a Performance Benchmark
    3. Making Models Smaller via Knowledge Distillation
      1. Knowledge Distillation for Fine-Tuning
      2. Knowledge Distillation for Pretraining
      3. Creating a Knowledge Distillation Trainer
      4. Choosing a Good Student Initialization
      5. Finding Good Hyperparameters with Optuna
      6. Benchmarking Our Distilled Model
    4. Making Models Faster with Quantization
    5. Benchmarking Our Quantized Model
    6. Optimizing Inference with ONNX and the ONNX Runtime
    7. Making Models Sparser with Weight Pruning
      1. Sparsity in Deep Neural Networks
      2. Weight Pruning Methods
    8. Conclusion
  11. 9. Dealing with Few to No Labels
    1. Building a GitHub Issues Tagger
      1. Getting the Data
      2. Preparing the Data
      3. Creating Training Sets
      4. Creating Training Slices
    2. Implementing a Naive Bayesline
    3. Working with No Labeled Data
    4. Working with a Few Labels
      1. Data Augmentation
      2. Using Embeddings as a Lookup Table
      3. Fine-Tuning a Vanilla Transformer
      4. In-Context and Few-Shot Learning with Prompts
    5. Leveraging Unlabeled Data
      1. Fine-Tuning a Language Model
      2. Fine-Tuning a Classifier
      3. Advanced Methods
    6. Conclusion
  12. 10. Training Transformers from Scratch
    1. Large Datasets and Where to Find Them
      1. Challenges of Building a Large-Scale Corpus
      2. Building a Custom Code Dataset
      3. Working with Large Datasets
      4. Adding Datasets to the Hugging Face Hub
    2. Building a Tokenizer
      1. The Tokenizer Model
      2. Measuring Tokenizer Performance
      3. A Tokenizer for Python
      4. Training a Tokenizer
      5. Saving a Custom Tokenizer on the Hub
    3. Training a Model from Scratch
      1. A Tale of Pretraining Objectives
      2. Initializing the Model
      3. Implementing the Dataloader
      4. Defining the Training Loop
      5. The Training Run
    4. Results and Analysis
    5. Conclusion
  13. 11. Future Directions
    1. Scaling Transformers
      1. Scaling Laws
      2. Challenges with Scaling
      3. Attention Please!
      4. Sparse Attention
      5. Linearized Attention
    2. Going Beyond Text
      1. Vision
      2. Tables
    3. Multimodal Transformers
      1. Speech-to-Text
      2. Vision and Text
    4. Where to from Here?
  14. Index
  15. About the Authors

Product information

  • Title: Natural Language Processing with Transformers, Revised Edition
  • Author(s): Lewis Tunstall, Leandro von Werra, Thomas Wolf
  • Release date: May 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098136796