Flair NLP Framework

repository·master·Indexed 12 days ago

https://github.com/flairnlp/flair

A PyTorch-based NLP framework for state-of-the-art tasks including Named Entity Recognition (NER), sentiment analysis, and PoS tagging. It provides a text embedding library supporting Transformers and various word/document embeddings, featuring tools like ModelTrainer for multi-GPU training and the Sentence object for text processing.

Tokens
92.2K
Snippets
212
Records
247
Agent score
96%

What's inside Flair

  1. Explore Flair NLP task examples

    master

    The examples/ directory contains maintained examples for various NLP tasks. You can find specific implementations and usage patterns for:

    • Named Entity Recognition (NER): Located in the examples/ner/ directory.
    • Multi GPU usage: Located in the examples/multi_gpu/ directory.
  2. What is a Sentence and how to create one

    master

    A Sentence object is the primary container for text that you want to embed or tag in Flair. When you initialize a Sentence with a string, the text is automatically tokenized (segmented into words and punctuation) using the segtok library.

    You can provide a custom Tokenizer during initialization if you do not want to use the default.

    To create a sentence:

    from flair.data import Sentence
    
    sentence = Sentence('The grass is green.')
  3. Use PooledFlairEmbeddings for evolving word representations

    master

    PooledFlairEmbeddings provide a 'global' representation of each distinct word by using a pooling operation of all past occurrences. Unlike standard embeddings, these embeddings evolve over time, meaning the same word in the same sentence may have different embeddings at different points in time.

    Warning: PooledFlairEmbeddings are memory-intensive because they maintain past embeddings of all words in memory. For many use cases, regular FlairEmbeddings provide similar performance with significantly lower memory requirements.

    from flair.embeddings import PooledFlairEmbeddings
    from flair.data import Sentence
    
    # init embedding
    flair_embedding_forward = PooledFlairEmbeddings('news-forward')
    
    # create a sentence
    sentence = Sentence('The grass is green .')
    
    # embed words in sentence
    flair_embedding_forward.embed(sentence)
  4. Configure WordEmbeddings identifiers

    master

    The WordEmbeddings constructor accepts a string identifier to determine which pre-trained model to load.

    • Language-based: Use a two-letter language code (e.g., 'en', 'de', 'fr') to load FastText embeddings trained over Wikipedia/news data for that language.
    • Web Crawl variants: Append -crawl to a language code (e.g., 'de-crawl') to use FastText embeddings trained over web crawls.
    • Specific English models: Use identifiers like 'en-glove', 'en-extvec', 'en-twitter', or 'en-turian'.
    • Custom embeddings: Pass a file path to a .gensim formatted file (e.g., 'path/to/your/custom/embeddings.gensim').
  5. Access biomedical NER datasets in HunFlair

    master
    HunFlair integrates 31 biomedical named entity recognition (NER) datasets into a unified format. All dataset implementations are located in the flair.datasets.biomedical module. You can use the specific Data Set Class names to load these corpora for model development and evaluation.
  6. Combine multiple embeddings using StackedEmbeddings

    master

    Stacked embeddings allow you to combine multiple embedding types (e.g., combining static GloVe with contextual Flair embeddings) into a single representation. This is a highly recommended pattern for sequence labeling tasks.

    To use them, instantiate the StackedEmbeddings class by passing a list of existing embedding objects to its constructor. The resulting object behaves like a single embedding: calling .embed(sentence) on it will concatenate the vectors from all included embeddings into a single PyTorch vector for each token.

    from flair.embeddings import WordEmbeddings, FlairEmbeddings, StackedEmbeddings
    from flair.data import Sentence
    
    # 1. Initialize individual embeddings
    glove_embedding = WordEmbeddings('glove')
    flair_embedding_forward = FlairEmbeddings('news-forward')
    flair_embedding_backward = FlairEmbeddings('news-backward')
    
    # 2. Stack them together
    stacked_embeddings = StackedEmbeddings([
        glove_embedding,
        flair_embedding_forward,
        flair_embedding_backward,
    ])
    
    # 3. Use the stacked embedding on a sentence
    sentence = Sentence('The grass is green .')
    stacked_embeddings.embed(sentence)
    
    # The token.embedding is now a concatenation of all three vectors
    for token in sentence:
        print(token.embedding)
  7. Configure ELMo embedding combination strategies

    master

    ELMo word embeddings can be constructed by combining ELMo layers using different strategies. When initializing ELMoEmbeddings, you can specify how layers are combined:

    • "all": Use the concatenation of the three ELMo layers.
    • "top": Use the top ELMo layer.
    • "average": Use the average of the three ELMo layers.

    By default, the top 3 layers are concatenated.

  8. When to use fine-tuning vs. classic training

    master

    Flair provides two distinct training approaches depending on whether you are modifying a pre-trained language model or training a model from scratch (or training only a prediction head on top of frozen weights).

    Fine-Tuning

    Use fine-tuning when you are working with a pre-trained language model and want to adapt it to a specific task. In this approach, you add a prediction head with randomly initialized weights to a model that already has millions of trained parameters. Because most parameters are already optimized, you should use:

    • A very small learning rate (LR).
    • Just a few epochs.

    Use the ModelTrainer.fine_tune() method for this approach.

    Classic Training

    Use the classic training approach (also known as "feature-based" or "probing") if the majority of your trainable parameters are randomly initialized. This is common when:

    • You are training a model from scratch.
    • You have frozen the weights of a pre-trained language model, leaving only the randomly initialized prediction head as trainable.

    Because most parameters need to be learned from scratch, you should use:

    • A high learning rate.
    • Many epochs.

    Use the ModelTrainer.train() method for this approach.

  9. Use CharacterEmbeddings to add character-level features

    master

    CharacterEmbeddings allow you to incorporate character-level word embeddings into your model.

    Important Note: These embeddings are randomly initialized when the class is instantiated. They do not contain pre-trained information and are only meaningful once they have been trained on a specific downstream task.

    To make these features useful, you must include them in an embedding stack and pass that stack to a training method.

    from flair.embeddings import CharacterEmbeddings
    
    # CharacterEmbeddings are randomly initialized and require training
    embedding = CharacterEmbeddings()
  10. Ensure data consistency in Multi-GPU training

    master

    When using launch_distributed, all processes must use the exact same corpus and preprocessing. If your corpus initialization involves randomness, you must ensure consistency using one of these two methods:

    1. Set a global seed: Call flair.set_seed(seed_value) before initializing the corpus to ensure all processes generate the same data splits/order.
    2. Pre-initialize the corpus: Initialize the corpus object before calling launch_distributed and pass the object as an argument to the function. This allows the corpus to be serialized and shared across all spawned processes.
    import flair
    from flair.distributed_utils import launch_distributed
    
    # Method 1: Set seed
    flair.set_seed(42)
    
    def main():
        # corpus initialization happens here
        pass
    
    if __name__ == '__main__':
        launch_distributed(main)
  11. Choose the right NER model for your task

    master

    Flair provides several NER models optimized for different priorities:

    • Standard ('ner'): Good tradeoff between accuracy and speed. Uses Flair embeddings and recognizes 4 entity types.
    • Best Accuracy ('ner-large'): Uses a large transformer. Best for accuracy, but requires more memory and time. Supports multiple languages.
    • Fastest ('ner-fast'): Optimized for speed.
    • High Granularity ('ner-ontonotes-large'): Distinguishes between 18 different entity types (e.g., DATE, MONEY, WORK_OF_ART).
    • Biomedical ('bioner'): Specifically for biomedical data, detecting types like Disease, Gene, and Species.
    • Language Specific: Models like 'de-ner-large' (German) or 'ar-ner' (Arabic) are available for non-English text.