-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_new_jointmod.py
1122 lines (883 loc) · 47.8 KB
/
model_new_jointmod.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
import os
import time
import sys
import copy
import numpy as np
import tensorflow as tf
import threading
import random
import argparse
import time
import logging
from logging.handlers import RotatingFileHandler
from logging import handlers
from bio_tools import *
#from tensorflow.python.client import timeline
from general_utils import minibatches, Progbar, get_cpu_loads, get_gpu_loads_and_efficiency
current_milli_time = lambda: int(round(time.time() * 1000))
parser = argparse.ArgumentParser(description='Train a morphing or tagging model.')
# Required positional argument
parser.add_argument('model_type', type=str,
help='morph or tag')
parser.add_argument('model_data_dir', type=str,
help='Data directory to load training data from (data/model_data_dir/model_type_{train,dev,test}.txt')
parser.add_argument('--model-output-dir', type=str, default=None,
help='Model directory to store or load checkpoints from')
parser.add_argument('--use-crf', action='store_true', default=False,
help='Add a CRF (Viterbi) layer after the Bi-LSTM (default off)')
parser.add_argument('--batch-size', type=int, default=-1,
help='Batch size (default NUM_GPUS*8 with CRF, NUM_GPUS*128 without CRF)')
parser.add_argument('--no-shuffle', action='store_true', default=False,
help='Don\'t shuffle before each epoch (default: off; shuffle)')
parser.add_argument('--max-epochs', type=int, default=100,
help='Maximum training epochs (default 100). Early stopping occurs automatically after 3 epochs anyway.')
parser.add_argument('--max-sequence-length', type=int, default=400,
help='Maximum sequence length for input into Bi-LSTM (default 400)')
parser.add_argument('--use-static-padding', action='store_true', default=False,
help='Use same max sequence length for every batch (inefficient, default off)')
parser.add_argument('--num-gpus', type=int, default=1,
help='Number of GPUs to use (default 1)')
parser.add_argument('--input-unit-embedding-size', type=int, default=300,
help='Embedding size for input unit (default 300)')
parser.add_argument('--combined-hidden-size', type=int, default=300,
help='Combined hidden size for input into LSTM (default 300)')
parser.add_argument('--dropout-keep-prob', type=float, default=0.7,
help='Dropout keep probability (default 0.7: which means 30%% dropout)')
parser.add_argument('--lstm-style', type=str, default='tf',
help='LSTM style: tf or cudnn (default tf). cuDNN may provide performance increases')
args = parser.parse_args()
assert args.lstm_style in ['tf', 'cudnn']
#if args.lstm_style == 'cudnn':
# assert args.use_static_padding == True, 'must enable static padding when using cuDNN LSTM: variable sequence lengths are not supported by TensorFlow'
# important to specify soft placement parameter for EVERY instance of tf.Session
# apparently, or otherwise it doesn't really take effect
SOFT_PLACEMENT = True
NUM_GPUS = args.num_gpus
if args.batch_size == -1:
if args.use_crf:
args.batch_size = NUM_GPUS*8
else:
args.batch_size = NUM_GPUS*128
# FIXME: for now we start with 0
# rest of code may need to be fixed as well to use GPU_IDS
GPU_IDS = [i for i in range(NUM_GPUS)]
USE_BATCH_NORMALIZATION = False
USE_DYNAMIC_PADDING = not args.use_static_padding
# assume equal work
SPLIT_PROPORTIONS = []
for i in range(NUM_GPUS):
SPLIT_PROPORTIONS.append(1)
#SPLIT_PROPORTIONS = [1] # equal work
#SPLIT_PROPORTIONS = [3, 1] # put 3x more work on GPU:0
#SPLIT_PROPORTIONS = [20, 15] # put 20/35 work on GPU:0, 15/35 work on GPU:1
assert len(SPLIT_PROPORTIONS) == NUM_GPUS
do_monitor = False
# amatteson@blp-deep-server-1:~/newunitag$ LD_PRELOAD=/home/amatteson/cudnn-8.0-6.0/cuda/lib64/libcudnn.so.6 python3 model.py
# TODO: try limiting sentence size to max size per-batch (could be much more efficient)
# TODO: add joint training of two different models (morphing and tagging)
# TODO: fix multi-gpu splitting of last remainder batch (one gpu might be left with no samples)
# might be able to train with garbage data and ignore gradients calculated for the gpus
# with no real batches
# TODO: check out transition params for crf and whether it's safe for this to be split across GPUs
# like it currently is in the code (UPDATE: this is just a temporarily used parameter, but it is
# in the trainable parameter list...a bit odd)
# seems like it may need to be stuck on one GPU...hmmm....maybe? maybe the way we had it before was
# ok with each GPU updating it as time went on...just the same variable??
# maximum input units (pad until this number; disallow longer input)
MAX_UNIT_COUNT = args.max_sequence_length
'''
Get a model name that might make sense given the specified parameters
'''
def get_model_name():
arch = ''
if args.use_crf:
arch = 'bilstm_crf'
else:
arch = 'bilstm'
if args.use_static_padding:
seq_type = 'static'
else:
seq_type = 'dynamic'
return '%s_%s_%s_%s_%s_ES%d_CH%d_DK%d_SEQ%d_B%d_EP%d' % (args.model_data_dir, args.model_type, arch, args.lstm_style, seq_type, args.input_unit_embedding_size, args.combined_hidden_size, 100.0*args.dropout_keep_prob, args.max_sequence_length, args.batch_size, args.max_epochs)
if args.model_output_dir == None:
args.model_output_dir = get_model_name()
if not args.model_output_dir.endswith('/'):
args.model_output_dir += '/'
# will throw error if already exists to alert user
#try:
os.mkdir(args.model_output_dir)
#except:
# pass
class ModelConfig(object):
def __init__(self):
self.input_unit_embedding_sizes = None
self.do_unit_embedding_training = None
self.dropout_keep_prob = None
self.combined_hidden_size = None
self.nepochs = None
self.nepoch_no_imprv = None
self.learning_rate = None
self.lr_decay = None
self.crf = None
self.output_path = None
self.model_output = None
self.log_path = None
self.batch_size = None
config = ModelConfig()
config.output_path = args.model_output_dir
config.model_output = config.output_path + 'output/'
#try:
os.mkdir(config.model_output)
#except:
# pass
# TODO: fix to train or eval depending on mode
config.log_path = config.model_output + 'train.log'
#log_format = logging.Formatter('%(asctime)s : %(levelname)s : [%(name)s] : %(message)s')
#logging.basicConfig(filename=config.log_path, filemode='w', level=logging.INFO)
'''logger = logging.getLogger('Model')
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(log_format)
logger.addHandler(ch)
fh = handlers.RotatingFileHandler(config.log_path, maxBytes=(1048576*500), backupCount=7)
fh.setFormatter(log_format)
logger.addHandler(fh)
'''
logging.basicConfig(
format='%(asctime)s : %(levelname)s : [%(name)s] : %(message)s',
level=logging.INFO,
handlers=[
logging.FileHandler(config.log_path, mode='w'), # overwrite log
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger('Model')
logger.info('Model data: data/%s/%s_{train,dev,test}.txt' % (args.model_data_dir, args.model_type))
logger.info('Model output directory: %s' % args.model_output_dir)
if NUM_GPUS > 1:
logger.info('Model batch size: %d (batch will be split amongst %d GPUs with proportions: %s)' % (args.batch_size, NUM_GPUS, str(SPLIT_PROPORTIONS)))
else:
logger.info('Model batch size: %d' % args.batch_size)
fixed_batch_split_sizes = []
if NUM_GPUS > 1:
for i in range(NUM_GPUS - 1):
fixed_batch_split_sizes.append(int(args.batch_size * float(SPLIT_PROPORTIONS[i]) / float(sum(SPLIT_PROPORTIONS))))
# add remainder
fixed_batch_split_sizes.append(args.batch_size - sum(fixed_batch_split_sizes))
else:
fixed_batch_split_sizes.append(args.batch_size)
if USE_DYNAMIC_PADDING:
padding_type = 'per-batch dynamic'
else:
padding_type = 'static'
logger.info('Model max sequence length: %d (%s padding)' % (MAX_UNIT_COUNT, padding_type))
if args.use_crf:
logger.info('Model CRF enabled: yes')
else:
logger.info('Model CRF enabled: no')
logger.info('Combined input unit embedding size: %d' % args.input_unit_embedding_size)
logger.info('Combined hidden layer unit size: %d' % args.combined_hidden_size)
logger.info('Dropout keep probability: %f' % args.dropout_keep_prob)
logger.info('Model max epochs: %d' % args.max_epochs)
def average_gradients(tower_grads):
'''Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
'''
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
# Small epsilon value for the BN transform
epsilon = 1e-3
def batch_norm_wrapper(inputs, is_training, decay = 0.999):
scale = tf.get_variable('batch_norm_scale',
dtype=tf.float32, initializer=tf.ones([inputs.get_shape()[-1]]))
beta = tf.get_variable('batch_norm_beta',
dtype=tf.float32, initializer=tf.zeros([inputs.get_shape()[-1]]))
pop_mean = tf.get_variable('batch_norm_pop_mean',
dtype=tf.float32, initializer=tf.zeros([inputs.get_shape()[-1]]), trainable=False)
pop_var = tf.get_variable('batch_norm_pop_var',
dtype=tf.float32, initializer=tf.ones([inputs.get_shape()[-1]]), trainable=False)
if is_training:
batch_mean, batch_var = tf.nn.moments(inputs,[0])
train_mean = tf.assign(pop_mean,
pop_mean * decay + batch_mean * (1 - decay))
train_var = tf.assign(pop_var,
pop_var * decay + batch_var * (1 - decay))
with tf.control_dependencies([train_mean, train_var]):
return tf.nn.batch_normalization(inputs,
batch_mean, batch_var, beta, scale, epsilon)
else:
return tf.nn.batch_normalization(inputs,
pop_mean, pop_var, beta, scale, epsilon)
# TODO: joint model
'''
class JointModel(object):
def __init__(self):
self.models = []
def add_submodel(self, submodel):
self.models.append(submodel)
def add_joint_loss_op(self):
self.joint_loss = tf.sum(self.
'''
class MorphingTaggingModel(object):
def __init__(self, lexicon, config):
self.logger = logging.getLogger('MorphingTaggingModel')
self.lexicon = lexicon
self.config = config
self.idx_to_unit0, self.unit0_to_idx = self.lexicon.get_input_lexicon_for_training(0)
self.idx_to_label, self.label_to_idx = self.lexicon.get_label_lexicon_for_training()
self.add_placeholders()
self.add_embeddings_op()
for g in range(NUM_GPUS):
# this way, parameters will be shared amongst training and inference
with tf.variable_scope('tower%d_arch' % g):
self.add_logits_ops(gpu_num=g, is_training=True)
tf.get_variable_scope().reuse_variables()
self.add_logits_ops(gpu_num=g, is_training=False)
self.add_pred_ops()
self.add_loss_ops()
self.add_train_op()
self.add_init_op()
self.logger.info('Trainable variables')
with tf.Session(config=tf.ConfigProto(allow_soft_placement=SOFT_PLACEMENT)) as sess:
sess.run(self.init)
#options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
#run_metadata = tf.RunMetadata()
tvars = tf.trainable_variables()
tvars_vals = sess.run(tvars)
#options=options,
#run_metadata=run_metadata)
#fetched_timeline = timeline.Timeline(run_metadata.step_stats)
#chrome_trace = fetched_timeline.generate_chrome_trace_format()
#with open('timeline_debug.json', 'w') as f:
# f.write(chrome_trace)
for var, val in zip(tvars, tvars_vals):
self.logger.info('... %s' % var.name) # , val) # Prints the name of the variable alongside its value.
def add_placeholders(self):
'''
Adds placeholders to self
'''
# words or morphemes
# shape = (batch size, max length of sentence in batch)
self.unit0_ids = tf.placeholder(tf.int32, shape=[self.config.batch_size, None],
name='unit0_ids')
# number of "units" (words or morphemes)
# shape = (batch size)
self.sequence_lengths = tf.placeholder(tf.int32, shape=[self.config.batch_size],
name='sequence_lengths')
# shape = (batch size, max length of sentence in batch)
self.labels = tf.placeholder(tf.int32, shape=[self.config.batch_size, None],
name='labels')
# hyper parameters
# dropout keep probability (1.0 means keep all data)
self.dropout_keep_prob = tf.placeholder(dtype=tf.float32, shape=[],
name='dropout_keep_prob')
# learning rate
self.learning_rate = tf.placeholder(dtype=tf.float32, shape=[],
name='learning_rate')
# split up batch for multi-gpu
self.batch_split_sizes = tf.placeholder(tf.int32, shape=[NUM_GPUS], name='batch_split_sizes')
self.split_unit0_ids = tf.split(self.unit0_ids, self.batch_split_sizes, axis=0)
self.split_sequence_lengths = tf.split(self.sequence_lengths, self.batch_split_sizes, axis=0)
self.split_labels = tf.split(self.labels, self.batch_split_sizes, axis=0)
# used at inference time
self.joined_labels = tf.concat(self.split_labels, axis=0)
self.transition_params = []
self.logits_train = []
self.logits_infer = []
self.loss = []
self.labels_pred = []
self.init_h = []
self.init_c = []
for _ in range(NUM_GPUS):
self.transition_params.append(None)
self.logits_train.append(None)
self.logits_infer.append(None)
self.loss.append(None)
self.labels_pred.append(None)
self.init_h.append(None)
self.init_c.append(None)
self.standard_trainable_variables = []
def add_embeddings_op(self):
'''
Adds embeddings to self (for first unit)
'''
combined_embeddings = None
with tf.variable_scope('unit0'):
# initialize unit 0's vocabulary embeddings as random normal distribution
unit0_initial_value = tf.random_normal([len(self.idx_to_unit0),
self.config.input_unit_embedding_sizes[0]],
stddev=1.0 / (self.config.input_unit_embedding_sizes[0]**.5), \
seed=0)
_unit0_embeddings = tf.get_variable(initializer=unit0_initial_value,
name='_unit0_embeddings', dtype=tf.float32,
trainable=self.config.do_unit_embedding_training[0])
if self.config.do_unit_embedding_training[0]:
self.standard_trainable_variables.append(_unit0_embeddings)
combined_embeddings = tf.nn.embedding_lookup(_unit0_embeddings,
self.unit0_ids,
name='unit0_embeddings')
self.logger.info('... Adding unit 0 embeddings with shape: ' + \
str(combined_embeddings.get_shape()))
self.logger.info('Concatenated embedding shape: ' + \
str(combined_embeddings.get_shape()))
# dropout created in static graph: takes effect if necessary when
# referring to self.combined_embeddings
self.combined_embeddings = tf.nn.dropout(combined_embeddings,
keep_prob=self.dropout_keep_prob)
# TODO: evaluate if there's a better way to do this.
# is it even necessary to split up unit0_ids when we have this var?
self.split_combined_embeddings = tf.split(self.combined_embeddings,
self.batch_split_sizes, axis=0)
def add_pred_ops(self):
'''
Adds labels_pred to self
'''
for gpu_num in range(NUM_GPUS):
with tf.device('/gpu:%d' % gpu_num):
if not self.config.crf:
self.labels_pred[gpu_num] = tf.cast(tf.argmax(self.logits_infer[gpu_num], axis=-1), tf.int32)
def add_loss_ops(self):
'''
Adds loss to self
'''
for gpu_num in range(NUM_GPUS):
with tf.device('/gpu:%d' % gpu_num):
if self.config.crf:
with tf.variable_scope('tower%d_crf' % gpu_num):
log_likelihood, self.transition_params[gpu_num] = tf.contrib.crf.crf_log_likelihood(
self.logits_train[gpu_num], self.split_labels[gpu_num], self.split_sequence_lengths[gpu_num])
self.loss[gpu_num] = tf.reduce_mean(-log_likelihood)
else:
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits_train[gpu_num], labels=self.split_labels[gpu_num])
mask = tf.sequence_mask(self.split_sequence_lengths[gpu_num])
losses = tf.boolean_mask(losses, mask)
self.loss[gpu_num] = tf.reduce_mean(losses)
#tf.add_to_collection('losses', self.loss)
# for tensorboard
tf.summary.scalar('loss_%d' % gpu_num, self.loss[gpu_num])
def add_logits_ops(self, gpu_num, is_training):
'''
Adds logits to self
'''
assert not USE_BATCH_NORMALIZATION, 'USE_BATCH_NORMALIZATION needs fix below for is_training'
# number of output labels
nlabels = len(self.idx_to_label)
#if is_training:
# # reuse can't be false...so???
# do_reuse = tf.AUTO_REUSE
#else:
# do_reuse = True
with tf.device('/gpu:%d' % gpu_num):
if args.lstm_style == 'tf':
# self.split_combined_embeddings[gpu_num]: input shape:[64 400 300]
lstm_cell = tf.contrib.rnn.LSTMCell(num_units=self.config.combined_hidden_size, state_is_tuple=True)
(output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(lstm_cell,
lstm_cell, self.split_combined_embeddings[gpu_num], sequence_length=self.split_sequence_lengths[gpu_num], dtype=tf.float32)
output = tf.concat([output_fw, output_bw], axis=-1)
#output = tf.Print(output, [tf.shape(output)], message='output shape:')
# print('output.get_shape()', output.get_shape().as_list())
# [64 400 600]
elif args.lstm_style == 'cudnn':
combined_embeddings_input = self.split_combined_embeddings[gpu_num]
self.logger.debug('combined_embeddings_input shape: %s' % str(combined_embeddings_input.get_shape().as_list()))
#combined_embeddings_input = tf.reshape(combined_embeddings_input, [-1, MAX_UNIT_COUNT, sum(self.config.input_unit_embedding_sizes)])
combined_embeddings_input = tf.reshape(combined_embeddings_input, [fixed_batch_split_sizes[gpu_num], -1, sum(self.config.input_unit_embedding_sizes)])
combined_embeddings_input = tf.transpose(combined_embeddings_input, [1, 0, 2])
self.logger.debug('combined_embeddings_input transpose: %s' % str(combined_embeddings_input.get_shape().as_list()))
num_lstm_layers = 1
cudnn_cell = tf.contrib.cudnn_rnn.CudnnLSTM(
num_layers=num_lstm_layers,
num_units=self.config.combined_hidden_size,
direction='bidirectional',
input_mode='linear_input',
input_size=self.config.combined_hidden_size,
dtype=tf.float32
# TODO: dropout here?
)
# bizarre: init_h,init_c require first dim to be 2 because it gets divided by dir_count(2 when bidirectional)
# model_shapes->num_layers = (*input_h)->dim_size(0) / model_shapes->dir_count;
# otherwise first dim ends up being 0.5 (0 as int)
# don't change batch_split_sizes later
self.logger.debug('fixed_batch_split_sizes[%d]: %d' % (gpu_num, fixed_batch_split_sizes[gpu_num]))
self.init_h[gpu_num] = tf.get_variable('h', dtype=tf.float32, initializer=tf.zeros([2*num_lstm_layers, fixed_batch_split_sizes[gpu_num], self.config.combined_hidden_size]))
self.init_c[gpu_num] = tf.get_variable('c', dtype=tf.float32, initializer=tf.zeros([2*num_lstm_layers, fixed_batch_split_sizes[gpu_num], self.config.combined_hidden_size]))
# FIXME: tweak init_scale
params_size_t = cudnn_cell.params_size()
self.logger.debug('params_size_t: %s' % str(params_size_t))
cudnn_params = tf.get_variable('lstm_params',
initializer=tf.random_uniform([params_size_t], -0.04, 0.04), validate_shape=False)
outputs, h, c = cudnn_cell(
combined_embeddings_input,
input_h=self.init_h[gpu_num],
input_c=self.init_c[gpu_num],
params=cudnn_params,
is_training=is_training
)
self.logger.debug('outputs.get_shape(): %s' % str(outputs.get_shape().as_list()))
self.logger.debug('h.get_shape(): %s' % str(h.get_shape().as_list()))
self.logger.debug('c.get_shape(): %s' % str(c.get_shape().as_list()))
output = tf.transpose(outputs, [1, 0, 2])
self.logger.debug('after transpose output.get_shape(): %s' % str(output.get_shape().as_list()))
output = tf.nn.dropout(output, self.dropout_keep_prob)
with tf.device('/gpu:%d' % gpu_num):
#with tf.variable_scope('tower%d_proj' % gpu_num, reuse=tf.AUTO_REUSE):
W = tf.get_variable('tower%d_W' % gpu_num, shape=[2*self.config.combined_hidden_size, nlabels],
dtype=tf.float32, initializer=tf.random_normal_initializer( \
stddev=1.0 / (2*self.config.combined_hidden_size)**.5, \
seed=0))
b = tf.get_variable('tower%d_b' % gpu_num, shape=[nlabels], dtype=tf.float32,
initializer=tf.zeros_initializer())
ntime_steps = tf.shape(output)[1]
output = tf.reshape(output, [-1, 2*self.config.combined_hidden_size])
#output = tf.Print(output, [tf.shape(output)], 'after proj reshape:')
#print('after proj reshape:', output.get_shape().as_list())
# after proj reshape:[25600 600]
pred = tf.matmul(output, W) + b
#if USE_BATCH_NORMALIZATION:
# pred_batch_norm = batch_norm_wrapper(pred, is_training)
#else:
pred_batch_norm = pred
logits = tf.reshape(pred_batch_norm, [-1, ntime_steps, nlabels])
if is_training:
#logits = tf.Print(logits, [logits], 'logits_train:')
self.logits_train[gpu_num] = logits
else:
#logits = tf.Print(logits, [logits], 'logits_infer:')
self.logits_infer[gpu_num] = logits
def add_train_op(self):
'''
Add train_op to self
'''
root_scope = tf.get_default_graph().get_name_scope()
all_trainable = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, root_scope)
self.logger.debug('Root scope: %s' % str(root_scope))
optimizer = tf.train.AdamOptimizer(self.learning_rate)
self.global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)
tower_grads = []
for i in range(NUM_GPUS):
with tf.device('/gpu:%d' % i):
logits = self.logits_train[i]
loss_op = self.loss[i]
trainable = []
for elem in all_trainable:
if elem.name.startswith('%s/tower' % root_scope):
if elem.name.startswith('%s/tower%d_' % (root_scope, i)):
trainable.append(elem)
else:
trainable.append(elem)
self.logger.info('Trainable variables [gpu %d]' % i)
for elem in trainable:
self.logger.info('... %s' % elem.name)
#tf.get_variable_scope().reuse_variables()
#summaries = tf.get_collection(tf.GraphKeys.SUMMARIES, scope)
#print('\n'.join('{}: {}'.format(*k) for k in enumerate(summaries)))
grads = optimizer.compute_gradients(loss_op, trainable)
#print('\n'.join('{}: {}'.format(*k) for k in enumerate(grads)))
tower_grads.append(grads)
# Create a dummy optimization operation to create variables needed for optimization.
with tf.variable_scope('tower%d_adam_opt' % i, reuse=tf.AUTO_REUSE):
_ = optimizer.minimize(loss_op)
tf.get_variable_scope().reuse_variables()
# We must calculate the mean of each gradient. Note that this is the
# synchronization point across all towers.
grads = average_gradients(tower_grads)
# Apply the gradients to adjust the shared variables.
apply_gradient_op = optimizer.apply_gradients(grads, global_step=self.global_step)
self.train_op = apply_gradient_op
def add_init_op(self):
## TODO: check that already initialize variables don't get
# reinitialized (so that models don't collide with each other)
self.init = tf.global_variables_initializer()
def add_summary(self, sess):
# tensorboard stuff
self.merged = tf.summary.merge_all()
self.file_writer = tf.summary.FileWriter(self.config.output_path, sess.graph)
'''
Prepare a feed dict for the specified model based on the given input batch
Fills tf variables and placeholders with necessary values for sentence batch
bio_data_sentence_list: list of BIODataSentence objects
dropout_keep_prob: inverse dropout rate: 1.0 means keep all data
learning_rate: optimizer learning rate
'''
def prepare_feed_dict(self, bio_data_sentence_batch, dropout_keep_prob=None, learning_rate=None):
input_batch = []
labels_batch = []
sequence_lengths = []
if USE_DYNAMIC_PADDING:
max_length = max([len(s.labels) for s in bio_data_sentence_batch])
self.logger.debug('max_length (per-batch dynamic): %d' % (max_length))
else:
max_length = MAX_UNIT_COUNT
self.logger.debug('max_length (per-session static): %d' % (max_length))
assert max_length <= MAX_UNIT_COUNT
for sidx, sent in enumerate(bio_data_sentence_batch): # sent:BIODataSentence
## for now, only using first input
input_batch.append(sent.get_inputs_padded(
input_idx=0,
max_length=max_length,
padding_token=BIOLexicon.PAD_TOK,
embed_id_dict=self.unit0_to_idx,
embed_oov_token=BIOLexicon.UNK_TOK))
labels_batch.append(sent.get_labels_padded(
max_length=max_length,
padding_token=BIOLexicon.PAD_TOK,
embed_id_dict=self.label_to_idx,
embed_oov_token=BIOLexicon.UNK_TOK))
sequence_lengths.append(sent.sentence_length)
self.logger.debug('input_batch[%d] (len=%d): %s' % (sidx, len(input_batch[sidx]), str(input_batch[sidx])))
self.logger.debug('labels_batch[%d] (len=%d): %s' % (sidx, len(labels_batch[sidx]), str(labels_batch[sidx])))
self.logger.debug('sequence_length[%d]: %d' % (sidx, sequence_lengths[sidx]))
assert len(input_batch[sidx]) == len(labels_batch[sidx])
feed = {}
feed[self.unit0_ids] = input_batch
feed[self.sequence_lengths] = sequence_lengths
feed[self.labels] = labels_batch
#actual_batch_size = len(bio_data_sentence_batch)
## FIXME: during inference, we do a concat and the concat should work
# as we expect. always fill batch to may size for safety
#actual_batch_size = self.config.batch_size
assert len(bio_data_sentence_batch) == self.config.batch_size
feed[self.batch_split_sizes] = fixed_batch_split_sizes
if self.dropout_keep_prob != None:
feed[self.dropout_keep_prob] = dropout_keep_prob
if self.learning_rate != None:
feed[self.learning_rate] = learning_rate
return feed, sequence_lengths
def run_epoch(self, sess, train, dev, epoch):
'''
Performs one complete pass over the train set and evaluates on dev
Args:
sess: tensorflow session
train: large BIODataSentence list (training set)
dev: large BIODataSentence list (dev set)
epoch: (int) number of the epoch
'''
nbatches = len(train) // self.config.batch_size
if len(train) % self.config.batch_size != 0:
nbatches += 1
# hmm..couldn't this be slightly inefficient? seems like dynamic graph
# almost
prog = Progbar(target=nbatches)
for i, sent_batch in enumerate(minibatches(train, self.config.batch_size, always_fill=True)):
#if i % 100 == 0:
fd, _ = self.prepare_feed_dict( \
bio_data_sentence_batch=sent_batch, \
dropout_keep_prob=self.config.dropout_keep_prob, \
learning_rate=self.config.learning_rate)
#_, train_loss, summary = sess.run([self.train_op, self.loss, self.merged], feed_dict=fd)
#_, train_loss = sess.run([self.train_op, self.loss], feed_dict=fd)
#prog.update(i + 1, [('train loss', train_loss)])
loss_ops_to_add = [self.loss[z] for z in range(NUM_GPUS)]
_, losses = sess.run([self.train_op, loss_ops_to_add], feed_dict=fd)
mean_loss = np.mean(losses)
#prog.update(i + 1, [('train loss', train_loss)])
prog.update(i + 1, [('train loss', mean_loss)]) # test
# tensorboard
#if i % 10 == 0:
#self.file_writer.add_summary(summary, epoch*nbatches + i)
acc, f1, mod_p = self.run_evaluate(sess, dev)
self.logger.info('- dev acc {:04.2f} - f1 {:04.2f} - mod prec {:04.2f}'.format(100*acc, 100*f1, 100*mod_p))
return acc, f1
def predict_batch(self, sess, sents):
'''
Args:
sess: a tensorflow session
sents: list of BIODataSentence objects (batch)
(labels can be filled with PAD or other reserved value by
default, or may be passed in as gold value for convenience)
Returns:
pred_sents: list of new BIODataSentence objects filled with
predicted label data
'''
# ?? enforce?
# assert len(sents) == self.config.batch_size
cumulative_sequence_length = 0
total_inference_time = 0
total_viterbi_time = 0
num_sentences = 0
# dropout_keep_prob forced to 1.0 at inference time
fd, sequence_lengths = self.prepare_feed_dict( \
bio_data_sentence_batch=sents, \
dropout_keep_prob=1.0)
assert len(sequence_lengths) == len(sents)
pred_sents = copy.deepcopy(sents)
for sidx, s in enumerate(pred_sents):
# s.labels = []
assert sequence_lengths[sidx] == len(s.inputs)
cumulative_sequence_length += sequence_lengths[sidx]
# for i in range(sequence_lengths[sidx]):
# s.labels.append(BIOLexicon.PAD_TOK)
if self.print_time_details:
if args.use_static_padding:
self.logger.info('... Fixed batch sequence length: %d' % MAX_UNIT_COUNT)
else:
self.logger.info('... Max batch sequence length: %d' % max(sequence_lengths))
## TODO: detect batch extension len(sents) % self.config.batch_size remove this extra remainder from the results
if self.config.crf:
logits_exec = []
transition_params_exec = []
for i in range(NUM_GPUS):
with tf.device('/gpu:%d' % i):
logits = self.logits_infer[i]
logits_exec.append(logits)
pred = self.labels_pred[i]
transition_params_exec.append(self.transition_params[i])
tf.get_variable_scope().reuse_variables()
viterbi_sequences = []
total_ff_time = current_milli_time()
all_logits, all_transition_params = sess.run([logits_exec, transition_params_exec],
feed_dict=fd)
total_ff_time = current_milli_time() - total_ff_time
total_inference_time = total_ff_time
if self.print_time_details:
self.logger.info('...... Feed forward time: %.4fms/unit (batch total %.4fms)' % (total_ff_time / cumulative_sequence_length, total_ff_time))
sidx = 0
for gpu_idx, (this_gpu_logits, this_gpu_transition_params) in enumerate(zip(all_logits, all_transition_params)):
# logits may be longer due to padding
this_gpu_sequence_lengths = sequence_lengths[sidx:sidx+len(this_gpu_logits)]
# iterate over the sentences
for logit, sequence_length in zip(this_gpu_logits, this_gpu_sequence_lengths):
if sidx >= len(sents):
self.logger.info('... Breaking before padding during inference')
break # ignore extra padding
# keep only the valid time steps
logit = logit[:sequence_length]
viterbi_time = current_milli_time()
viterbi_sequence, viterbi_score = tf.contrib.crf.viterbi_decode(
logit, this_gpu_transition_params)
viterbi_time = current_milli_time() - viterbi_time
total_viterbi_time += viterbi_time
viterbi_sequences.append(viterbi_sequence)
sidx += 1
num_sentences += 1
total_inference_time += total_viterbi_time
if self.print_time_details:
self.logger.info('...... Viterbi time: %.4fms/unit (batch total %.4fms)' % (total_viterbi_time / cumulative_sequence_length, total_viterbi_time))
self.logger.info('... Total inference time: %.4fms/unit (batch total %.4fms)' % (total_inference_time / cumulative_sequence_length, total_inference_time))
for sidx, s in enumerate(pred_sents):
s.labels = []
#print('sequence_lengths[sidx]', sequence_lengths[sidx])
#print('viterbi_sequences[sidx]', viterbi_sequences[sidx])
for i in range(sequence_lengths[sidx]):
#print('i', i)
#print('viterbi_sequences[sidx][i]', viterbi_sequences[sidx][i])
## FIXME: is it possible for the NN to return a non-sensible value here?
s.labels.append(self.idx_to_label[viterbi_sequences[sidx][i]])
#print('s.labels', s.labels)
#return viterbi_sequences, sequence_lengths
else:
## TODO
labels_pred_exec = []
for i in range(NUM_GPUS):
with tf.device('/gpu:%d' % i):
logits = self.logits_infer[i]
pred = self.labels_pred[i]
labels_pred_exec.append(pred)
tf.get_variable_scope().reuse_variables()
labels_pred_concat = tf.concat(labels_pred_exec, axis=0)
total_ff_time = current_milli_time()
labels_pred = sess.run(labels_pred_concat, feed_dict=fd)
total_ff_time = current_milli_time() - total_ff_time
total_inference_time = total_ff_time
if self.print_time_details:
self.logger.info('... Total inference time: %.4fms/unit (batch total %.4fms)' % (total_inference_time / cumulative_sequence_length, total_inference_time))
## TODO: check sequence_lengths against input lengths
for sidx, s in enumerate(pred_sents):
#print('crf=false, sidx=', sidx)
if sidx >= len(sents):
self.logger.info('... Breaking before padding during inference')
break # ignore extra padding
s.labels = []
for i in range(sequence_lengths[sidx]):
## FIXME: is it possible for the NN to return a non-sensible value here?
s.labels.append(self.idx_to_label[labels_pred[sidx][i]])
#print('s.labels', s.labels)
#return labels_pred, sequence_lengths
return pred_sents
def run_evaluate(self, sess, test):
'''
Evaluates performance on specified test/dev set
Args:
sess: tensorflow session
test: large BIODataSentence list (dev/test set)
'''
nbatches = len(test) // self.config.batch_size
if len(test) % self.config.batch_size != 0:
nbatches += 1
correct_preds = 0
total_preds = 0
total_correct = 0
correct_mod = 0
total_mod = 0
accs = []
#prog = Progbar(target=nbatches)
# always fill to match batch size by wrapping around if necessary
for i, gold_sent_batch in enumerate(minibatches(test, self.config.batch_size, always_fill=True)):
if i % 50 == 0:
self.logger.info('Evaluate: batch %d/%d' % (i+1, nbatches))
self.print_time_details = True
else:
self.print_time_details = False
pred_sent_batch = self.predict_batch(sess, gold_sent_batch)
assert len(gold_sent_batch) == len(pred_sent_batch)
for sidx in range(len(gold_sent_batch)):
gold_chunks = gold_sent_batch[sidx].get_label_chunks()
pred_chunks = pred_sent_batch[sidx].get_label_chunks()
correct_chunks = gold_chunks & pred_chunks
self.logger.debug('gold_chunks: ' + str(sorted(gold_chunks)))
self.logger.debug('pred_chunks: ' + str(sorted(pred_chunks)))
for (chunk_idx, chunk_label) in gold_chunks:
if chunk_label.startswith('MOD') or chunk_label.startswith('B-MOD') or chunk_label.startswith('I-MOD'):
total_mod += 1
for (chunk_idx, chunk_label) in correct_chunks:
if chunk_label.startswith('MOD') or chunk_label.startswith('B-MOD') or chunk_label.startswith('I-MOD'):
correct_mod += 1
correct_preds += len(correct_chunks)
total_preds += len(pred_chunks)
total_correct += len(gold_chunks)
accs += map(lambda items: items[0] == items[1], list(zip(gold_sent_batch[sidx].labels, pred_sent_batch[sidx].labels)))
self.logger.info('correct_preds: ' + str(correct_preds))
self.logger.info('total_mod: ' + str(total_mod))
self.logger.info('total_preds: ' + str(total_preds))
self.logger.info('total_correct: ' + str(total_correct))
p = correct_preds / total_preds if correct_preds > 0 else 0
r = correct_preds / total_correct if correct_preds > 0 else 0
mod_p = correct_mod / total_mod if correct_mod > 0 else 0
f1 = 2 * p * r / (p + r) if correct_preds > 0 else 0
acc = np.mean(accs)
return acc, f1, mod_p
def train(self, sent_train, sent_dev):
best_score = 0
saver = tf.train.Saver()
# for early stopping
nepoch_no_imprv = 0
#config = tf.ConfigProto()
#config.gpu_options.allow_growth = True
#with tf.Session(config=config) as sess:
with tf.Session(config=tf.ConfigProto(allow_soft_placement=SOFT_PLACEMENT)) as sess:
sess.run(self.init)
# tensorboard
self.add_summary(sess)
for epoch in range(self.config.nepochs):
self.logger.info('Epoch {:} out of {:}'.format(epoch + 1, self.config.nepochs))
if not args.no_shuffle:
self.logger.debug('Shuffling training set...')