-
Notifications
You must be signed in to change notification settings - Fork 1
/
dep_parser.py
1027 lines (837 loc) · 39.4 KB
/
dep_parser.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import hashlib
import sys
import pickle
import copy
import logging
import math
import tensorflow as tf
import numpy as np
import sys
import os
import argparse
import json
import random
from model_parameters import *
from lexicon import *
from utils import *
from conll_utils import *
from feature_extractor import SparseFeatureExtractor
from parser_state import ParserState
from arc_standard_transition_system import ArcStandardTransitionState, \
ArcStandardTransitionSystem
from arc_eager_transition_system import ArcEagerTransitionState, \
ArcEagerTransitionSystem
from gold_parse_reader import GoldParseReader
from decoded_parse_reader import DecodedParseReader
from tensorflow.python.ops import state_ops
logger = logging.getLogger('DepParser')
parser = argparse.ArgumentParser(
description='Train a Chen and Manning-style neural network dependency' \
' parser')
# Required positional argument
parser.add_argument('model_folder', type=str,
help='Folder in which to load or save model')
parser.add_argument('training_file', type=str,
help='CoNLL-U format tagged training corpus (UTF-8)')
parser.add_argument('testing_file', type=str,
help='CoNLL-U format tagged evaluation corpus (UTF-8)')
parser.add_argument('--train', action='store_true', default=False,
help='Training a new model or continue training of an '
'old model')
parser.add_argument('--evaluate', action='store_true', default=False,
help='Evaluate an existing model')
parser.add_argument('--debug', action='store_true', default=False,
help='Enable verbose debug lines')
parser.add_argument('--restart', action='store_true', default=False,
help='Re-train model from scratch instead of restoring '
'a previously saved model')
parser.add_argument('--epochs', type=int, default=10,
help='Number of epochs to run (run-throughs over all '
'training corpus feature bags). Default 10')
parser.add_argument('--scoring-strategy', type=str, default='default',
help='Choices: "default", "conllx", "ignore_parens"')
#parser.add_argument('--feature-bag', type=str,
# help='Specify pre-created feature bag file to save' \
# ' computation time (saved in model dir by default')
#parser.add_argument('--epochs', type=int, default=10,
# help='Training epochs (default 10). Shuffle sentences '
# ' and re-train during each training epoch.')
## TODO:
# add param: use pretrained word/sense embeddings gensim/Mikolov
## FIXME:
# trying to continue training with different corpus should throw better error
args = parser.parse_args()
if args.debug:
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.DEBUG)
else:
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO)
try:
os.makedirs(args.model_folder)
except:
pass
if (args.train and args.evaluate) or ((not args.train) and (not args.evaluate)):
print('Please specify either training or evaluation mode '
'(--train/--evaluate)')
sys.exit(1)
if not (args.scoring_strategy == 'default' or \
args.scoring_strategy == 'conllx' or \
args.scoring_strategy == 'ignore_parens'):
print('Unknown scoring strategy "%s"' % args.scoring_strategy)
sys.exit(1)
def batchedSparseToDense(sparse_indices, output_size):
"""Batch compatible sparse to dense conversion.
This is useful for one-hot coded target labels.
Args:
sparse_indices: [batch_size] tensor containing one index per batch
output_size: needed in order to generate the correct dense output
Returns:
A [batch_size, output_size] dense tensor.
"""
eye = tf.diag(tf.fill([output_size], tf.constant(1, tf.float32)))
return tf.nn.embedding_lookup(eye, sparse_indices)
def embeddingLookupFeatures(params, ids):
"""Computes embeddings for each entry of sparse features sparse_features.
Args:
params: list of 2D tensors containing vector embeddings
sparse_features: 1D tensor of strings. Each entry is a string encoding of
dist_belief.SparseFeatures, and represents a variable length list of
feature ids, and optionally, corresponding weights values.
allow_weights: boolean to control whether the weights returned from the
SparseFeatures are used to multiply the embeddings.
Returns:
A tensor representing the combined embeddings for the sparse features.
For each entry s in sparse_features, the function looks up the embeddings
for each id and sums them into a single tensor weighing them by the
weight of each id. It returns a tensor with each entry of sparse_features
replaced by this combined embedding.
"""
if not isinstance(params, list):
params = [params]
# Lookup embeddings.
embeddings = tf.nn.embedding_lookup(params, ids)
return embeddings
'''
Takes an SHA-1 hash of a file
(Useful for hashing training corpus)
'''
def fileHash(fname):
fd = open(fname, 'rb')
retval = hashlib.sha1(fd.read()).hexdigest()
fd.close()
return retval
'''
Entry point for dependency parser
'''
class Parser(object):
def __init__(self, modelParams):
self.logger = logging.getLogger('Parser')
self.modelParams = modelParams
self.variables = {}
self.params = {}
self.trainableParams = []
self.inits = {}
self.averaging = {}
self.averaging_decay = self.modelParams.cfg['averagingDecay']
self.use_averaging = True
self.check_parameters = True
self.training = {}
self.evaluation = {}
with tf.name_scope('params') as self._param_scope:
pass
#self.trainingCorpus = None
#self.testingCorpus = None
def getStep(self):
def onesInitializer(shape, dtype=tf.float32, partition_info=None):
return tf.ones(shape, dtype)
return self.addVariable([], tf.int32, 'step', onesInitializer)
def incrementCounter(self, counter):
return state_ops.assign_add(counter, 1, use_locking=True)
def addLearningRate(self, initial_learning_rate, decay_steps):
"""Returns a learning rate that decays by 0.96 every decay_steps.
Args:
initial_learning_rate: initial value of the learning rate
decay_steps: decay by 0.96 every this many steps
Returns:
learning rate variable.
"""
step = self.getStep()
return cf.with_dependencies(
[self.incrementCounter(step)],
tf.train.exponential_decay(initial_learning_rate,
step,
decay_steps,
0.96,
staircase=True))
def addVariable(self, shape, dtype, name, initializer=None):
if name in self.variables:
return self.variables[name]
self.variables[name] = tf.get_variable(name, shape, dtype, initializer)
if initializer is not None:
self.inits[name] = state_ops.init_variable(self.variables[name],
initializer)
return self.variables[name]
'''
Don't use variable_scope, as param names will overwrite each other
'''
def addParam(self, shape, dtype, name, initializer=None,
return_average=False):
# this isn't a problem. we reload variables if they already exist.
#if name in self.params:
# self.logger.warning(name + ' already exists!')
if name not in self.params:
step = tf.cast(self.getStep(), tf.float32)
with tf.name_scope(self._param_scope):
# Put all parameters and their initializing ops in their own
# scope irrespective of the current scope (training or eval).
self.params[name] = tf.get_variable(name, shape, dtype,
initializer)
param = self.params[name]
if initializer is not None:
self.inits[name] = state_ops.init_variable(param,
initializer)
if self.averaging_decay == 1:
self.logging.info('Using vanilla averaging of parameters.')
ema = tf.train.ExponentialMovingAverage(
decay=(step / (step + 1.0)), num_updates=None)
else:
ema = tf.train.ExponentialMovingAverage(
decay=self.averaging_decay, num_updates=step)
self.averaging[name + '_avg_update'] = ema.apply([param])
self.variables[name + '_avg_var'] = ema.average(param)
self.inits[name + '_avg_init'] = state_ops.init_variable(
ema.average(param), tf.zeros_initializer())
return (self.variables[name + '_avg_var'] if return_average else
self.params[name])
def addEmbedding(self, features, num_features, num_ids, embedding_size,
major_type, return_average=False):
initializer = tf.random_normal_initializer(
stddev=1.0 / embedding_size**.5, \
seed=0)
embedding_matrix = self.addParam(
[num_ids, embedding_size],
tf.float32,
'embedding_matrix_%s' % major_type,
initializer,
return_average=return_average)
embedding = embeddingLookupFeatures(embedding_matrix,
tf.reshape(features,
[-1],
name='feature_%s' % major_type))
return tf.reshape(embedding, [-1, num_features * embedding_size])
'''
Setup transition and action system and feature maps
(necessary whether training or evaluating)
'''
def setupParser(self, mode):
hiddenLayerSizes = self.modelParams.cfg['hiddenLayerSizes']
featureStrings = self.modelParams.cfg['featureStrings']
embeddingSizes = self.modelParams.cfg['embeddingSizes']
batchSize = self.modelParams.cfg['batchSize']
transitionSystem = self.modelParams.cfg['transitionSystem']
if transitionSystem == 'arc-standard':
self.transitionSystem = ArcStandardTransitionSystem()
elif transitionSystem == 'arc-eager':
self.transitionSystem = ArcEagerTransitionSystem()
else:
assert None, 'transition system must be arc-standard or arc-eager'
assert len(hiddenLayerSizes) > 0, 'must have at least one hidden layer'
assert len(featureStrings) == len(set(featureStrings)), \
'duplicate feature string detected'
if mode == 'train':
# determine if we have to compute or read the lexicon
self.logger.info('Computing lexicon from training corpus...')
self.modelParams.lexicon.compute()
self.logger.info('Done building lexicon')
self.modelParams.lexicon.write()
elif mode == 'evaluate':
self.logger.info('Reading lexicon from trained model...')
self.modelParams.lexicon.read()
else:
assert None, 'invalid mode: ' + mode
self.featureMaps = self.modelParams.lexicon.getFeatureMaps()
self.logger.info('Feature strings: ' + str(featureStrings))
# Get major type groups in sorted order by contructing null parser
# state and extracting features, and then concatenating the similar
# types
fvec = SparseFeatureExtractor(featureStrings, self.featureMaps) \
.extract(ParserState(ParsedConllSentence(docid=None),
self.featureMaps), doLogging=False)
featureTypeInstances = fvec.types
self.featureMajorTypeGroups, _ = fvec.concatenateSimilarTypes()
# index: major feature type index
# values: feature names under that type
self.featureNames = [[] for t in self.featureMajorTypeGroups]
self.logger.info('Detected major feature groups (in alphabetical '
'order): ' + str(self.featureMajorTypeGroups))
self.featureDomainSizes = []
#self.featureEmbeddings = []
# For now, use all same embedding sizes
self.featureEmbeddingSizes = \
[embeddingSizes[t] for t in self.featureMajorTypeGroups]
self.BAG_OF_FEATURES_LEN = 0
for i in range(len(featureTypeInstances)):
major_type = featureTypeInstances[i].major_type
major_type_index = self.featureMajorTypeGroups.index(major_type)
self.featureNames[major_type_index].append(
featureTypeInstances[i].name)
self.BAG_OF_FEATURES_LEN += \
(self.featureEmbeddingSizes[major_type_index])
for i in range(len(self.featureMajorTypeGroups)):
major_type = self.featureMajorTypeGroups[i]
self.logger.info('')
self.logger.info('Feature group \'%s\'' % major_type)
self.logger.info('... domain size: %d' % \
(self.featureMaps[major_type].getDomainSize( \
includeSpecial=True)))
self.logger.info('... embedding size: %d' % \
(self.featureEmbeddingSizes[i]))
#self.logger.info('... feature count: %d' % \
# (len(self.featureNames[i])))
self.logger.info('... features')
for fname in self.featureNames[i]:
self.logger.info('....... %s' % (fname))
self.logger.info('... total group embedding size: %d' % \
(len(self.featureNames[i]) * self.featureEmbeddingSizes[i]))
self.logger.info('... initializing random normal embeddings...')
self.featureDomainSizes.append(
self.featureMaps[major_type].getDomainSize( \
includeSpecial=True))
assert len(self.featureDomainSizes) == len(self.featureEmbeddingSizes)
#assert len(self.featureDomainSizes) == len(self.featureEmbeddings)
assert len(self.featureDomainSizes) == len(self.featureNames)
self.logger.info('')
self.logger.info('Batch size (number of parser states): %d' % batchSize)
self.logger.info('Total feature count: %d' % \
(len(featureTypeInstances)))
self.logger.info('Total bag of features length per state: %d' % \
(self.BAG_OF_FEATURES_LEN))
self.logger.info('Total features input size: %d' % \
(batchSize*self.BAG_OF_FEATURES_LEN))
# for actions, we don't encode UNKNOWN, ROOT, or OUTSIDE
# we only encode the number of base values
self.ACTION_COUNT = self.transitionSystem.numActions(
self.featureMaps['label'].getDomainSize(includeSpecial=False))
self.logger.info('Total action count: %d' % self.ACTION_COUNT)
'''
Setup TensorFlow Variables in model
'''
def buildNetwork(self, mode='train'):
assert mode == 'train' or mode == 'eval'
if mode == 'train':
return_average = False
nodes = self.training
else:
return_average = self.use_averaging
nodes = self.evaluation
learningRate = self.modelParams.cfg['learningRate']
decaySteps = self.modelParams.cfg['decaySteps']
# FIXME: does momentum/learning rate reload properly when retraining?
momentum = self.modelParams.cfg['momentum']
topK = self.modelParams.cfg['topK']
hiddenLayerSizes = self.modelParams.cfg['hiddenLayerSizes']
batchSize = self.modelParams.cfg['batchSize']
with tf.name_scope(mode):
weights = []
biases = []
embeddings = []
nodes['feature_endpoints'] = []
for i in range(len(self.featureMajorTypeGroups)):
major_type = self.featureMajorTypeGroups[i]
# shape will be [-1, number of sparse integer features in group]
nodes['feature_endpoints'].append(tf.placeholder(tf.int32, \
[None, len(self.featureNames[i])],
name="ph_feature_endpoints_%s" % major_type))
embeddings.append(self.addEmbedding( \
nodes['feature_endpoints'][i],
len(self.featureNames[i]),
self.featureDomainSizes[i],
self.featureEmbeddingSizes[i],
major_type,
return_average=return_average))
# Input layer
last_layer = tf.concat(embeddings, 1)
last_layer_size = self.BAG_OF_FEATURES_LEN
# Hidden layers
for i in range(len(hiddenLayerSizes)):
h = hiddenLayerSizes[i]
weights.append(self.addParam(
[last_layer_size, h],
tf.float32,
'layer_%d_weights' % i,
tf.random_normal_initializer(stddev=1e-4, seed=0),
return_average=return_average))
biases.append(self.addParam(
[h],
tf.float32,
'layer_%d_biases' % i,
tf.constant_initializer(0.2),
return_average=return_average))
last_layer = tf.nn.relu_layer(last_layer,
weights[-1],
biases[-1],
name='layer_%d' % i)
last_layer_size = h
# Output layer
weights.append(self.addParam(
[last_layer_size, self.ACTION_COUNT],
tf.float32,
'softmax_weights',
tf.random_normal_initializer(stddev=1e-4, seed=0),
return_average=return_average))
biases.append(self.addParam(
[self.ACTION_COUNT],
tf.float32,
'softmax_biases',
tf.zeros_initializer(),
return_average=return_average))
logits = tf.nn.xw_plus_b(last_layer,
weights[-1],
biases[-1],
name='logits')
if mode == 'train':
nodes['gold_actions'] = tf.placeholder(tf.int32, [None], \
name='ph_gold_actions')
nodes['filled_slots'] = tf.placeholder(tf.int32, \
name='ph_filled_slots')
# one-hot encoding for each batch
dense_golden = batchedSparseToDense(nodes['gold_actions'], \
self.ACTION_COUNT)
#cross_entropy = tf.div(
# tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(
# logits=logits, labels=dense_golden)),
# tf.cast(nodes['filled_slots'], tf.float32))
# we should divide by batch size here, not filled slots
# seems to fix the accuracy issue for whatever reason,
# even though cost seems to go crazy momentarily
# (plummets because only a few slots are filled)
cross_entropy = tf.div(
tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=dense_golden)),
batchSize)
# regularize all parameters except output layer
regularized_params = [tf.nn.l2_loss(p) for p in weights[:-1]]
regularized_params += [tf.nn.l2_loss(p) for p in biases[:-1]]
l2_loss = 1e-4 * tf.add_n(regularized_params) \
if regularized_params else 0
cost = tf.add(cross_entropy, l2_loss, name='cost')
lr = self.addLearningRate(learningRate, decaySteps)
optimizer = tf.train.MomentumOptimizer(lr,
momentum,
use_locking=False)
trainableParams = self.params.values()
train_op = optimizer.minimize(cost, var_list=trainableParams)
for param in trainableParams:
slot = optimizer.get_slot(param, 'momentum')
self.inits[slot.name] = state_ops.init_variable(slot,
tf.zeros_initializer())
self.variables[slot.name] = slot
numerical_checks = [
tf.check_numerics(param,
message='Parameter is not finite.')
for param in trainableParams
if param.dtype.base_dtype in [tf.float32, tf.float64]
]
check_op = tf.group(*numerical_checks)
avg_update_op = tf.group(*self.averaging.values())
train_ops = [train_op]
if self.check_parameters:
train_ops.append(check_op)
if self.use_averaging:
train_ops.append(avg_update_op)
nodes['train_op'] = tf.group(*train_ops, name='train_op')
nodes['cost'] = cost
nodes['logits'] = logits
#nodes['softmax'] = tf.nn.softmax(logits)
else:
nodes['logits'] = logits
#nodes['softmax'] = tf.nn.softmax(logits)
'''
Serialize the feature definitions
(so that we can determine when they change)
'''
def serializeFeatureDef(self):
d = []
bs = self.modelParams.cfg['batchSize']
d.append(bs)
# when transition system changes, so do the gold actions
ts = self.modelParams.cfg['transitionSystem']
d.append(ts)
# if projectivize parameter is changed, we may have to recalculate
# features as well (in case there are non-projective sentences)
p = self.modelParams.cfg['projectivizeTrainingSet']
d.append(p)
fs = self.modelParams.cfg['featureStrings']
# order doesn't matter
fs.sort()
d.append(fs)
e = []
# because dictionaries aren't ordered...
for (k, v) in self.modelParams.cfg['embeddingSizes'].items():
e.append((k,v))
# sort by key
e.sort()
d.append(e)
return json.dumps(d)
'''
Generate or load pre-computed feature bags
'''
def obtainFeatureBags(self, trainingFileName):
batchSize = self.modelParams.cfg['batchSize']
projectivizeTrainingSet = self.modelParams.cfg \
['projectivizeTrainingSet']
transitionSystem = self.modelParams.cfg['transitionSystem']
activeFeatureDef = self.serializeFeatureDef().strip()
activeCorpusHash = fileHash(trainingFileName)
cachedFeatureDef = None
try:
fd = open(self.modelParams.getFilePath('feature-def'), 'r',
encoding='utf-8')
cachedFeatureDef = fd.read().strip()
fd.close()
except:
cachedFeatureDef = None
cachedCorpusHash = None
try:
fd = open(self.modelParams.getFilePath('training-corpus-hash'), 'r',
encoding='utf-8')
cachedCorpusHash = fd.read().strip()
self.logger.debug('Cached corpus hash: %s' % cachedCorpusHash)
fd.close()
except:
cachedCorpusHash = None
self.logger.debug('Training corpus hash: %s' % activeCorpusHash)
self.logger.debug('Cached corpus hash: %s' % cachedCorpusHash)
self.logger.debug('Active feature definition: %s' % activeFeatureDef)
self.logger.debug('Cached feature definition: %s' % cachedFeatureDef)
if activeFeatureDef == cachedFeatureDef and \
activeCorpusHash == cachedCorpusHash:
self.logger.info('Loading pre-existing feature bags...')
fd = open(self.modelParams.getFilePath('feature-bag-bin'), 'rb')
batches = pickle.load(fd)
fd.close()
else:
featureStrings = self.modelParams.cfg['featureStrings']
self.logger.info('Feature bag needs recalculation (first training' \
' or features changed)')
# parameters here must match parameters during lexicon generation
trainingCorpus = ParsedConllFile(keepMalformed=False,
projectivize=projectivizeTrainingSet)
trainingCorpus.read(open(self.modelParams.trainingFile, 'r',
encoding='utf-8').read())
# Start getting sentence batches...
reader = GoldParseReader(trainingCorpus, batchSize, \
featureStrings, self.featureMaps, transitionSystem,
epoch_print=False)
batches = []
i = 0
while(True):
self.logger.info('Generating feature bag #%d...' % (i+1))
reader_output = reader.nextFeatureBags()
if reader_output[0] == None:
self.logger.debug('Iter(%d): reader output is None' % i)
break
features_major_types, features_output, gold_actions, \
epoch_num = reader_output
if epoch_num > 1:
# don't make more than one epoch
break
batches.append(reader_output)
i += 1
self.logger.info('Saving feature bags...')
fd = open(self.modelParams.getFilePath('feature-bag-bin'), 'wb')
pickle.dump(batches, fd)
fd.close()
fd = open(self.modelParams.getFilePath('feature-def'), 'w',
encoding='utf-8')
fd.write(activeFeatureDef)
fd.close()
fd = open(self.modelParams.getFilePath('training-corpus-hash'), 'w',
encoding='utf-8')
fd.write(activeCorpusHash)
fd.close()
return batches
'''
Start training from scratch, or from where we left off
'''
def startTraining(self, sess, epochs_to_run=10, restart=False):
batchSize = self.modelParams.cfg['batchSize']
featureStrings = self.modelParams.cfg['featureStrings']
ckpt_dir = fixPath(self.modelParams.modelFolder) + '/'
saver = tf.train.Saver()
if restart:
self.logger.info('Start fitting')
else:
ckpt = tf.train.get_checkpoint_state(ckpt_dir)
if ckpt and ckpt.model_checkpoint_path:
# Restore variables from disk.
saver.restore(sess, ckpt.model_checkpoint_path)
self.logger.info('Model restored')
self.logger.info('Continue fitting')
else:
self.logger.info('Start fitting')
print_freq = 10
save_freq = 500
#eval_freq = 200
batches = self.obtainFeatureBags(self.modelParams.trainingFile)
if epochs_to_run <= 0:
# just do attachment metric if epochs is 0
self.attachmentMetric(sess, runs=200, mode='testing')
return
epoch_num = 0
while epoch_num < epochs_to_run:
i = 0
while i < len(batches):
reader_output = batches[i]
if reader_output[0] == None:
self.logger.debug('Iter(%d): reader output is None' % i)
break
'''
epoch_num refers to the number of run-throughs through the
whole training corpus, whereas `i` is just the batch
iteration number
'''
features_major_types, features_output, gold_actions, \
_ = reader_output
filled_count = len(gold_actions)
if filled_count < batchSize:
# break out (partial batches seem to completely ruin the
# model for whatever reason)
# use continue because in case we shuffle the outer
# dimension, we might get the partial batches in the
# middle
# FIXME: investigate what SyntaxNet does in this case
# have a feeling this might be negatively affecting
# attachmentMetric() function as well, which does process
# partial batches
i += 1
continue
pass
#print('feature(0) len: %d' % len(features_output[0]))
#print('feature(1) len: %d' % len(features_output[1]))
#print('feature(2) len: %d' % len(features_output[2]))
# debug: print out first 40 actions (useful to compare with
# SyntaxNet)
self.logger.debug('gold_actions: %s' % \
str(gold_actions[:40]))
assert len(self.training['feature_endpoints']) == \
len(features_output)
feed_dict = {}
for k in range(len(self.training['feature_endpoints'])):
features_output[k] = np.asarray(features_output[k])
feed_dict[self.training['feature_endpoints'][k]] = \
features_output[k].reshape( \
[-1, len(self.featureNames[k])])
feed_dict[self.training['filled_slots']] = filled_count
feed_dict[self.training['gold_actions']] = gold_actions
c, _ = sess.run([self.training['cost'],
self.training['train_op']],
feed_dict=feed_dict)
if i > 0 and i % print_freq == 0:
self.logger.info('Epoch: %04d Iter: %06d cost=%s' % \
(epoch_num+1, i+1, "{:.2f}".format(c)))
#self.quickEvaluationMetric(sess, mode='training')
# reset avg
#avg_cost = 0.0
if i > 0 and i % save_freq == 0:
save_path = saver.save(sess, ckpt_dir + 'model.ckpt')
self.logger.info('Model saved to file: %s' % save_path)
#self.attachmentMetric(sess, runs=100, mode='training')
#self.attachmentMetric(sess, runs=100, mode='testing')
#if i > 0 and i % eval_freq == 0:
# self.attachmentMetric(sess, runs=200)
i += 1
epoch_num += 1
if epoch_num < epochs_to_run:
# evaluate now. otherwise evaluate after training
# complete message is shown
#self.attachmentMetric(sess, runs=100, mode='training')
pass
else:
self.logger.info('Training is complete (%d epochs)' % \
epochs_to_run)
save_path = saver.save(sess, ckpt_dir + 'model.ckpt')
self.logger.info('Model saved to file: %s' % save_path)
self.attachmentMetric(sess, runs=200, mode='testing')
return
'''
Runs features through the network and gets logits
'features' must be a list with length being
the number of major feature groups
- Each major index will represent feature group
- Each minor index will represent an id in that feature group
'''
def feedForward(self, sess, features, mode):
assert mode == 'train' or mode == 'eval'
nodes = None
if mode == 'train':
# training feed-forward never returns exponentially averaged value
nodes = self.training
else:
# evaluation returns exponentially averaged value if enabled
nodes = self.evaluation
if len(nodes) == 0:
# if not already built...
self.buildNetwork(mode)
assert len(nodes['feature_endpoints']) == \
len(features), 'feature group count must match'
feed_dict = {}
for k in range(len(nodes['feature_endpoints'])):
feed_dict[nodes['feature_endpoints'][k]] = \
np.asarray(features[k]).reshape( \
[-1, len(self.featureNames[k])])
logits = sess.run(nodes['logits'], feed_dict=feed_dict)
return np.asarray(logits)
def attachmentMetric(self, sess, runs=200, mode='testing'):
batchSize = self.modelParams.cfg['batchSize']
transitionSystem = self.modelParams.cfg['transitionSystem']
#batchSize = 128 # let's try a smaller batch for evaluation
featureStrings = self.modelParams.cfg['featureStrings']
topK = self.modelParams.cfg['topK']
assert mode == 'testing' or mode == 'training'
testingCorpus = ParsedConllFile()
if mode == 'testing':
testingCorpus.read(open(self.modelParams.testingFile, 'r',
encoding='utf-8').read())
elif mode == 'training':
testingCorpus.read(open(self.modelParams.trainingFile, 'r',
encoding='utf-8').read())
# evaluate sentence-wide accuracy by UAS and LAS
# of course, token errors can accumulate and this is why sentence-wide
# accuracy is lower than token-only accuracy given by
# quickEvaluationMetric()
# batch size set at one temporarily
test_reader_decoded = DecodedParseReader(testingCorpus, \
batchSize, featureStrings, self.featureMaps, transitionSystem,
epoch_print=False)
correctActions = 0
correctElems = 0
totalElems = 0
outputs = []
filled_count = 0
# eventually will be (filled_count, num_actions)
logits = np.asarray([])
test_runs = runs
for i in range(test_runs):
logger.debug('Evaluation(batch %d)' % i)
test_reader_output = test_reader_decoded.nextFeatureBags(
logits, filled_count)
if test_reader_output[0] == None:
logger.critical('Reader error')
return
features_major_types, features_output, epochs, \
filled_count = test_reader_output
logits = self.feedForward(sess=sess, features=features_output,
mode='eval')
logger.info('Evaluating batch %d/%d...' % (i+1, test_runs))
sentences = test_reader_decoded.getNextAnnotations()
outputs.append(sentences)
token_count = 0
deprel_correct = 0
head_correct = 0
deprel_and_head_correct = 0
for sentences in outputs:
logger.info('-'*20)
for sentence in sentences:
logger.info('-'*20)
#logger.info([w for w in sentence.tokens])
for w in sentence.tokens:
suffix = ''
gold_head = w.HEAD
gold_deprel = w.DEPREL
if gold_head == -1:
gold_deprel = 'ROOT'
if w.parsedHead == -1:
# make it simple
w.parsedLabel = 'ROOT'
if shouldScoreToken(w.FORM, w.UPOSTAG,
self.modelParams.scoring_strategy):
if w.parsedLabel == gold_deprel:
deprel_correct += 1
else:
suffix = 'L'
if w.parsedHead == gold_head:
head_correct += 1
else:
suffix += 'H'
if w.parsedLabel == gold_deprel and \
w.parsedHead == gold_head:
deprel_and_head_correct += 1
# mark both correct
suffix = 'O'
token_count += 1
if w.parsedHead == -1:
logger.info('%-20s%-10s%-5d%-5s' % \
(w.FORM, 'ROOT', w.parsedHead, suffix))
else:
logger.info('%-20s%-10s%-5d%-5s' % \
(w.FORM, w.parsedLabel, w.parsedHead, suffix))
else:
logger.debug('Not scoring token: form="%s", tag="%s"' \
% (w.FORM, w.UPOSTAG))
if token_count <= 0:
logger.warning('No tokens to calculate Attachment Error Metric')
return
# errors that accumulate (tokens are tested based on previous decoded
# decisions, which could screw up shifting and arcing, etc)
# SyntaxNet uses UAS (HEAD-only) for its evaluation during training!
logger.info('Attachment Error Metric (%s_set)' % mode)
logger.info('Scoring Strategy: %s' % \
self.modelParams.scoring_strategy)
logger.info('Accuracy(UAS): %d/%d (%.2f%%)' % \
(head_correct, token_count,
100.0 * float(head_correct) / float(token_count)))
logger.info('Accuracy(LAS): %d/%d (%.2f%%)' % \
(deprel_and_head_correct, token_count,
100.0 * float(deprel_and_head_correct) / float(token_count)))
logger.info('Accuracy(DepRel): %d/%d (%.2f%%)' % \
(deprel_correct, token_count,
100.0 * float(deprel_correct) / float(token_count)))
def __main__():
modelParams = ModelParameters(args.model_folder)
modelParams.trainingFile = args.training_file
modelParams.testingFile = args.testing_file
# set variables from parser-config and isolate them in a separate namespace
# to avoid collisions with this code
fd = open(modelParams.getFilePath('parser-config'), 'r', \
encoding='utf-8')
configFile = fd.read()
fd.close()
fd = open(modelParams.getFilePath('trained-config'), 'w', \
encoding='utf-8')
fd.write(configFile)
fd.close()
compile(configFile, '<string>', 'exec')
configNamespace = {}
exec(configFile, configNamespace)
requiredFields = ['learningRate', 'batchSize', 'topK',
'hiddenLayerSizes', 'embeddingSizes', 'featureStrings',
'momentum', 'projectivizeTrainingSet', 'transitionSystem']
for field in requiredFields:
assert configNamespace[field] != None, 'please set %s in config' % field
modelParams.cfg = configNamespace
modelParams.lexicon = Lexicon(modelParams)