-
Notifications
You must be signed in to change notification settings - Fork 15
/
ipca_v2.py
557 lines (504 loc) · 23.5 KB
/
ipca_v2.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
"""Helper file to run the discover concept algorithm in the toy dataset."""
# lint as: python3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import itertools
from absl import app
import keras
from keras.activations import sigmoid
import keras.backend as K
from keras.layers import Input
from keras.layers import Lambda
from keras.layers import Layer
from keras.models import Model
from keras.layers import Flatten
from keras.optimizers import Adam
from keras.optimizers import SGD
import numpy as np
from numpy import inf
from numpy.random import seed
from scipy.special import comb
import tensorflow as tf
seed(0)
tf.random.set_seed(0)
# global variables
init = keras.initializers.RandomUniform(minval=-0.5, maxval=0.5, seed=None)
batch_size = 256
step = 200
min_weight_arr = []
min_index_arr = []
concept_arr = {}
class Weight(Layer):
"""Simple Weight class."""
def __init__(self, dim, **kwargs):
self.dim = dim
super(Weight, self).__init__(**kwargs)
def build(self, input_shape):
# creates a trainable weight variable for this layer.
self.kernel = self.add_weight(
name='proj', shape=self.dim, initializer=init, trainable=True)
super(Weight, self).build(input_shape)
def call(self, x):
return self.kernel
def compute_output_shape(self, input_shape):
return self.dim
def given_loss(loss1):
"""creates loss for topic model"""
def loss(y_true, y_pred):
return (tf.reduce_mean(input_tensor=loss1(y_true, y_pred)))
return loss
def topic_loss(topic_prob_n, topic_vector_n, n_concept, f_input, loss1):
"""creates loss for topic model"""
def loss(y_true, y_pred):
return (1.0*tf.reduce_mean(input_tensor=loss1(y_true, y_pred))
- 10.0*tf.reduce_mean(input_tensor=(tf.nn.top_k(K.transpose(K.reshape(topic_prob_n,(-1,n_concept))),k=2,sorted=True).values))
+ 10.0*tf.reduce_mean(input_tensor=(K.dot(K.transpose(topic_vector_n), topic_vector_n) - np.eye(n_concept)))
)
return loss
def topic_loss_toy(topic_prob_n, topic_vector_n, n_concept, f_input, loss1, para = 1.0):
"""creates loss for topic model"""
def loss(y_true, y_pred):
return (1.0*tf.reduce_mean(input_tensor=loss1(y_true, y_pred))\
- 0.1*tf.reduce_mean(input_tensor=(tf.nn.top_k(K.transpose(K.reshape(topic_prob_n,(-1,n_concept))),k=32,sorted=True).values))
+ 0.1*tf.reduce_mean(input_tensor=(K.dot(K.transpose(topic_vector_n), topic_vector_n) - np.eye(n_concept)))
)
return loss
def topic_loss_nlp(topic_prob_n, topic_vector_n, n_concept, f_input, loss1, para = 1.0):
"""creates loss with regularization (for NLP)"""
def loss(y_true, y_pred):
return (tf.reduce_mean(input_tensor=loss1(y_true, y_pred))
- 0.1*tf.reduce_mean(input_tensor=(tf.nn.top_k(K.transpose(K.reshape(topic_prob_n,(-1,n_concept))),k=16,sorted=True).values))
+ 0.1 *tf.reduce_mean(input_tensor=(K.dot(K.transpose(topic_vector_n), topic_vector_n) - np.eye(n_concept)))
)
return loss
def mean_sim(topic_prob_n,n_concept):
"""creates loss for topic model"""
def loss(y_true, y_pred):
return 1*tf.reduce_mean(input_tensor=tf.nn.top_k(K.transpose(K.reshape(topic_prob_n,(-1,n_concept))),k=32,sorted=True).values)
return loss
def sample_binary(n_concept, n_sample, pp=0.2):
"""sample binary vectors for shapley calculation"""
binary_matrix = np.zeros((n_sample,n_concept))
remain = -1
for i in range(n_sample):
binary_matrix[i,:] = np.random.choice(2, n_concept, p=[1-pp, pp])
return binary_matrix
def get_completeness(predict,
f_train,
y_train,
f_val,
y_val,
n_concept,
topic_vector_init,
verbose=False,
epochs=20,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.5,
load=False):
"""Returns main function of topic model."""
f_input = Input(shape=(f_train.shape[1],f_train.shape[2],f_train.shape[3]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(f_input)
topic_vector = Weight((f_train.shape[3], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=3, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[3]))(f_input)
rec_layer_1 = Lambda(lambda x:K.relu(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
pred = predict(rec_layer_2)
topic_model_pr = Model(inputs=f_input, outputs=pred)
topic_model_pr.layers[-1].trainable = True
if load:
topic_model_pr.load_weights(load)
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
topic_model_pr.layers[1].set_weights([topic_vector_init])
topic_model_pr.layers[1].trainable = False
topic_model_pr.layers[-1].trainable = False
topic_model_pr.compile(
loss=given_loss(loss1=loss1),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
topic_model_pr.fit(
f_train,
y_train,
batch_size=128,
epochs=epochs,
validation_data=(f_val, y_val),
verbose=verbose)
return 0
def topic_model_new_crop(predict,
f_train_crop,
f_train,
y_train,
f_val,
y_val,
n_concept,
verbose=False,
epochs=20,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.5,
load=False):
"""Returns main function of topic model."""
# f_input size (None, 8,8,2048)
#input = Input(shape=(299,299,3), name='input')
#f_input = get_feature(input)
f_crop = Input(shape=(4, f_train.shape[1],f_train.shape[2],f_train.shape[3]), name='f_input_crop')
f_crop_n = Lambda(lambda x:K.l2_normalize(x,axis=(4)))(f_crop)
f_input = Input(shape=(f_train.shape[1],f_train.shape[2],f_train.shape[3]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(f_input)
# topic vector size (2048,n_concept)
topic_vector = Weight((f_train.shape[3], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
topic_prob_crop_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_crop_n, topic_vector_n])
# topic prob = batchsize * 8 * 8 * n_concept
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
#topic_prob_pos = Lambda(lambda x: K.maximum(x,-1000))(topic_prob)
#print(K.sum(topic_prob, axis=3, keepdims=True))
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=3, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
# rec size is batchsize * 8 * 8 * 2048
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[3]))(f_input)
#rec = Lambda(lambda x:K.dot(x[0],K.transpose(x[1])))([topic_prob_pos, topic_vector])
#scale_value = Weight((1,1,1,1))(f_input)
#bias_value = Weight((1,1,1,2048))(f_input)
#scaled_rec1 = Lambda(lambda x: x[0] * x[1])([rec, scale_value])
#scaled_rec2 = Lambda(lambda x: x[0] + x[1])([scaled_rec1, bias_value])
rec_layer_1 = Lambda(lambda x:K.relu(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
#rec_layer_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(rec_layer)
pred = predict(rec_layer_2)
topic_model_pr = Model(inputs=[f_input,f_crop], outputs=pred)
topic_model_pr.layers[-1].trainable = True
#topic_model_pr.layers[1].trainable = False
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
# These depend on the optimizer class
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
# Later when you want to reset the optimizer
#K.get_session().run(optimizer_reset)
#print(metric1)
metric1.append(mean_sim(topic_prob_crop_n, n_concept))
topic_model_pr.compile(
loss=topic_loss(topic_prob_crop_n, topic_vector_n, n_concept, f_input, loss1=loss1),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
if load:
topic_model_pr.load_weights(load)
#topic_model_pr.layers[-3].set_weights([np.zeros((2048,1000))])
#topic_model_pr.layers[-3].trainable = False
return topic_model_pr, optimizer_reset, optimizer, topic_vector_n, n_concept, f_input
def topic_model_new(predict,
f_train,
y_train,
f_val,
y_val,
n_concept,
verbose=False,
epochs=20,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.5,
load=False):
"""Returns main function of topic model."""
# f_input size (None, 8,8,2048)
#input = Input(shape=(299,299,3), name='input')
#f_input = get_feature(input)
f_input = Input(shape=(f_train.shape[1],f_train.shape[2],f_train.shape[3]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(f_input)
# topic vector size (2048,n_concept)
topic_vector = Weight((f_train.shape[3], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
# topic prob = batchsize * 8 * 8 * n_concept
#topic_prob = Weight_instance((n_concept))(f_input)
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
#topic_prob_pos = Lambda(lambda x: K.maximum(x,-1000))(topic_prob)
#print(K.sum(topic_prob, axis=3, keepdims=True))
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=3, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
# rec size is batchsize * 8 * 8 * 2048
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[3]))(f_input)
#rec = Lambda(lambda x:K.dot(x[0],K.transpose(x[1])))([topic_prob_pos, topic_vector])
#scale_value = Weight((1,1,1,1))(f_input)
#bias_value = Weight((1,1,1,2048))(f_input)
#scaled_rec1 = Lambda(lambda x: x[0] * x[1])([rec, scale_value])
#scaled_rec2 = Lambda(lambda x: x[0] + x[1])([scaled_rec1, bias_value])
rec_layer_1 = Lambda(lambda x:K.relu(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
#rec_layer_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(rec_layer)
pred = predict(rec_layer_2)
topic_model_pr = Model(inputs=f_input, outputs=pred)
topic_model_pr.layers[-1].trainable = True
#topic_model_pr.layers[1].trainable = False
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
# These depend on the optimizer class
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
# Later when you want to reset the optimizer
#K.get_session().run(optimizer_reset)
#print(metric1)
metric1.append(mean_sim(topic_prob_n, n_concept))
topic_model_pr.compile(
loss=topic_loss(topic_prob_n, topic_vector_n, n_concept, f_input, loss1=loss1),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
if load:
topic_model_pr.load_weights(load)
#topic_model_pr.layers[-3].set_weights([np.zeros((2048,1000))])
#topic_model_pr.layers[-3].trainable = False
return topic_model_pr, optimizer_reset, optimizer, topic_vector_n, n_concept, f_input
def topic_model_nlp(predict,
f_train,
y_train,
f_val,
y_val,
n_concept,
verbose=False,
epochs=20,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.5,
load=False):
"""Returns main function of topic model."""
# f_input size (None, 8,8,2048)
#input = Input(shape=(299,299,3), name='input')
#f_input = get_feature(input)
f_input = Input(shape=(f_train.shape[1],f_train.shape[2]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(2)))(f_input)
# topic vector size (2048,n_concept)
topic_vector = Weight((f_train.shape[2], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
# topic prob = batchsize * 8 * 8 * n_concept
#topic_prob = Weight_instance((n_concept))(f_input)
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
#topic_prob_pos = Lambda(lambda x: K.maximum(x,-1000))(topic_prob)
#print(K.sum(topic_prob, axis=3, keepdims=True))
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=2, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
# rec size is batchsize * 8 * 8 * 2048
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[2]))(f_input)
rec_layer_1 = Lambda(lambda x:(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
rec_layer_f2 = Flatten()(rec_layer_2)
#rec_layer_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(rec_layer)
pred = predict(rec_layer_f2)
topic_model_pr = Model(inputs=f_input, outputs=pred)
topic_model_pr.layers[-1].trainable = True
#topic_model_pr.layers[1].trainable = False
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
# These depend on the optimizer class
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
# Later when you want to reset the optimizer
#K.get_session().run(optimizer_reset)
#print(metric1)
metric1.append(mean_sim(topic_prob_n, n_concept))
topic_model_pr.compile(
loss=topic_loss_nlp(topic_prob_n, topic_vector_n, n_concept, f_input, loss1=loss1),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
if load:
topic_model_pr.load_weights(load)
#topic_model_pr.layers[-3].set_weights([np.zeros((2048,1000))])
#topic_model_pr.layers[-3].trainable = False
return topic_model_pr, optimizer_reset, optimizer, topic_vector_n, n_concept, f_input
def topic_model_shap(predict,
f_train,
y_train,
f_val,
y_val,
n_concept,
verbose=False,
epochs=20,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.5,
load=False):
"""Returns main function of topic model."""
last_dim = len(f_train.shape)-1
print(last_dim)
f_input = Input(shape=(f_train.shape[1:]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(last_dim)))(f_input)
# topic vector size (2048,n_concept)
topic_vector = Weight((f_train.shape[-1], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
# topic prob = batchsize * 8 * 8 * n_concept
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
#topic_prob_pos = Lambda(lambda x: K.maximum(x,-1000))(topic_prob)
#print(K.sum(topic_prob, axis=3, keepdims=True))
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=last_dim, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
# rec size is batchsize * 8 * 8 * 2048
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[last_dim]))(f_input)
rec_layer_1 = Lambda(lambda x:K.relu(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
if last_dim==2:
rec_layer_2 = Flatten()(rec_layer_2)
#rec_layer_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(rec_layer)
pred = predict(rec_layer_2)
topic_model_pr = Model(inputs=f_input, outputs=pred)
topic_model_pr.layers[-1].trainable = False
topic_model_pr.layers[1].trainable = False
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
# These depend on the optimizer class
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
# Later when you want to reset the optimizer
#K.get_session().run(optimizer_reset)
#print(metric1)
metric1.append(mean_sim(topic_prob_n, n_concept))
topic_model_pr.compile(
loss=given_loss( loss1=loss1),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
if load:
topic_model_pr.load_weights(load)
#topic_model_pr.layers[-3].trainable = False
return topic_model_pr
def topic_model_new_toy(predict,
f_train,
y_train,
f_val,
y_val,
n_concept,
verbose=False,
metric1=['accuracy'],
opt='adam',
loss1=tf.nn.softmax_cross_entropy_with_logits,
thres=0.0,
load=False,
para = 0.5):
"""Returns main function of topic model."""
f_input = Input(shape=(f_train.shape[1],f_train.shape[2],f_train.shape[3]), name='f_input')
f_input_n = Lambda(lambda x:K.l2_normalize(x,axis=(3)))(f_input)
topic_vector = Weight((f_train.shape[3], n_concept))(f_input)
topic_vector_n = Lambda(lambda x: K.l2_normalize(x, axis=0))(topic_vector)
topic_prob = Lambda(lambda x:K.dot(x[0],x[1]))([f_input, topic_vector_n])
topic_prob_n = Lambda(lambda x:K.dot(x[0],x[1]))([f_input_n, topic_vector_n])
topic_prob_mask = Lambda(lambda x:K.cast(K.greater(x,thres),'float32'))(topic_prob_n)
topic_prob_am = Lambda(lambda x:x[0]*x[1])([topic_prob,topic_prob_mask])
topic_prob_sum = Lambda(lambda x: K.sum(x, axis=3, keepdims=True)+1e-3)(topic_prob_am)
topic_prob_nn = Lambda(lambda x: x[0]/x[1])([topic_prob_am, topic_prob_sum])
rec_vector_1 = Weight((n_concept, 500))(f_input)
rec_vector_2 = Weight((500, f_train.shape[3]))(f_input)
rec_layer_1 = Lambda(lambda x:K.relu(K.dot(x[0],x[1])))([topic_prob_nn, rec_vector_1])
rec_layer_2 = Lambda(lambda x:K.dot(x[0],x[1]))([rec_layer_1, rec_vector_2])
pred = predict(rec_layer_2)
topic_model_pr = Model(inputs=f_input, outputs=pred)
topic_model_pr.layers[-1].trainable = False
if opt =='sgd':
optimizer = SGD(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr,
optimizer.momentum, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
elif opt =='adam':
optimizer = Adam(lr=0.001)
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.compat.v1.variables_initializer(optimizer_state)
metric1.append(mean_sim(topic_prob_n, n_concept))
topic_model_pr.compile(
loss=topic_loss_toy(topic_prob_n, topic_vector_n, n_concept, f_input, loss1=loss1, para = para),
optimizer=optimizer,metrics=metric1)
print(topic_model_pr.summary())
if load:
topic_model_pr.load_weights(load)
return topic_model_pr, optimizer_reset, optimizer, topic_vector_n, n_concept, f_input
def get_acc(binary_sample, f_val, y_val_logit, shap_model, verbose=False):
"""Returns accuracy."""
acc = shap_model.evaluate(
[f_val, np.tile(np.array(binary_sample), (f_val.shape[0], 1))],
y_val_logit,
verbose=verbose)[1]
print(acc)
return acc
def shap_kernel(n, k):
"""Returns kernel of shapley in KernelSHAP."""
return (n-1)*1.0/((n-k)*k*comb(n, k))
def shap_kernel_adjust(n, k, p=0.5):
"""Returns kernel of shapley in KernelSHAP."""
return (n-1)*1.0/((n-k)*k*comb(n, k)) / (np.power(p,k)*np.power(1-p,n-k))
def get_shap(nc, f_train, y_train, f_val, y_val, topic_vec, model_shap, full_acc, null_acc, n_concept, get_acc_f):
"""Returns ConceptSHAP."""
inputs = list(itertools.product([0, 1], repeat=n_concept))
#\binary_sample, topic_vec, f_train, y_train, f_val, y_val, model_shap, verbose=False)
outputs = [(get_acc_f(k, topic_vec, f_train, y_train, f_val, y_val, model_shap, verbose=False)-null_acc)/
(full_acc-null_acc) for k in inputs]
kernel = [shap_kernel(nc, np.sum(ii)) for ii in inputs]
x = np.array(inputs)
y = np.array(outputs)
k = np.array(kernel)
k[k == inf] = 10000
xkx = np.matmul(np.matmul(x.transpose(), np.diag(k)), x)
xky = np.matmul(np.matmul(x.transpose(), np.diag(k)), y)
expl = np.matmul(np.linalg.pinv(xkx), xky)
return expl
def main(_):
return
if __name__ == '__main__':
app.run(main)