Natural Language Processing with Flair

Book description

Learn how to solve practical NLP problems with the Flair Python framework, train sequence labeling models, work with text classifiers and word embeddings, and much more through hands-on practical exercises

Key Features

  • Backed by the community and written by an NLP expert
  • Get an understanding of basic NLP problems and terminology
  • Solve real-world NLP problems with Flair with the help of practical hands-on exercises

Book Description

Flair is an easy-to-understand natural language processing (NLP) framework designed to facilitate training and distribution of state-of-the-art NLP models for named entity recognition, part-of-speech tagging, and text classification. Flair is also a text embedding library for combining different types of embeddings, such as document embeddings, Transformer embeddings, and the proposed Flair embeddings.

Natural Language Processing with Flair takes a hands-on approach to explaining and solving real-world NLP problems. You'll begin by installing Flair and learning about the basic NLP concepts and terminology. You will explore Flair's extensive features, such as sequence tagging, text classification, and word embeddings, through practical exercises. As you advance, you will train your own sequence labeling and text classification models and learn how to use hyperparameter tuning in order to choose the right training parameters. You will learn about the idea behind one-shot and few-shot learning through a novel text classification technique TARS. Finally, you will solve several real-world NLP problems through hands-on exercises, as well as learn how to deploy Flair models to production.

By the end of this Flair book, you'll have developed a thorough understanding of typical NLP problems and you'll be able to solve them with Flair.

What you will learn

  • Gain an understanding of core NLP terminology and concepts
  • Get to grips with the capabilities of the Flair NLP framework
  • Find out how to use Flair's state-of-the-art pre-built models
  • Build custom sequence labeling models, embeddings, and classifiers
  • Learn about a novel text classification technique called TARS
  • Discover how to build applications with Flair and how to deploy them to production

Who this book is for

This Flair NLP book is for anyone who wants to learn about NLP through one of the most beginner-friendly, yet powerful Python NLP libraries out there. Software engineering students, developers, data scientists, and anyone who is transitioning into NLP and is interested in learning about practical approaches to solving problems with Flair will find this book useful. The book, however, is not recommended for readers aiming to get an in-depth theoretical understanding of the mathematics behind NLP. Beginner-level knowledge of Python programming is required to get the most out of this book.

Publisher resources

Download Example Code

Table of contents

  1. Natural Language Processing with Flair
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1: Understanding and Solving NLP with Flair
  7. Chapter 1: Introduction to Flair
    1. Technical requirements
    2. A brief introduction to NLP
      1. Tokenization
      2. Text vectorization
      3. Named entity recognition
      4. Word-sense disambiguation
      5. Part-of-speech tagging
      6. Chunking
      7. Stemming and lemmatization
      8. Text classification
    3. Introducing Flair
    4. Setting up the development environment
      1. Creating the virtual environment
      2. Installing a published version of Flair in a virtual environment
      3. Installing directly from the GitHub repository (optional)
      4. Running code that uses Flair
    5. Summary
  8. Chapter 2: Flair Base Types
    1. Technical requirements
    2. Sentence and Token objects
      1. Understanding the Sentence class
      2. Understanding the Token class
      3. Tokenization in Flair
      4. Sentence and Token object helper methods
    3. Using custom tokenizers
      1. Using the TokenizerWrapper class
    4. Understanding the Corpus object
    5. Summary
  9. Chapter 3: Embeddings in Flair
    1. Technical requirements
    2. Understanding word embeddings
      1. Cosine similarity
      2. The "king – man ≈ queen – woman" analogy
    3. Classic word embeddings in Flair
    4. Flair embeddings
      1. Understanding the contextuality of Flair embeddings
      2. Character-level sequence modeling in Flair embeddings
      3. Pooled Flair embeddings
      4. Available Flair embeddings
    5. Stacked embeddings
    6. Document embeddings
    7. Other embeddings in Flair
    8. Summary
  10. Chapter 4: Sequence Tagging
    1. Technical requirements
    2. Understanding sequence tagging in Flair
    3. Named entity recognition in Flair
      1. Types of NER taggers in Flair
    4. Part-of-speech tagging in Flair
      1. Part-of-speech tagging tag sets
      2. Types of PoS taggers in Flair
      3. Other tagging tasks in Flair
    5. Sequence labeling metrics
      1. Measuring accuracy for sequence labeling tasks
      2. Measuring the F1 score for sequence labeling tasks
    6. Summary
  11. Part 2: Deep Dive into Flair – Training Custom Models
  12. Chapter 5: Training Sequence Labeling Models
    1. Technical requirements
    2. The motivation behind training custom models
    3. Understanding the hardware requirements for training models
    4. Working with parameters for training and evaluation
      1. Understanding neural model training
      2. Parameters and terminology used in Flair model training
    5. Training custom sequence labeling models
      1. Loading a tagged corpus
      2. Loading the tag dictionary
      3. Building the embedding stack
      4. Initializing the SequenceTagger object
      5. Training the model
      6. Understanding training output files
      7. Loading and using custom Flair models
    6. Knowing when to stop and try again
      1. Monitoring loss
      2. Assessing and comparing performance metrics
    7. Summary
  13. Chapter 6: Hyperparameter Optimization in Flair
    1. Technical requirements
    2. Understanding hyperparameter tuning
      1. Optimization methods
    3. Hyperparameter tuning in Python
      1. Hyperparameter tuning in Python hands-on
    4. Hyperparameter optimization in Flair
      1. Hyperparameter tuning in Flair hands-on
    5. Summary
  14. Chapter 7: Train Your Own Embeddings
    1. Technical requirements
    2. Understanding the how and why behind custom Flair embeddings
      1. Why training embeddings rarely ever means training embeddings
      2. Corpora for Flair embeddings
      3. Dictionaries for training Flair embeddings
    3. Evaluating word embeddings
    4. Training Flair embeddings on the world's smallest language
      1. Preparing the dictionary
      2. Preparing the corpus
      3. Training the language model
      4. Using custom embeddings on downstream tasks
      5. Performing intrinsic evaluation on custom Flair embeddings
    5. Summary
  15. Chapter 8: Text Classification in Flair
    1. Technical requirements
    2. Understanding text classification
      1. Training text classifiers
    3. Text classification in Flair
      1. Using pre-trained Flair text classification models
      2. Document embeddings in Flair
      3. Text classifiers in Flair
    4. Training a text classifier in Flair
    5. Working with text classifiers that require little to no training data
      1. Zero-shot classification with TARS
      2. Few-shot classification with TARS
    6. Summary
  16. Part 3: Real-World Applications with Flair
  17. Chapter 9: Deploying and Using Models in Production
    1. Technical requirements
    2. Technical considerations for NLP models in production
      1. Client-side versus server-side inference
      2. When to use managed services and when to self-serve server-side models
      3. Managed services for Flair models
      4. A model deployment decision-making flowchart
    3. Self-serving flair models
      1. A proof-of-concept self-serve tool for Flair
    4. Using managed services for deploying and using Flair models
      1. The Hugging Face Models Hub
    5. Summary
  18. Chapter 10: Hands-On Exercise – Building a Trading Bot with Flair
    1. Technical requirements
    2. Understanding the trading strategy
    3. Implementing the Flair trading bot
    4. A Flair coding cheat sheet
    5. Summary
    6. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Natural Language Processing with Flair
  • Author(s): Tadej Magajna
  • Release date: April 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801072311