def test_plotting_training_curves_and_weights(resources_path): plotter = Plotter() plotter.plot_training_curves(resources_path / "visual/loss.tsv") plotter.plot_weights(resources_path / "visual/weights.txt") # clean up directory (resources_path / "visual/weights.png").unlink() (resources_path / "visual/training.png").unlink()
def test_plotting_training_curves_and_weights(): plotter = Plotter() plotter.plot_training_curves('./resources/visual/loss.tsv') plotter.plot_weights('./resources/visual/weights.txt') # clean up directory os.remove('./resources/visual/weights.png') os.remove('./resources/visual/training.png')
def plot_curve(self, traing_curve_path=os.path.normpath( r'./resources/taggers/slow_bert/loss.tsv'), weights_path=os.path.normpath( r'./resources/taggers/slow_bert/loss.tsv')): from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves(traing_curve_path) plotter.plot_weights(weights_path)
def main(args): args = parser.parse_args() # 1. get the corpus corpus: TaggedCorpus = NLPTaskDataFetcher.load_classification_corpus( args.data_dir[0], train_file='train.txt', dev_file='dev.txt', test_file='test.txt') # 2. create the label dictionary label_dict = corpus.make_label_dictionary() # 3. make a list of word embeddings word_embeddings = [ WordEmbeddings('glove'), # comment in flair embeddings for state-of-the-art results # FlairEmbeddings('news-forward'), # FlairEmbeddings('news-backward'), # ELMoEmbeddings() ] # 4. init document embedding by passing list of word embeddings document_embeddings: DocumentLSTMEmbeddings = DocumentLSTMEmbeddings( word_embeddings, hidden_size=128, reproject_words=True, reproject_words_dimension=64, ) # 5. create the text classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, multi_label=False) # 6. initialize the text classifier trainer trainer = ModelTrainer(classifier, corpus) # 7. start the training model_out = 'resources/classifiers/sentence-classification/glove' trainer.train(model_out, learning_rate=0.1, mini_batch_size=32, anneal_factor=0.5, patience=5, max_epochs=100) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves(join(model_out, 'loss.tsv')) plotter.plot_weights(join(model_out, 'weights.txt'))
def __init__(self, corpus_name: str): corpus = NLPTaskDataFetcher.load_column_corpus( loc.abs_path([loc.ASSETS, loc.MODELS, loc.DIRKSON]), { 0: 'text', 1: 'ner' }, train_file=corpus_name + loc.DIRKSON_VALIDATION_TXT, test_file=corpus_name + loc.DIRKSON_TEST_TXT) embedding_types = [ BertEmbeddings('bert-base-uncased'), FlairEmbeddings('mix-forward'), FlairEmbeddings('mix-backward') ] tag_type = 'ner' embeddings = StackedEmbeddings(embeddings=embedding_types) tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) trainer: ModelTrainer = ModelTrainer(tagger, corpus) if not path.exists: os.mkdir( loc.abs_path( [loc.ASSETS, loc.MODELS, loc.DIRKSON, corpus_name])) trainer.train(loc.abs_path( [loc.ASSETS, loc.MODELS, loc.DIRKSON, corpus_name]), learning_rate=0.1, mini_batch_size=32, max_epochs=150) plotter = Plotter() plotter.plot_training_curves( loc.abs_path([ loc.ASSETS, loc.MODELS, loc.DIRKSON, corpus_name, loc.LOSS_TSV ])) plotter.plot_weights( loc.abs_path([ loc.ASSETS, loc.MODELS, loc.DIRKSON, corpus_name, loc.WEIGHTS_TXT ]))
def train(self, trainfile, devfile, testfile, resfolder, embtype="bert", chunk_len=100, batch_len=8): """ *** This method can be used to train new models with the settings used in project Redewiedergabe It is not accessible from rwtagger_script and not documented in detail. Use at your own risk. ;-) *** :param trainfile: :param devfile: :param testfile: :param resfolder: :param embtype: :param chunk_len: :param batch_len: :return: """ emb_name, embeddings = self._get_embeddings(embtype) corpus: Corpus = self.create_corpus(trainfile, devfile, testfile, chunk_len) tag_dictionary = corpus.make_tag_dictionary(tag_type="cat") if not os.path.exists(resfolder): os.makedirs(resfolder) tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type="cat", use_crf=True, rnn_layers=2) trainer: ModelTrainer = ModelTrainer(tagger, corpus) trainer.train(resfolder, learning_rate=0.1, mini_batch_size=batch_len, max_epochs=150, checkpoint=True) # plot training curves plotter = Plotter() plotter.plot_training_curves(os.path.join(resfolder, 'loss.tsv')) plotter.plot_weights(os.path.join(resfolder, 'weights.txt'))
def train_sequence_labeling_model(data_folder, proposed_tags_vocabulary_size): # define columns columns = {0: 'text', 1: 'pos', 2: 'is_separator', 3: 'proposed_tags'} # init a corpus using column format, data folder and the names of the train and test files # 1. get the corpus corpus: Corpus = ColumnCorpus(data_folder, columns, train_file='train', test_file='test', dev_file=None) log.info(corpus) # 2. what tag do we want to predict tag_type = 'pos' # 3. make the tag dictionary from the corpus tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) log.info(tag_dictionary) # 4. initialize embeddings local_model_path = use_scratch_dir_if_available('resources/polish_FastText_embeddings') embedding_types: List[TokenEmbeddings] = [ FlairEmbeddings('pl-forward', chars_per_chunk=64), FlairEmbeddings('pl-backward', chars_per_chunk=64), OneHotEmbeddings(corpus=corpus, field='is_separator', embedding_length=3, min_freq=3), OneHotEmbeddings(corpus=corpus, field='proposed_tags', embedding_length=math.ceil((proposed_tags_vocabulary_size + 1)**0.25), min_freq=3), WordEmbeddings(local_model_path) if os.path.exists(local_model_path) else WordEmbeddings('pl') ] embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # 5. initialize sequence tagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=False, rnn_layers=2) # 6. initialize trainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train(use_scratch_dir_if_available('resources_pol_eval/taggers/example-pos/'), learning_rate=0.1, mini_batch_size=32, embeddings_storage_mode='gpu', max_epochs=sys.maxsize, monitor_test=True) # 8. plot weight traces (optional) plotter = Plotter() plotter.plot_weights(use_scratch_dir_if_available('resources_pol_eval/taggers/example-pos/weights.txt'))
def train(data_folder, model_output_folder): corpus: TaggedCorpus = NLPTaskDataFetcher.load_corpus( NLPTask.CONLL_03, base_path=data_folder) # 2. what tag do we want to predict? tag_type = 'ner' # 3. make the tag dictionary from the corpus tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) print(tag_dictionary.idx2item) # init Flair embeddings flair_forward_embedding = FlairEmbeddings('multi-forward') flair_backward_embedding = FlairEmbeddings('multi-backward') # init multilingual BERT bert_embedding = BertEmbeddings('bert-base-multilingual-cased') # 4. initialize embeddings embedding_types: List[TokenEmbeddings] = [ flair_forward_embedding, flair_backward_embedding, bert_embedding ] embeddings: StackedEmbeddings = StackedEmbeddings( embeddings=embedding_types) # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type) # 6. initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train(model_output_folder, mini_batch_size=256, max_epochs=150) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves(model_output_folder + '/loss.tsv') plotter.plot_weights(model_output_folder + '/weights.txt')
def train( self, output_dir: Union[Path, str], learning_rate: float = 0.07, mini_batch_size: int = 32, anneal_factor: float = 0.5, patience: int = 5, max_epochs: int = 150, plot_weights: bool = False, **kwargs, ) -> None: """ Train the Sequence Classifier * **output_dir** - The output directory where the model predictions and checkpoints will be written. * **learning_rate** - The initial learning rate * **mini_batch_size** - Batch size for the dataloader * **anneal_factor** - The factor by which the learning rate is annealed * **patience** - Patience is the number of epochs with no improvement the Trainer waits until annealing the learning rate * **max_epochs** - Maximum number of epochs to train. Terminates training if this number is surpassed. * **plot_weights** - Bool to plot weights or not * **kwargs** - Keyword arguments for the rest of Flair's `Trainer.train()` hyperparameters """ if isinstance(output_dir, str): output_dir = Path(output_dir) # Start the training self.trainer.train( output_dir, learning_rate=learning_rate, mini_batch_size=mini_batch_size, anneal_factor=anneal_factor, patience=patience, max_epochs=max_epochs, **kwargs, ) # Plot weight traces if plot_weights: plotter = Plotter() plotter.plot_weights(output_dir / "weights.txt")
def main(): train_dev_corpus = NLPTaskDataFetcher.load_classification_corpus( Path(DATA_PATH), train_file='flair_train.csv', test_file='flair_test.csv', dev_file='flair_dev.csv') label_dict = train_dev_corpus.make_label_dictionary() word_embeddings = [ WordEmbeddings('crawl'), FlairEmbeddings('news-forward-fast', chars_per_chunk=128), FlairEmbeddings('news-backward-fast', chars_per_chunk=128) ] document_embeddings = DocumentRNNEmbeddings(word_embeddings, rnn_type='LSTM', hidden_size=128, reproject_words=True, reproject_words_dimension=64) classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, multi_label=False) trainer = ModelTrainer(classifier, train_dev_corpus) trainer.train(PRETRAINED_FLAIR, max_epochs=40, learning_rate=0.2, mini_batch_size=32, embeddings_in_memory=False, checkpoint=True) plotter = Plotter() plotter.plot_training_curves(FLAIR_LOSS) plotter.plot_weights(FLAIR_WEIGHTS)
# Just replace the names of model and print the details of training and weights from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves('FLAIR/resources/taggers/flairpos1/loss.tsv') plotter.plot_weights('FLAIR/resources/taggers/flairpos1/weights.txt')
reproject_words_dimension=256, ) # 5. create the text classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict) # 6. initialize the text classifier trainer trainer = ModelTrainer(classifier, corpus) # 7. start the training trainer.train('/home/anna/Desktop/markup/8', learning_rate=0.1, mini_batch_size=32, anneal_factor=0.5, patience=5, max_epochs=150) # 8. plot weight traces (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_weights('/home/anna/Desktop/markup/8/weights.txt') classifier = TextClassifier.load('/home/anna/Desktop/markup/8/final-model.pt') # create example sentences sentence = Sentence('France is the current world cup winner.') # predict class and print classifier.predict(sentence) print(sentence.labels)
# CharLMEmbeddings('news-forward'), # CharLMEmbeddings('news-backward'), ] embeddings = WordEmbeddings("tmp/glove.bin") # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=1024, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import SequenceTaggerTrainer trainer: SequenceTaggerTrainer = SequenceTaggerTrainer(tagger, corpus) # 7. start training trainer.train('resources/taggers/example-ner', learning_rate=0.1, mini_batch_size=8, max_epochs=150) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves('resources/taggers1/example-ner/loss.tsv') plotter.plot_weights('resources/taggers1/example-ner/weights.txt')
) # 5. create the text classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict) # 6. initialize the text classifier trainer trainer = ModelTrainer(classifier, corpus) # 7. start the training trainer.train('resources/taggers/ag_news', learning_rate=0.1, mini_batch_size=32, anneal_factor=0.5, patience=5, max_epochs=150) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves('resources/taggers/ag_news/loss.tsv') plotter.plot_weights('resources/taggers/ag_news/weights.txt') classifier = TextClassifier.load('resources/taggers/ag_news/final-model.pt') # create example sentence sentence = Sentence('France is the current world cup winner.') # predict tags and print classifier.predict(sentence) print(sentence.labels)
def trainer(file_path: Path, filenames: Tuple[str, str, str], checkpoint: str, stack: str, n_epochs: int) -> None: """Train sentiment model using Flair NLP library: https://github.com/zalandoresearch/flair/blob/master/resources/docs/TUTORIAL_7_TRAINING_A_MODEL.md To help provide added context, we can stack Glove, Bert or ELMo embeddings along with Flair embeddings. """ # pip install flair allennlp from flair.datasets import ClassificationCorpus from flair.embeddings import FlairEmbeddings, DocumentRNNEmbeddings, DocumentPoolEmbeddings from flair.models import TextClassifier from flair.trainers import ModelTrainer from flair.training_utils import EvaluationMetric from flair.visual.training_curves import Plotter if stack == "glove": from flair.embeddings import WordEmbeddings stacked_embedding = WordEmbeddings('glove') elif stack == "fasttext": from flair.embeddings import WordEmbeddings stacked_embedding = WordEmbeddings('it') elif stack == "elmo": from flair.embeddings import ELMoEmbeddings stacked_embedding = ELMoEmbeddings('original') elif stack == "bert": from flair.embeddings import BertEmbeddings stacked_embedding = BertEmbeddings('bert-base-uncased') elif stack == "bert-multi": from flair.embeddings import BertEmbeddings stacked_embedding = BertEmbeddings('bert-base-multilingual-uncased') elif stack == 'bpe': from flair.embeddings import BytePairEmbeddings stacked_embedding = BytePairEmbeddings('it') else: stacked_embedding = None # Define and Load corpus from the provided dataset train, dev, test = filenames corpus = ClassificationCorpus( file_path, train_file=train, dev_file=dev, test_file=test, ) # Create label dictionary from provided labels in data label_dict = corpus.make_label_dictionary() # Stack Flair string-embeddings with optional embeddings word_embeddings = list( filter(None, [ stacked_embedding, FlairEmbeddings('it-forward'), FlairEmbeddings('it-backward'), ])) # Initialize document embedding by passing list of word embeddings document_embeddings = DocumentRNNEmbeddings( word_embeddings, hidden_size=256, reproject_words=True, dropout=0.5, reproject_words_dimension=256, ) #document_embeddings = DocumentPoolEmbeddings([ # stacked_embedding, # FlairEmbeddings('it-forward'), # FlairEmbeddings('it-backward')],pooling='mean') # Define classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, multi_label=True) if not checkpoint: trainer = ModelTrainer(classifier, corpus) else: # If checkpoint file is defined, resume training #checkpoint = classifier.load_checkpoint(Path(checkpoint)) trainer = ModelTrainer.load_checkpoint(checkpoint, corpus) # Begin training (enable checkpointing to continue training at a later time, if desired) trainer.train( file_path, max_epochs=n_epochs, checkpoint=True, ) # Plot curves and store weights and losses plotter = Plotter() plotter.plot_training_curves(file_path + '/loss.tsv') plotter.plot_weights(file_path + '/weights.txt')
class SequenceTaggerEvaluation(): def __init__(self, path: Union[Path, str], model: str = 'final-model.pt'): if type(path) == str: path = Path(path) assert path.exists() self.path = path self.model = SequenceTagger.load(path / model) self.cv_results = {} for file in ['summary', 'details']: try: self.cv_results[file] = pickle.load( (path / (file + '.pkl')).open(mode='rb')) except FileNotFoundError: print( f"{file+'.pkl'} not found. Setting cv_results['{file}'] to None" ) self.plotter = Plotter() def result_tables(self, save_as_html: bool = True): html_0 = self.cv_results['summary'].to_frame('value').to_html() html_1 = self.cv_results['details'].to_html() display(HTML(html_0)) print('\n') display(HTML(html_1)) if save_as_html: (self.path / 'summary.html').write_text(html_0) (self.path / 'details.html').write_text(html_1) def plot_tag_stats(self, mode: str, savefig: bool = False, **kwargs): """ mode tp-fn: stacked barplot - true-positives and false-negatives tp-fp: bar plot - true-positives and false-positives """ details = self.cv_results['details'] if mode == 'tp_fn': details[['true-positive', 'false-negative']].plot.bar(stacked=True, **kwargs) elif mode == 'tp_fp': details[['true-positive', 'false-positive']].plot.bar(stacked=False, **kwargs) else: details[mode.split('_')].plot.bar(stacked=False, **kwargs) plt.gca().yaxis.grid(True, linestyle='--') plt.tight_layout() if savefig: plt.savefig(self.path / (mode + '.png')) def confusion_matrix(self, ): # confusion matrix tags pass def predict(self, sentences: Union[str, Sentence, List[Sentence], List[str]], display_html: bool = True, html_file: str = None, display_str: bool = False, **kwargs): if type(sentences) == Sentence: sentences = [sentences] elif type(sentences) == str: sentences = split_single(sentences) if type(sentences[0]) == str: sentences = [Sentence(s, use_tokenizer=True) for s in sentences] self.model.predict(sentences) if display_html or html_file: html = render_ner_html(sentences, **kwargs) if display_html: display(HTML(html)) if html_file: (self.path / html_file).write_text(html) if display_str: for sentence in sentences: print(sentence.to_tagged_string()) def plot_training_curves(self, plot_values: List[str] = ["loss", "F1"]): self.plotter.plot_training_curves(self.path / 'loss.tsv', plot_values) def plot_weights(self): self.plotter.plot_weights(self.path / 'weights.txt') def plot_learning_rate(self, skip_first: int = 10, skip_last: int = 5): self.plotter.plot_learning_rate(self.path / 'loss.tsv', skip_first, skip_last) @staticmethod def _preprocess(text, mode=None): '''helper function to preprocess text. returns List of Sentences''' sentences = split_single(text) if mode: nlp = spacy.load('de_core_news_sm') if mode == 'lemmatize': sentences = [ Sentence((' ').join([token.lemma_ for token in nlp(s)])) for s in sentences ] elif mode == 'stem': stemmer = GermanStemmer() sentences = [ Sentence((' ').join( [stemmer.stem(token.text) for token in nlp(s)])) for s in sentences ] else: sentences = [Sentence(s, use_tokenizer=True) for s in sentences] return sentences
def train_sequence_labeling_model(data_folder, proposed_tags_vocabulary_size, skf_split_no): """ Trains the sequence labeling model (by default model uses one RNN layer). Model is trained to predict part of speech tag and takes into account information about: - text (plain text made of tokens that together form a sentence), - occurrence of separator before token, - proposed tags for given token. It is trained with use of Stacked Embeddings used to combine different embeddings together. Words are embedded using a concatenation of three vector embeddings: - WordEmbeddings - classic word embeddings. That kind of embeddings are static and word-level, meaning that each distinct word gets exactly one pre-computed embedding. Here FastText embeddings trained over polish Wikipedia are used. - CharacterEmbeddings - allow to add character-level word embeddings during model training. These embeddings are randomly initialized when the class is being initialized, so they are not meaningful unless they are trained on a specific downstream task. For instance, the standard sequence labeling architecture used by Lample et al. (2016) is a combination of classic word embeddings with task-trained character features. Normally this would require to implement a hierarchical embedding architecture in which character-level embeddings for each word are computed using an RNN and then concatenated with word embeddings. In Flair, this is simplified by treating CharacterEmbeddings just like any other embedding class. To reproduce the Lample architecture, there is only a need to combine them with standard WordEmbeddings in an embedding stack. - One Hot Embeddings - embeddings that encode each word in a vocabulary as a one-hot vector, followed by an embedding layer. These embeddings thus do not encode any prior knowledge as do most other embeddings. They also differ in that they require to see a Corpus during instantiation, so they can build up a vocabulary consisting of the most common words seen in the corpus, plus an UNK token for all rare words. There are one One Hot Embeddings used in training: to embed information about proposed tags (concatenated with a ';') and appearance of separator before each token. Model training is based on stratified 10 fold cross validation split indicated by skf_split_no argument. Model and training logs are saved in resources_ex_3/taggers/example-pos/it-<skf_split_no> directory (where <skf_split_no> is the number of stratified 10 fold cross validation split used to train the model). :param data_folder: folder where files with column corpus split are stored. Those columns are used to initialize ColumnCorpus object :param proposed_tags_vocabulary_size: number of proposed tags :param skf_split_no: number that indicates one of stratified 10 fold cross validation splits (from range 1 to 10) used to train the model """ # define columns columns = {0: 'text', 1: 'pos', 2: 'is_separator', 3: 'proposed_tags'} # init a corpus using column format, data folder and the names of the train and test files # 1. get the corpus corpus: Corpus = ColumnCorpus(data_folder, columns, train_file='train_' + str(skf_split_no), test_file='test_' + str(skf_split_no), dev_file=None) log.info(corpus) # 2. what tag do we want to predict tag_type = 'pos' # 3. make the tag dictionary from the corpus tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) log.info(tag_dictionary) # 4. initialize embeddings local_model_path = use_scratch_dir_if_available( 'resources/polish_FastText_embeddings') embedding_types: List[TokenEmbeddings] = [ WordEmbeddings(local_model_path) if os.path.exists(local_model_path) else WordEmbeddings('pl'), CharacterEmbeddings( use_scratch_dir_if_available('resources/polish_letters_dict')), OneHotEmbeddings(corpus=corpus, field='is_separator', embedding_length=3, min_freq=3), OneHotEmbeddings(corpus=corpus, field='proposed_tags', embedding_length=math.ceil( (proposed_tags_vocabulary_size + 1)**0.25), min_freq=3) ] embeddings: StackedEmbeddings = StackedEmbeddings( embeddings=embedding_types) # 5. initialize sequence tagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=False, rnn_layers=1) # 6. initialize trainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train( use_scratch_dir_if_available('resources_ex_3/taggers/example-pos/it-' + str(skf_split_no)), learning_rate=0.1, mini_batch_size=32, embeddings_storage_mode='gpu', max_epochs=sys.maxsize, monitor_test=True) # 8. plot weight traces (optional) plotter = Plotter() plotter.plot_weights( use_scratch_dir_if_available('resources_ex_3/taggers/example-pos/it-' + str(skf_split_no) + '/weights.txt'))
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger( hidden_size=nb_cells, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True, ) # initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) trainer.train( "resources/taggers/" + exp_name, learning_rate=0.1, embeddings_storage_mode="cpu", mini_batch_size=32, max_epochs=150, shuffle=False, ) plotter = Plotter() plotter.plot_training_curves("resources/taggers/" + exp_name + "/loss.tsv") plotter.plot_weights("resources/taggers/" + exp_name + "/weights.txt")
# 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) # initialize sequence tagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # initialize trainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # start training trainer.train("".join( ('flairmodel', str(n), '/taggers/example-ner' ) ), learning_rate=0.1, mini_batch_size=32, max_epochs=150) # plot weight traces (optional) plotter = Plotter() plotter.plot_weights("".join( ( 'flairmodel', str(n), '/taggers/example-ner/weights.txt' ) ))
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) #5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, rnn_layers=2, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train('resources/taggers/usDL2', learning_rate=0.01, embeddings_in_memory=False, mini_batch_size=32, max_epochs=150, checkpoint=True) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves('resources/taggers/nerpan/loss.tsv') plotter.plot_weights('resources/taggers/nerpan/weights.txt')
rnn_type='RNN_RELU') classifier = TextClassifier(document_embeddings, label_dictionary=corpus.make_label_dictionary(), multi_label=False) trainer = ModelTrainer(classifier, corpus) trainer.train('./', learning_rate=0.05, mini_batch_size=32, max_epochs=10, evaluation_metric=EvaluationMetric.MACRO_F1_SCORE) #plot training curves from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves('loss.tsv') plotter.plot_weights('weights.txt') #make a table with predictions test_pred = pd.read_csv('test.tsv', sep='\t', encoding="utf-8", names=['text', 'label', 'prediction', 'confidence']) test_full = pd.concat([data_test_filtered, test_pred], axis=1, sort=False) #make a table with wrong predicions df = test_full.loc[test_full['prediction'] != test_full['label']] wrong_prediction = df[[ 'attribute::id', 'text', 'label', 'prediction', 'confidence' ]] wrong_prediction.to_csv("cat_without_lemma_flair_wrong_pred.csv", encoding='utf-8', index=False)
# 4. initialize embeddings embedding_types = WordEmbeddings('ru-wiki') # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embedding_types, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train('/home/anna/Desktop/markup/brat_data/without_out/model', learning_rate=0.1, mini_batch_size=32, max_epochs=150) # 8. plot weight traces (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_weights( '/home/anna/Desktop/markup/brat_data/without_out/model/weights.txt')
from flair.visual.training_curves import Plotter clf_dir = 'resources/binary_unbiased_031219/' plotter = Plotter() plotter.plot_training_curves('./resources/loss.tsv') plotter.plot_weights(clf_dir + 'weights.txt')
# CCASS Flair Embeddings BWD #FlairEmbeddings('/data/embeddings_CCASS/flair_language_model/jurinet/best-lm-backward.pt') ] embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) #trainer.num_workers = 20 # 7. start training trainer.train('models/baseline_ner', learning_rate=0.1, mini_batch_size=32, max_epochs=150) # 8. plot weight traces (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_weights('models/baseline_ner/weights.txt')
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger( hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True, ) # initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) trainer.train( "resources/taggers/example-ner", learning_rate=0.1, mini_batch_size=32, max_epochs=1, shuffle=False, ) plotter = Plotter() plotter.plot_training_curves("resources/taggers/example-ner/loss.tsv") plotter.plot_weights("resources/taggers/example-ner/weights.txt")
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionaries=tag_dictionaries, tag_types=tag_types, use_crf=True) # initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # trainer: ModelTrainer = ModelTrainer(tagger, corpus, optimizer=Adam) trainer.train('resources/taggers/famulus_eda_test_n_bert_long2', EvaluationMetric.MICRO_F1_SCORE, learning_rate=0.1, mini_batch_size=32, max_epochs=1000, test_mode=True) # trainer.train('resources/taggers/famulus_test', EvaluationMetric.MICRO_F1_SCORE, learning_rate=0.001, mini_batch_size=32, # max_epochs=100, test_mode=True) plotter = Plotter() plotter.plot_training_curves( 'resources/taggers/famulus_eda_test_n_bert_long2/loss.tsv') plotter.plot_weights( 'resources/taggers/famulus_eda_test_n_bert_long2/weights.txt')
def train_sequence_labeling_model(data_folder, proposed_tags_vocabulary_size, skf_split_no): """ Trains the sequence labeling model (by default model uses one RNN layer). Model is trained to predict part of speech tag and takes into account information about: - text (plain text made of tokens that together form a sentence), - occurrence of separator before token, - proposed tags for given token. It is trained with use of Stacked Embeddings used to combine different embeddings together. Words are embedded using a concatenation of two vector embeddings: - Flair Embeddings - contextual string embeddings that capture latent syntactic-semantic information that goes beyond standard word embeddings. Key differences are: (1) they are trained without any explicit notion of words and thus fundamentally model words as sequences of characters. And (2) they are contextualized by their surrounding text, meaning that the same word will have different embeddings depending on its contextual use. There are forward (that goes through the given on input plain text form left to right) and backward model (that goes through the given on input plain text form right to left) used for part of speech (pos) tag training. - One Hot Embeddings - embeddings that encode each word in a vocabulary as a one-hot vector, followed by an embedding layer. These embeddings thus do not encode any prior knowledge as do most other embeddings. They also differ in that they require to see a Corpus during instantiation, so they can build up a vocabulary consisting of the most common words seen in the corpus, plus an UNK token for all rare words. There are two One Hot Embeddings used in training: - first to embed information about occurrence of separator before token, - second to embed information about concatenated with a ';' proposed tags. Model and training logs are saved in resources/taggers/example-pos directory. This is the method where internal states of forward and backward Flair models are taken at the end of each token and, supplemented by information about occurrence of separator before token and proposed tags for given token used to train model for one of stratified 10 fold cross validation splits. :param data_folder: folder where files with column corpus split are stored. Those columns are used to initialize ColumnCorpus object :param proposed_tags_vocabulary_size: number of proposed tags :param skf_split_no: number that indicates one of stratified 10 fold cross validation splits (from range 1 to 10) used to train the model """ # define columns columns = {0: 'text', 1: 'pos', 2: 'is_separator', 3: 'proposed_tags'} # init a corpus using column format, data folder and the names of the train and test files # 1. get the corpus corpus: Corpus = ColumnCorpus(data_folder, columns, train_file='train_' + str(skf_split_no), test_file='test_' + str(skf_split_no), dev_file=None) log.info(corpus) # 2. what tag do we want to predict tag_type = 'pos' # 3. make the tag dictionary from the corpus tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) log.info(tag_dictionary) # 4. initialize embeddings embedding_types: List[TokenEmbeddings] = [ FlairEmbeddings('pl-forward', chars_per_chunk=64), FlairEmbeddings('pl-backward', chars_per_chunk=64), OneHotEmbeddings(corpus=corpus, field='is_separator', embedding_length=3, min_freq=3), OneHotEmbeddings(corpus=corpus, field='proposed_tags', embedding_length=math.ceil( (proposed_tags_vocabulary_size + 1)**0.25), min_freq=3) ] embeddings: StackedEmbeddings = StackedEmbeddings( embeddings=embedding_types) # 5. initialize sequence tagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=False, rnn_layers=1) # 6. initialize trainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train( use_scratch_dir_if_available('resources/taggers/example-pos/it-' + str(skf_split_no)), learning_rate=0.1, mini_batch_size=32, embeddings_storage_mode='gpu', max_epochs=sys.maxsize, monitor_test=True) # 8. plot weight traces (optional) plotter = Plotter() plotter.plot_weights( use_scratch_dir_if_available('resources/taggers/example-pos/it-' + str(skf_split_no) + '/weights.txt'))
CharacterEmbeddings(), WordEmbeddings("tmp/glove.1.8G.bin") ] embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types) # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=1024, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import SequenceTaggerTrainer trainer: SequenceTaggerTrainer = SequenceTaggerTrainer(tagger, corpus) # 7. start training model_path = "tmp/model2" trainer.train(model_path, learning_rate=0.1, mini_batch_size=8, max_epochs=150) # 8. plot training curves (optional) from flair.visual.training_curves import Plotter plotter = Plotter() plotter.plot_training_curves(f'{model_path}/loss.tsv') plotter.plot_weights(f'{model_path}/weights.txt')
def main(train_file): # 1. get the corpus # define columns columns = {0: 'text', 1: '', 2: '', 3: 'ner'} # this is the folder in which train, test and dev files reside data_folder = './eng_data_mini_onefile/' # retrieve corpus using column format, data folder and the names of the train, dev and test files corpus: TaggedCorpus = NLPTaskDataFetcher.load_column_corpus( data_folder, columns, train_file=train_file, test_file='eng.testb', dev_file='eng.testa') print(corpus) # 2. what tag do we want to predict? tag_type = 'ner' # 3. make the tag dictionary from the corpus tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type) print(tag_dictionary.idx2item) # 4. initialize embeddings 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) # 5. initialize sequence tagger from flair.models import SequenceTagger tagger: SequenceTagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=tag_dictionary, tag_type=tag_type, use_crf=True) # 6. initialize trainer from flair.trainers import ModelTrainer trainer: ModelTrainer = ModelTrainer(tagger, corpus) # 7. start training trainer.train('resources/taggers/example-ner', learning_rate=0.1, mini_batch_size=32, max_epochs=150) # 8. plot training curves (optional) 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')
] # 4. init document embedding by passing list of word embeddings document_embeddings: DocumentLSTMEmbeddings = DocumentLSTMEmbeddings( word_embeddings, hidden_size=512, reproject_words=True, reproject_words_dimension=256, ) # 5. create the text classifier classifier = TextClassifier(document_embeddings, label_dictionary=label_dict, multi_label=True) # 6. initialize the text classifier trainer trainer = ModelTrainer(classifier, corpus) # 7. start the training trainer.train('C:/Users/jeanc/Documents/reviews/model', learning_rate=0.1, mini_batch_size=32, anneal_factor=0.5, patience=5, max_epochs=150) # 8. plot training curves (optional) plotter = Plotter() plotter.plot_training_curves('C:/Users/jeanc/Documents/reviews/model/loss.tsv') plotter.plot_weights('C:/Users/jeanc/Documents/reviews/model/weights.txt')