<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Flair Training Sequence Labeling Models
(C) 2019-2020 by Damir Cavar
Version
Step1:  If you want to use the CoNLL-03 corpus, you need to download it and unpack it in your Flair data and model folder. This folder should be in your home-directory and it is named .flair. Once you have downloaded the corpus, unpack it into a folder .flair/datasets/conll_03. If you do not want to use the CoNLL-03 corpus, but rather the free W-NUT 17 corpus, you can use the Flair command
Step2:  Declare the tag type to be predicted
Step3:  Create the tag-dictionary for the tag-type
Step4:  Load the embeddings
Step5:  Load and initialize the sequence tagger
Step6:  Load and initialize the trainer
Step7:  If you have a GPU (otherwise maybe tweak the batch size, etc.), run the training with 150 epochs
Step8:  Plot the training curves and results
Step9:  Use the model via the predict method
<ASSISTANT_TASK:>
Python Code:
from flair.data import Corpus
from flair.datasets import WNUT_17
from flair.embeddings import TokenEmbeddings, WordEmbeddings, StackedEmbeddings
from typing import List
Explanation: Flair Training Sequence Labeling Models
(C) 2019-2020 by Damir Cavar
Version: 0.3, February 2020
Download: This and various other Jupyter notebooks are available from my GitHub repo.
For the Flair tutorial 7 license and copyright restrictions, see the website below. For all the parts that I added, consider the license to be Creative Commons Attribution-ShareAlike 4.0 International License (CA BY-SA 4.0).
Based on the Flair Tutorial 7 Training a Model.
This tutorial is using the CoNLL-03 Named Entity Recognition data set. See this website for more details and to download an independent copy of the data set.
Training a Sequence Labeling Model
We will need the following modules:
End of explanation
corpus: Corpus = WNUT_17().downsample(0.1)
print(corpus)
Explanation: If you want to use the CoNLL-03 corpus, you need to download it and unpack it in your Flair data and model folder. This folder should be in your home-directory and it is named .flair. Once you have downloaded the corpus, unpack it into a folder .flair/datasets/conll_03. If you do not want to use the CoNLL-03 corpus, but rather the free W-NUT 17 corpus, you can use the Flair command: WNUT_17()
If you decide to download the CoNLL-03 corpus, adapt the following code. We load the W-NUT17 corpus and down-sample it to 10% of its size:
End of explanation
tag_type = 'ner'
Explanation: Declare the tag type to be predicted:
End of explanation
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
print(tag_dictionary)
Explanation: Create the tag-dictionary for the tag-type:
End of explanation
embedding_types: List[TokenEmbeddings] = [
    WordEmbeddings('glove'),
    # comment in this line to use character embeddings
    # CharacterEmbeddings(),
    # comment in these lines to use flair embeddings
    # FlairEmbeddings('news-forward'),
    # FlairEmbeddings('news-backward'),
]
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types)
Explanation: Load the embeddings:
End of explanation
from flair.models import SequenceTagger
tagger: SequenceTagger = SequenceTagger(hidden_size=256,
                                        embeddings=embeddings,
                                        tag_dictionary=tag_dictionary,
                                        tag_type=tag_type,
                                        use_crf=True)
Explanation: Load and initialize the sequence tagger:
End of explanation
from flair.trainers import ModelTrainer
trainer: ModelTrainer = ModelTrainer(tagger, corpus)
Explanation: Load and initialize the trainer:
End of explanation
trainer.train('resources/taggers/example-ner',
              learning_rate=0.1,
              mini_batch_size=32,
              max_epochs=150)
Explanation: If you have a GPU (otherwise maybe tweak the batch size, etc.), run the training with 150 epochs:
End of explanation
from flair.visual.training_curves import Plotter
plotter = Plotter()
plotter.plot_training_curves('resources/taggers/example-ner/loss.tsv')
plotter.plot_weights('resources/taggers/example-ner/weights.txt')
Explanation: Plot the training curves and results:
End of explanation
from flair.data import Sentence
model = SequenceTagger.load('resources/taggers/example-ner/final-model.pt')
sentence = Sentence('John lives in the Empire State Building .')
model.predict(sentence)
print(sentence.to_tagged_string())
Explanation: Use the model via the predict method:
End of explanation
<END_TASK>