[2b4aea]: / model.py

Download this file

732 lines (616 with data), 36.2 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
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
""" Code for the MetaPred algorithm and network architecture. """
import numpy as np
import sklearn
import tensorflow as tf
import os, time, shutil, collections
import tensorflow.contrib.layers as layers
from tensorflow.contrib.rnn import RNNCell
from tensorflow.python.platform import flags
FLAGS = flags.FLAGS
PADDING_ID = 1016
WORDS_NUM = 1017
MASK_ARRAY = [[1.]] * PADDING_ID + [[0.]] + [[1.]] * (WORDS_NUM - PADDING_ID - 1)
SUMMARY_INTERVAL = 100
SAVE_INTERVAL = 1000
PRINT_INTERVAL = 100
TEST_PRINT_INTERVAL = PRINT_INTERVAL*5
class BaseModel(object):
"""
Base Model for basic networks with sequential data, i.e., RNN, CNN.
"""
def __init__(self):
self.regularizers = []
def convert_to_array(self, data):
'''convert other type to numpy array'''
if type(data) is not np.ndarray:
# data = np.array(data)
data = data.toarray() # convert sparse matrices
return data
# Helper methods.
def _get_path(self, folder):
path = '../../models/'
return os.path.join(path, folder, self.dir_name)
def _get_session(self, sess=None):
'''Restore parameters if no session given.'''
if sess is None:
sess = tf.Session(graph=self.graph)
filename = tf.train.latest_checkpoint(self._get_path('checkpoints'))
self.op_saver.restore(sess, filename)
return sess
def _get_prediction(self, logits):
'''Return the predicted classes.'''
with tf.name_scope('prediction'):
prediction = tf.argmax(logits, axis=1)
return prediction
def loss_func(self, pred, label):
'''cross entropy'''
# Note - with tf version <=0.12, this loss has incorrect 2nd derivatives
label = tf.one_hot(label, FLAGS.n_classes)
return tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=label) / FLAGS.update_batch_size
class MetaPred(BaseModel):
def __init__(self, data_loader, meta_lr=1e-3, update_lr=1e-2, test_num_updates=-1):
"""
Args:
dim_input: dimension of input data (for mlps)
n_tasks: task number including both source and target
meta_lr: the base learning rate of the generator
update_lr: step size alpha for inner gradient update
"""
super().__init__()
self.data_loader = data_loader
self.dim_input = data_loader.dim_input
self.n_tasks = data_loader.n_tasks
self.meta_lr = meta_lr
self.update_lr = update_lr
self.test_num_updates = test_num_updates
self.auc_stable = []
self.f1s_stable = []
self.weights_for_finetune = dict() # to store the value of learned params
print('method:', "meta-"+FLAGS.method, 'data shape:', self.dim_input, 'meta-bz:', FLAGS.meta_batch_size, 'update-bz:', FLAGS.update_batch_size, \
'num update:', FLAGS.num_updates, 'meta-lr:', meta_lr, 'update-lr:', update_lr)
if FLAGS.method == "cnn":
# sequential network (cnn) configuration
self.cnn_config(data_loader)
elif FLAGS.method == "rnn":
# sequential network (cnn) configuration
self.rnn_config(data_loader)
# Build the computational graph.
self.build_graph()
####################################### Networks #######################################
def weight_variable(self, shape, name='weights'):
if FLAGS.pretrain:
initial = self.pretrain_weights[name]
var = tf.Variable(initial_value=initial, name=name)
else:
initial = tf.truncated_normal_initializer(0, 0.1)
var = tf.get_variable(name, shape, tf.float32, initializer=initial)
if FLAGS.isReg:
self.regularizers.append(tf.nn.l2_loss(var))
tf.summary.histogram(var.op.name, var)
return var
def bias_variable(self, shape, initial=None, name='bias'):
if FLAGS.pretrain:
initial = self.pretrain_weights[name]
var = tf.Variable(initial_value=initial, name=name)
else:
initial = tf.constant_initializer(0.1)
var = tf.get_variable(name, shape, tf.float32, initializer=initial)
if FLAGS.isReg:
self.regularizers.append(tf.nn.l2_loss(var))
tf.summary.histogram(var.op.name, var)
return var
############################### Fully Conneted Network #################################
# construct weights
def build_fc_weights(self, dim_in, weights):
for i, dim in enumerate(self.dim_hidden):
dim_out = dim
weights["fc_W"+str(i)] = self.weight_variable([int(dim_in), dim_out], name="fc_W"+str(i))
weights["fc_b"+str(i)] = self.bias_variable([dim_out], name="fc_b"+str(i))
dim_in = dim_out
return weights
def fc(self, x, W, b, relu=True):
"""Fully connected layer with Mout features."""
x = tf.matmul(x, W) + b
return tf.nn.relu(x) if relu else x
############################ Embedding Layer for SeqNet ################################
def build_emb_weights(self, weights):
weights["emb_W"] = tf.Variable(tf.random_normal([self.n_words, self.n_hidden], stddev=self.init_std), name="emb_W")
with tf.variable_scope("emb", reuse=tf.AUTO_REUSE) as scope:
weights["emb_mask_W"] = tf.get_variable("mask_padding", initializer=MASK_ARRAY, dtype="float32", trainable=False)
return weights
def embedding(self, x, Wemb, Wemb_mask):
_x = tf.nn.embedding_lookup(Wemb, x) # recs size is (batch_size, timesteps, code_size)
_x_mask = tf.nn.embedding_lookup(Wemb_mask, x)
# print (_x.get_shape())
# print (_x_mask.get_shape())
emb_vecs = tf.multiply(_x, _x_mask)
emb_vecs = tf.reduce_sum(emb_vecs, 2)
# print (emb_vecs.get_shape())
return emb_vecs
############################ Convolutional Neural Network ##############################
def cnn_config(self, data_loader, init_std=0.05):
# Network Parameters
self.init_std = init_std
self.n_hidden = 256 # hidden dimensions of embedding
self.n_hidden_1 = 128
self.n_hidden_2 = 128
self.n_words = data_loader.n_words
self.n_classes = FLAGS.n_classes
self.n_filters = 128
self.num_input = data_loader.dim_input
self.timesteps = data_loader.timesteps
self.code_size = data_loader.code_size
self.dim_hidden = [self.n_hidden_1, self.n_hidden_2, FLAGS.n_classes] # for AD
self.filter_sizes = [3, 4, 5]
self.learner = self.cnn_sequential
def build_conv_weights(self, weights):
for i, filter_size in enumerate(self.filter_sizes):
filter_shape = [filter_size, self.n_hidden, 1, self.n_filters]
weights["conv_W"+str(filter_size)] = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="conv_W"+str(filter_size))
weights["conv_b"+str(filter_size)] = tf.Variable(tf.constant(0.1, shape=[self.n_filters]), name="conv_b"+str(filter_size))
return weights
def conv(self, emb_vecs, weights, is_training=True):
'''Create a convolution + maxpool layer for each filter size'''
pooled_outputs = []
emb_expanded = tf.expand_dims(emb_vecs, -1)
# print(emb_expanded.get_shape())
for i, filter_size in enumerate(self.filter_sizes):
W = weights["conv_W"+str(filter_size)]
b = weights["conv_b"+str(filter_size)]
with tf.variable_scope("conv-maxpool-%s" % filter_size):
# Convolution Layer
conv_ = tf.nn.conv2d(
emb_expanded,
W,
strides=[1, 1, 1, 1],
padding="VALID",
name="conv")
# Apply nonlinearity
h = tf.nn.leaky_relu(tf.nn.bias_add(conv_, b), name="relu")
with tf.name_scope("bnorm{}".format(filter_size)) as scope:
h = layers.batch_norm(h, updates_collections=None,
decay=0.99,
scale=True, center=True,
is_training=is_training, reuse=tf.AUTO_REUSE, scope=scope)
# Maxpooling over the outputs
pooled = tf.nn.max_pool(
h,
ksize=[1, self.timesteps - filter_size + 1, 1, 1],
strides=[1, 1, 1, 1],
padding='VALID',
name="pool")
pooled_outputs.append(pooled)
# Combine all the pooled features
num_filters_total = self.n_filters * len(self.filter_sizes)
h_pool = tf.concat(pooled_outputs, 3)
h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total])
return h_pool_flat
def cnn_sequential(self, x, weights, dropout, reuse=False, is_training=True, type="source"):
xemb = self.embedding(x, weights["emb_W"], weights["emb_mask_W"])
# convolutional network
hout = self.conv(xemb, weights, is_training)
h_ = layers.dropout(hout, keep_prob=dropout)
for i, dim in enumerate(self.dim_hidden[:-1]):
h_ = self.fc(h_, weights["fc_W"+str(i)], weights["fc_b"+str(i)])
h_ = tf.nn.dropout(h_, dropout)
# Logits linear layer, i.e. softmax without normalization.
N, Min = h_.get_shape()
i = len(self.dim_hidden)-1
logits = self.fc(h_, weights["fc_W"+str(i)], weights["fc_b"+str(i)], relu=False)
return logits
############################ Recurrent Neural Network ##############################
def rnn_config(self, data_loader, init_std=0.05):
# Network Parameters
self.init_std = init_std
self.n_hidden = 256 # hidden dimensions of embedding
self.n_hidden_1 = 128
self.n_hidden_2 = 128
self.n_words = data_loader.n_words
self.num_input = data_loader.dim_input
self.n_classes = FLAGS.n_classes
self.timesteps = data_loader.timesteps
self.code_size = data_loader.code_size
self.dim_hidden = [self.n_hidden_1, self.n_hidden_2, FLAGS.n_classes]
self.learner = self.rnn_sequential
def build_lstm_weights(self, weights):
# # Keep W_xh and W_hh separate here as well to reuse initialization methods
# with tf.variable_scope(scope or type(self).__name__):
weights["lstm_W_xh"] = tf.get_variable('lstm_W_xh', [self.n_hidden, 4 * self.n_hidden],
initializer=self.orthogonal_initializer())
weights["lstm_W_hh"] = tf.get_variable('lstm_W_hh', [self.n_hidden, 4 * self.n_hidden],
initializer=self.lstm_identity_initializer(0.95),)
weights["lstm_b"] = tf.get_variable('lstm_b', [4 * self.n_hidden])
return weights
def lstm_identity_initializer(self, scale):
def _initializer(shape, dtype=tf.float32, partition_info=None):
"""Ugly cause LSTM params calculated in one matrix multiply"""
size = shape[0]
t = np.zeros(shape)
t[:, size:size * 2] = np.identity(size) * scale
t[:, :size] = self.orthogonal([size, size])
t[:, size * 2:size * 3] = self.orthogonal([size, size])
t[:, size * 3:] = self.orthogonal([size, size])
return tf.constant(t, dtype=dtype)
return _initializer
def orthogonal_initializer(self):
def _initializer(shape, dtype=tf.float32, partition_info=None):
return tf.constant(self.orthogonal(shape), dtype)
return _initializer
def orthogonal(self, shape):
flat_shape = (shape[0], np.prod(shape[1:]))
a = np.random.normal(0.0, 1.0, flat_shape)
u, _, v = np.linalg.svd(a, full_matrices=False)
q = u if u.shape == flat_shape else v
return q.reshape(shape)
def rnn_sequential(self, x, weights, dropout, reuse=False, is_training=True, type='source'):
# embedding
xemb = self.embedding(x, weights["emb_W"], weights["emb_mask_W"])
# recurrent neural networks
xemb = tf.unstack(xemb, self.timesteps, 1)
lstm_cell = LSTMCell(self.n_hidden, weights["lstm_W_xh"], weights["lstm_W_hh"], weights["lstm_b"])
#c, h
if type == "source":
W_state_c = tf.random_normal([(self.n_tasks-1)*FLAGS.update_batch_size, self.n_hidden], stddev=0.1)
W_state_h = tf.random_normal([(self.n_tasks-1)*FLAGS.update_batch_size, self.n_hidden], stddev=0.1)
elif type == "target":
W_state_c = tf.random_normal([FLAGS.update_batch_size, self.n_hidden], stddev=0.1)
W_state_h = tf.random_normal([FLAGS.update_batch_size, self.n_hidden], stddev=0.1)
# outputs, state = tf.nn.dynamic_rnn(lstm_cell, xemb, initial_state=(W_state_c, W_state_h), dtype=tf.float32)
outputs, state = tf.nn.static_rnn(lstm_cell, xemb, initial_state=(W_state_c, W_state_h), dtype=tf.float32)
_, hout = state
with tf.variable_scope("dropout"):
h_ = layers.dropout(hout, keep_prob=dropout)
for i, dim in enumerate(self.dim_hidden[:-1]):
h_ = self.fc(h_, weights["fc_W"+str(i)], weights["fc_b"+str(i)])
h_ = tf.nn.dropout(h_, dropout)
x_rep = tf.identity(h_)
# Logits linear layer, i.e. softmax without normalization.
N, Min = h_.get_shape()
i = len(self.dim_hidden)-1
logits = self.fc(h_, weights["fc_W"+str(i)], weights["fc_b"+str(i)], relu=False)
return logits, x_rep
def build_graph(self):
"""Build the computational graph of the model."""
self.graph = tf.Graph()
with self.graph.as_default():
# Inputs.
with tf.name_scope('inputs'):
self.input_s = tf.placeholder(tf.int32, (FLAGS.meta_batch_size, (self.n_tasks-1) * FLAGS.update_batch_size, self.timesteps, self.code_size), 'source_x')
self.input_t = tf.placeholder(tf.int32, (FLAGS.meta_batch_size, FLAGS.update_batch_size, self.timesteps, self.code_size), 'target_x')
self.label_s = tf.placeholder(tf.int64, (FLAGS.meta_batch_size, (self.n_tasks-1) * FLAGS.update_batch_size), 'source_y')
self.label_t = tf.placeholder(tf.int64, (FLAGS.meta_batch_size, FLAGS.update_batch_size), 'target_y')
self.ph_training = tf.placeholder(tf.bool, name='trainingFlag')
self.ph_dropout = tf.placeholder(tf.float32, (), 'dropout')
# Model.
# construct metatrain_ and metaval_
if FLAGS.method == "cnn" or FLAGS.method == "rnn":
self.build_model((self.input_s, self.input_t, self.label_s, self.label_t), prefix='metatrain_', is_training=self.ph_training)
# Initialize variables, i.e. weights and biases.
self.op_init = tf.global_variables_initializer()
self.op_weights = self.get_op_variables()
# Summaries for TensorBoard and Save for model parameters.
self.op_summary = tf.summary.merge_all()
self.op_saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES), max_to_keep=10)
print ('graph built!')
self.graph.finalize()
def get_op_variables(self):
if FLAGS.method == "cnn":
op_weights = dict()
op_var = tf.trainable_variables()
# embedding
op_weights["emb_W"] = [v for v in op_var if "emb_W" in v.name][0]
# cnn
for i, filter_size in enumerate(self.filter_sizes):
op_weights["conv_W"+str(filter_size)] = [v for v in op_var if "conv_W"+str(filter_size) in v.name][0]
op_weights["conv_b"+str(filter_size)] = [v for v in op_var if "conv_b"+str(filter_size) in v.name][0]
# fully connected
for i, dim in enumerate(self.dim_hidden):
op_weights["fc_W"+str(i)] = [v for v in op_var if "fc_W"+str(i) in v.name][0]
op_weights["fc_b"+str(i)] = [v for v in op_var if "fc_b"+str(i) in v.name][0]
elif FLAGS.method == "rnn":
op_weights = dict()
op_var = tf.trainable_variables()
# embedding
op_weights["emb_W"] = [v for v in op_var if "emb_W" in v.name][0]
# lstm
op_weights["lstm_W_xh"] = [v for v in op_var if "lstm_W_xh" in v.name][0]
op_weights["lstm_W_hh"] = [v for v in op_var if "lstm_W_hh" in v.name][0]
op_weights["lstm_b"] = [v for v in op_var if "lstm_b" in v.name][0]
# fully connected
for i, dim in enumerate(self.dim_hidden):
op_weights["fc_W"+str(i)] = [v for v in op_var if "fc_W"+str(i) in v.name ][0]
op_weights["fc_b"+str(i)] = [v for v in op_var if "fc_b"+str(i) in v.name][0]
return op_weights
def build_weights(self):
weights = {}
if FLAGS.method == "cnn":
weights = self.build_emb_weights(weights)
weights = self.build_conv_weights(weights)
weights = self.build_fc_weights(self.n_filters * len(self.filter_sizes), weights)
elif FLAGS.method == "rnn":
weights = self.build_emb_weights(weights)
weights = self.build_lstm_weights(weights)
weights = self.build_fc_weights(self.n_hidden, weights)
return weights
def build_model(self, input_tensors, prefix='metatrain_', is_training=True):
"""
Args:
input_tensors = []:
source_xb: [batch_size, (n_tasks-1)*update_batch_size, data_shape]
source_yb: [batch_size, (n_tasks-1)*update_batch_size, ]
target_xb: [batch_size, update_batch_size, data_shape]
target_yb: [batch_size, update_batch_size, ] i.e., querysz = 1
# update_batch_size: number of examples used for inner gradient update (K for K tasks)
# meta_batch_size: number of mate-batches sampled per meta-update
prefix: pretrain_/metatrain_/metaval_/metatest_, for training, we build train val and test network meanwhile.
"""
# source: training data for inner gradient, target: test data for meta gradient
source_xb, target_xb, source_yb, target_yb = input_tensors
# create or reuse network variable, not including batch_norm variable, therefore we need extra reuse mechnism
# to reuse batch_norm variables.
with tf.variable_scope('model', reuse=tf.AUTO_REUSE) as training_scope:
# Define the weights. weights is a dictionary
self.weights = weights = self.build_weights()
num_updates = max(self.test_num_updates, FLAGS.num_updates)
# target_preds_tasks[i] and target_losses_tasks[i] is the output and loss after i+1 gradient updates
source_pred_tasks, source_loss_tasks, source_acc_tasks, source_auc_tasks = [], [], [], [] # source and target has seperate loss
# and accuracies
target_losses_tasks = [[]]*num_updates # result of every updates for test data
target_preds_tasks = [[]]*num_updates # prediction
target_accs_tasks = [[]]*num_updates
target_aucs_tasks = [[]]*num_updates
def task_metalearn(input, reuse=True):
"""
Perform gradient descent for one task in the meta-batch.
Args:
source_x: [(n_tasks-1)*update_batch_size, data_shape]
source_y: [(n_tasks-1)*update_batch_size, ]
target_x: [update_batch_size, data_shape]
target_y: [update_batch_size, ]
training: training or not, for batch_norm
"""
source_x, target_x, source_y, target_y = input # map_fn only support one parameters, so we need to unpack from tuple
# print (source_x.get_shape())
# print (target_x.get_shape())
# print (source_y.get_shape())
# print (target_y.get_shape())
# record the op in t update step, each element is the results of the upate step.
target_preds, target_losses, target_accs, target_aucs, target_represents = [], [], [], [], []
# That's, to create variable, you must turn off reuse
source_pred, _ = self.learner(source_x, weights, self.ph_dropout, reuse=False, is_training=is_training, type="source")
# print (source_pred.get_shape())
source_loss = self.loss_func(source_pred, source_y)
source_acc = tf.contrib.metrics.accuracy(tf.argmax(tf.nn.softmax(source_pred), 1), source_y)
# compute gradients
grads = tf.gradients(source_loss, list(weights.values()))
if FLAGS.stop_grad: # if True, do not use second derivatives in meta-optimization (for speed)
grads = [tf.stop_gradient(grad) for grad in grads]
# grad and variable dict
gvs = dict(zip(weights.keys(), grads))
# theta_pi = theta - alpha * grads
fast_weights = dict(zip(weights.keys(), [weights[key] - tf.multiply(self.update_lr, gvs[key]) for key in weights.keys()]))
# fast_weights = dict(zip(weights.keys(), [weights[key] - self.update_lr*gvs[key] for key in weights.keys()]))
# use theta_pi for fast adaption
target_pred, target_represent = self.learner(target_x, fast_weights, self.ph_dropout, reuse=True, is_training=is_training, type="target")
target_loss = self.loss_func(target_pred, target_y)
target_preds.append(target_pred)
target_losses.append(target_loss)
target_represents.append(target_represent)
# continue to build T1-TK steps graph
for _ in range(1, num_updates): # i.e., num_updates = 4, update 3 times
# T_k loss on meta-train
# we need meta-train loss to fine-tune the task and meta-test loss to update theta
loss = self.loss_func(self.learner(source_x, fast_weights, self.ph_dropout, reuse=True, is_training=is_training, type="source")[0], source_y)
# compute gradients
grads = tf.gradients(loss, list(fast_weights.values()))
# compose grad and variable dict
gvs = dict(zip(fast_weights.keys(), grads))
# update theta_pi according to varibles
fast_weights = dict(zip(fast_weights.keys(), [fast_weights[key] - tf.multiply(self.update_lr, gvs[key])
for key in fast_weights.keys()]))
# forward on theta_pi
target_pred, target_represent = self.learner(target_x, fast_weights, self.ph_dropout, reuse=True, is_training=is_training, type="target")
# we need accumulate all meta-test losses to update theta
target_loss = self.loss_func(target_pred, target_y)
target_preds.append(target_pred)
target_losses.append(target_loss)
target_represents.append(target_represent)
task_output = [target_represents, source_pred, target_preds, source_loss, target_losses]
for j in range(num_updates):
target_accs.append(tf.contrib.metrics.accuracy(predictions=tf.argmax(tf.nn.softmax(target_preds[j]), 1), labels=target_y))
task_output.extend([source_acc, target_accs])
return task_output
if FLAGS.norm is not 'None': # batch norm or layer norm
# to initialize the batch norm vars, might want to combine this, and not run idx 0 twice.
unused = task_metalearn((source_xb[0], target_xb[0], source_yb[0], target_yb[0]), False)
out_dtype = [[tf.float32] * num_updates, tf.float32, [tf.float32] * num_updates, tf.float32, [tf.float32] * num_updates,
tf.float32, [tf.float32] * num_updates]
result = tf.map_fn(task_metalearn, elems=(source_xb, target_xb, source_yb, target_yb),
dtype=out_dtype, parallel_iterations=FLAGS.meta_batch_size, name='map_fn')
target_represents_tasks, source_pred_tasks, target_preds_tasks, source_loss_tasks, target_losses_tasks, \
source_acc_tasks, target_accs_tasks = result
## Performance & Optimization
# average loss
self.source_loss = source_loss = tf.reduce_sum(source_loss_tasks) / FLAGS.meta_batch_size
# [avgloss_T1, avgloss_T2, ..., avgloss_TK]
self.target_losses = target_losses = [tf.reduce_sum(target_losses_tasks[j]) / FLAGS.meta_batch_size
for j in range(num_updates)]
self.source_acc = source_acc = tf.reduce_sum(source_acc_tasks) / FLAGS.meta_batch_size
self.target_accs = target_accs = [tf.reduce_sum(target_accs_tasks[j]) / FLAGS.meta_batch_size
for j in range(num_updates)]
self.source_pred = source_pred_tasks
self.target_preds = target_preds_tasks[FLAGS.num_updates-1]
self.target_represent = target_represents_tasks[FLAGS.num_updates-1]
if self.ph_training is not False:
# meta-train optim
optimizer = tf.train.AdamOptimizer(self.meta_lr, name='meta_optim')
# meta-train gradients, target_losses[-1] is the accumulated loss across over tasks.
self.gvs = gvs = optimizer.compute_gradients(self.source_loss + self.target_losses[FLAGS.num_updates-1])
# update theta
self.metatrain_op = optimizer.apply_gradients(gvs)
## Summaries
# NOTICE: every time build model, support_loss will be added to the summary, but it's different.
tf.summary.scalar(prefix+'Pre-update loss', source_loss)
tf.summary.scalar(prefix+'Pre-update accuracy', source_acc)
for j in range(num_updates):
tf.summary.scalar(prefix+'Post-update accuracy, step ' + str(j+1), target_losses[j])
tf.summary.scalar(prefix+'Post-update accuracy, step ' + str(j+1), target_losses[j])
def compute_metrics(self, predictions, labels):
'''compute metrics score'''
fpr, tpr, _ = sklearn.metrics.roc_curve(labels, predictions)
auc = sklearn.metrics.auc(fpr, tpr)
ncorrects = sum(predictions == labels)
accuracy = sklearn.metrics.accuracy_score(labels, predictions)
ap = sklearn.metrics.average_precision_score(labels, predictions, 'micro')
f1score = sklearn.metrics.f1_score(labels, predictions, 'micro')
return auc, ap, f1score
# def evaluate(self, sample, label, sess=None, prefix="metaval_"):
def evaluate(self, episode, data_tuple_val, sess=None, prefix="metaval_"):
'''validate meta learning model'''
target_acc,target_vals,target_preds = [], [], []
size = len(episode)
for begin in range(0, size, FLAGS.meta_batch_size):
end = begin + FLAGS.meta_batch_size
end = min([end, size])
if end-begin < FLAGS.meta_batch_size: break
batch_idx = range(begin, end)
sample, label = self.get_feed_data(episode, batch_idx, data_tuple_val, is_training=False)
X_tensor_s = self.convert_to_array(sample[:, :(self.n_tasks-1) * FLAGS.update_batch_size, :, :])
X_tensor_t = self.convert_to_array(sample[:, (self.n_tasks-1) * FLAGS.update_batch_size:, :, :])
y_tensor_s = self.convert_to_array(label[:, :(self.n_tasks-1) * FLAGS.update_batch_size])
y_tensor_t = self.convert_to_array(label[:, (self.n_tasks-1) * FLAGS.update_batch_size:])
feed_dict = {self.input_s: X_tensor_s, self.input_t: X_tensor_t, self.label_s: y_tensor_s, self.label_t: y_tensor_t, self.ph_dropout: 1, self.ph_training: False}
input_tensors = [self.target_preds, self.target_accs[FLAGS.num_updates-1]]
metaval_target_preds, metaval_target_accs = sess.run(input_tensors, feed_dict)
target_acc.append(metaval_target_accs)
target_preds.append(metaval_target_preds)
target_vals.append(y_tensor_t)
target_vals = np.array(target_vals).flatten()
target_preds = np.array([np.argmax(preds, axis=2) for preds in target_preds]).flatten()
target_acc = np.mean(target_acc)
target_auc, target_ap, target_f1 = self.compute_metrics(target_preds, target_vals)
return target_acc, target_auc, target_ap, target_f1
def get_feed_data(self, episode, batch_idx, data_tuple, is_training, is_show=False):
''' given batch indices, get data array from the generated index episodes'''
n_samples_per_task = FLAGS.update_batch_size
data_s, data_t, label_s, label_t = data_tuple
# generate episode
sample, label = [], []
batch_count = 0
for i in range(len(batch_idx)): # the 1st dimension is the batch size
# i.e., sample 16 patients from selected tasks
# len of spl and lbl: 4 * 16
spl, lbl = [], [] # samples and labels in one episode
bi = batch_idx[i]
data_idx = episode[bi] # all tasks are merged: [task1, task2, ..., tastn], where taskn is target
n_source = 0
for i in range(len(self.data_loader.source)):
s_idx = data_idx[i*n_samples_per_task:(i+1)*n_samples_per_task]
spl.extend(data_s[i][s_idx])
lbl.extend(label_s[i][s_idx])
n_source += n_samples_per_task
### do not keep pos/neg ratio
if is_training:
t_idx = data_idx[n_source:]
spl.extend(data_t[0][t_idx])
lbl.extend(label_t[0][t_idx])
else:
t_idx = data_idx[n_source:]
spl.extend(data_t[t_idx])
lbl.extend(label_t[t_idx])
batch_count += 1
# add meta_batch
sample.append(spl)
label.append(lbl)
sample = np.array(sample, dtype="float32")
label = np.array(label, dtype="float32")
return sample, label
def fit(self, episode, episode_val, ifold, exp_string, model_file = None):
sess = tf.Session(graph=self.graph)
if FLAGS.resume or not FLAGS.train:
model_file = tf.train.latest_checkpoint(FLAGS.logdir + '/' + exp_string)
if model_file:
ind1 = model_file.index('model')
print("Restoring model weights from " + model_file)
self.op_saver.restore(sess, model_file)
sess.run(self.op_init)
if FLAGS.log:
train_writer = tf.summary.FileWriter(FLAGS.logdir + '/' + exp_string, sess.graph)
# load data for metatrain
data_tuple = (self.data_loader.data_s, self.data_loader.data_t, self.data_loader.label_s, self.data_loader.label_t)
# load data for metaeval
data_tuple_val = (self.data_loader.data_s, self.data_loader.data_tt_val[ifold], self.data_loader.label_s, self.data_loader.label_tt_val[ifold])
prelosses, postlosses, preaccs, postaccs = [], [], [], []
# train for meta_iteartion epoches
indices = collections.deque()
for itr in range(FLAGS.metatrain_iterations):
feed_dict = {}
input_tensors = [self.metatrain_op]
if itr % SUMMARY_INTERVAL == 0 or itr % PRINT_INTERVAL == 0:
input_tensors.extend([self.op_summary, self.source_loss, self.target_losses[FLAGS.num_updates-1]])
input_tensors.extend([self.source_acc, self.target_accs[FLAGS.num_updates-1], self.target_preds])
if len(indices) < FLAGS.meta_batch_size:
indices.extend(np.random.permutation(len(episode)))
batch_idx = [indices.popleft() for i in range(FLAGS.meta_batch_size)]
sample, label = self.get_feed_data(episode, batch_idx, data_tuple, is_training=True)
X_tensor_s = self.convert_to_array(sample[:, :(self.n_tasks-1) * FLAGS.update_batch_size, :, :])
X_tensor_t = self.convert_to_array(sample[:, (self.n_tasks-1) * FLAGS.update_batch_size:, :, :])
y_tensor_s = self.convert_to_array(label[:, :(self.n_tasks-1) * FLAGS.update_batch_size])
y_tensor_t = self.convert_to_array(label[:, (self.n_tasks-1) * FLAGS.update_batch_size:])
feed_dict = {self.input_s: X_tensor_s, self.input_t: X_tensor_t, self.label_s: y_tensor_s, self.label_t: y_tensor_t, self.ph_dropout: FLAGS.dropout, self.ph_training: True}
result = sess.run(input_tensors, feed_dict)
if itr % SUMMARY_INTERVAL == 0:
prelosses.append(result[-5])
preaccs.append(result[-3])
if FLAGS.log:
train_writer.add_summary(result[1], itr)
postlosses.append(result[-4])
postaccs.append(result[-2])
postauc, postap, postf1 = self.compute_metrics(np.argmax(result[-1], axis=2).flatten(), y_tensor_t.flatten())
if (itr!=0) and itr % PRINT_INTERVAL == 0:
print_str = 'Iteration ' + str(itr)
print_str += ': sacc: ' + str(np.mean(preaccs)) + ', tacc: ' + str(np.mean(postaccs))
print_str += " tauc: " + str(postauc) + " tap: " + str(postap) + " tf1: " + str(postf1)
print(print_str)
preaccs, postaccs = [], []
prelosses, postlosses = [], []
if (itr!=0) and itr % SAVE_INTERVAL == 0:
self.op_saver.save(sess, FLAGS.logdir + '/' + exp_string + '/model' + str(itr))
if (itr!=0) and itr % TEST_PRINT_INTERVAL == 0:
target_accs, target_aucs, target_ap, target_f1s = self.evaluate(episode_val, data_tuple_val, sess=sess, prefix="metaval_")
self.auc_stable.append(target_aucs)
self.f1s_stable.append(target_f1s)
print('Validation results: ' + "tAcc: " + str(target_accs) + ", tAuc: " + str(target_aucs) + ", tAP: " + str(target_ap) + ", tF1: " + str(target_f1s))
print ("---------------")
self.op_saver.save(sess, FLAGS.logdir + '/' + exp_string + '/model' + str(itr))
print ("---------------")
# store weights value for fine-tune
feed_dict = {}
for k in self.op_weights:
self.weights_for_finetune[k] = sess.run([self.op_weights[k]], feed_dict)[0]
return sess
class LSTMCell(RNNCell):
'''Vanilla LSTM implemented with same initializations as BN-LSTM'''
def __init__(self, num_units, W_xh, W_hh, bias):
self.num_units = num_units
self.W_xh = W_xh
self.W_hh = W_hh
self.bias = bias
@property
def state_size(self):
return (self.num_units, self.num_units)
@property
def output_size(self):
return self.num_units
def __call__(self, x, state, scope=None):
with tf.variable_scope(scope or type(self).__name__, reuse=tf.AUTO_REUSE):
c, h = state
# hidden = tf.matmul(x, W_xh) + tf.matmul(h, W_hh) + bias
# improve speed by concat.
concat = tf.concat([x, h], 1)
W_both = tf.concat([self.W_xh, self.W_hh], 0)
hidden = tf.matmul(concat, W_both) + self.bias
i, j, f, o = tf.split(hidden, 4, axis=1)
new_c = c * tf.sigmoid(f) + tf.sigmoid(i) * tf.tanh(j)
new_h = tf.tanh(new_c) * tf.sigmoid(o)
return new_h, (new_c, new_h)