forked from ai-forever/ner-bert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconll2003 BERTBiLSTMCRF.py
More file actions
222 lines (90 loc) · 3.03 KB
/
conll2003 BERTBiLSTMCRF.py
File metadata and controls
222 lines (90 loc) · 3.03 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import sys
import warnings
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = torch.device("cpu")
warnings.filterwarnings("ignore")
sys.path.append("../")
# In[4]:
from modules.data.conll2003.prc import conll2003_preprocess
# In[7]:
data_dir = "./modules/data/conll2003/"
# In[8]:
conll2003_preprocess(data_dir)
# ## IO markup
# ### Train
# In[10]:
from modules.data import bert_data
# In[12]:
import os
data = bert_data.LearnData.create(
train_df_path=os.path.join(data_dir,"eng.train.train.csv"),
valid_df_path=os.path.join(data_dir,"eng.testa.dev.csv"),
idx2labels_path=os.path.join(data_dir,"idx2labels.txt"),
clear_cache=True,device=device
)
# In[14]:
from modules.models.bert_models import BERTBiLSTMCRF
# In[15]:
model = BERTBiLSTMCRF.create(
len(data.train_ds.idx2label),
# model_name='bert-base-multilingual-cased',
model_name='./bert_data',
lstm_dropout=0.3, crf_dropout=0.3,device=device)
# In[17]:
from modules.train.train import NerLearner
# In[18]:
num_epochs = 100
# In[19]:
learner = NerLearner(
model, data, "./modules/models/conll2003-BERTBiLSTMCRF-IO.cpt", t_total=num_epochs * len(data.train_dl))
# In[20]:
model.get_n_trainable_params()
# In[22]:
learner.fit(epochs=num_epochs)
# ### Predict
# In[12]:
from modules.data.bert_data import get_data_loader_for_predict
# In[13]:
dl = get_data_loader_for_predict(data, df_path=data.valid_ds.config["df_path"])
# In[14]:
preds = learner.predict(dl)
# In[15]:
from sklearn_crfsuite.metrics import flat_classification_report
# In[16]:
from modules.analyze_utils.utils import bert_labels2tokens, voting_choicer
from modules.analyze_utils.plot_metrics import get_bert_span_report
# In[17]:
pred_tokens, pred_labels = bert_labels2tokens(dl, preds)
true_tokens, true_labels = bert_labels2tokens(dl, [x.bert_labels for x in dl.dataset])
# In[18]:
assert pred_tokens == true_tokens
tokens_report = flat_classification_report(true_labels, pred_labels, labels=data.train_ds.idx2label[4:], digits=4)
# In[20]:
print(tokens_report)
# ### Test
# In[12]:
from modules.data.bert_data import get_data_loader_for_predict
# In[24]:
dl = get_data_loader_for_predict(data, df_path=os.path.join(data_dir,"eng.testa.dev.csv"))
# In[25]:
preds = learner.predict(dl)
# In[26]:
from sklearn_crfsuite.metrics import flat_classification_report
# In[27]:
from modules.analyze_utils.utils import bert_labels2tokens, voting_choicer
from modules.analyze_utils.plot_metrics import get_bert_span_report
# In[28]:
pred_tokens, pred_labels = bert_labels2tokens(dl, preds)
true_tokens, true_labels = bert_labels2tokens(dl, [x.bert_labels for x in dl.dataset])
# In[29]:
assert pred_tokens == true_tokens
tokens_report = flat_classification_report(true_labels, pred_labels, labels=data.train_ds.idx2label[4:], digits=4)
# In[30]:
print(tokens_report)
# In[11]:
# os.path.join(data_dir,"eng.train.train.csv"),
# In[ ]: