forked from flairNLP/flair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
51 lines (38 loc) · 1.63 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flair.data import NLPTaskDataFetcher, TaggedCorpus, NLPTask
from flair.embeddings import TextEmbeddings, WordEmbeddings, StackedEmbeddings, CharLMEmbeddings, CharacterEmbeddings
from typing import List
import torch
# 1. get the corpus
corpus: TaggedCorpus = NLPTaskDataFetcher.fetch_data(NLPTask.CONLL_03).downsample(0.01)
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)
# initialize embeddings
embedding_types: List[TextEmbeddings] = [
WordEmbeddings('glove')
# comment in this line to use character embeddings
, CharacterEmbeddings()
# comment in these lines to use contextual string embeddings
,
CharLMEmbeddings('news-forward')
,
CharLMEmbeddings('news-backward')
]
embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types)
# initialize sequence tagger
from flair.tagging_model import SequenceTagger
tagger: SequenceTagger = SequenceTagger(hidden_size=256,
embeddings=embeddings,
tag_dictionary=tag_dictionary,
tag_type=tag_type,
use_crf=True)
if torch.cuda.is_available():
tagger = tagger.cuda()
# initialize trainer
from flair.trainer import TagTrain
trainer: TagTrain = TagTrain(tagger, corpus, test_mode=True)
trainer.train('resources/taggers/example-ner', mini_batch_size=32, max_epochs=150, save_model=False,
train_with_dev=False, anneal_mode=False)