11
11
from entity_recognition_datasets .src import utils
12
12
from model import BiLSTM
13
13
14
- EPOCHS = 5
14
+ EPOCHS = 4
15
15
LOSS_FUNC = nn .CrossEntropyLoss ()
16
16
17
17
@@ -50,7 +50,7 @@ def train_single(sentence, optimizer, backprop):
50
50
optimizer .step ()
51
51
loss = loss .item ()
52
52
except Exception as e :
53
- print ("e" )
53
+ print (e )
54
54
print ("words: {}" .format (words ))
55
55
print ("tags: {}" .format (tags ))
56
56
print ("continuing..." )
@@ -72,32 +72,32 @@ def train():
72
72
dev_losses = []
73
73
for epoch in range (EPOCHS ):
74
74
print ("EPOCH {}/{}" .format (epoch + 1 , EPOCHS ))
75
- start = time .time ()
76
75
77
76
# run train epoch
77
+ start = time .time ()
78
78
train_loss = 0
79
79
for sentence in tqdm (train_data , desc = "train-set" ):
80
80
loss = train_single (sentence , optimizer , backprop = True )
81
81
train_loss += loss
82
82
train_loss /= len (train_data )
83
83
train_losses .append (train_loss )
84
+ duration = time .time () - start
85
+ print ("train set completed in {:.3f}s, {:.3f}s per iteration" .format (duration , duration / len (train_data )))
84
86
85
87
# run a dev epoch
88
+ start = time .time ()
86
89
dev_loss = 0
87
90
with torch .no_grad ():
88
91
for sentence in tqdm (dev_data , desc = "dev-set" ):
89
92
loss = train_single (sentence , optimizer , backprop = False )
90
- train_loss += loss
93
+ dev_loss += loss
91
94
dev_loss /= len (dev_data )
92
95
dev_losses .append (dev_loss )
96
+ duration = time .time () - start
97
+ print ("dev set completed in {:.3f}s, {:.3f}s per iteration" .format (duration , duration / len (dev_data )))
93
98
94
99
print ("train loss = {}" .format (train_loss ))
95
100
print ("dev loss = {}" .format (dev_loss ))
96
- duration = time .time () - start
97
- print ("epoch completed in {:.3f}s, {:.3f}s per iteration" .format (
98
- duration ,
99
- duration / (len (train_data ) + len (dev_data ))
100
- ))
101
101
102
102
losses = {
103
103
"train" : train_losses ,
@@ -120,11 +120,17 @@ def evaluate():
120
120
with torch .no_grad ():
121
121
confusion = np .zeros ((2 , 2 ))
122
122
for sentence in tqdm (test_data , desc = "train-set" ):
123
- words , tags = get_words_and_tags (sentence )
124
- pred = model .evaluate (words )
125
- assert len (pred ) == len (tags )
126
- for i in range (len (pred )):
127
- confusion [pred [i ]][tags [i ]] += 1
123
+ try :
124
+ words , tags = get_words_and_tags (sentence )
125
+ pred = model .evaluate (words )
126
+ assert len (pred ) == len (tags )
127
+ for i in range (len (pred )):
128
+ confusion [pred [i ]][tags [i ]] += 1
129
+ except Exception as e :
130
+ print (e )
131
+ print ("words: {}" .format (words ))
132
+ print ("tags: {}" .format (tags ))
133
+ print ("continuing..." )
128
134
129
135
confusion /= np .sum (confusion )
130
136
return confusion
0 commit comments