-
Notifications
You must be signed in to change notification settings - Fork 0
/
INVASE.py
337 lines (237 loc) · 12.3 KB
/
INVASE.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
'''
Personalized Variable Selection Code (PVS)
for ICLR 2019 Conference
'''
#%% Necessary packages
# 1. Keras
from keras.layers import Input, Dense, Multiply
from keras.layers import BatchNormalization
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras import regularizers
from keras import backend as K
# 2. Others
import tensorflow as tf
import numpy as np
from sklearn.metrics import roc_auc_score, average_precision_score, accuracy_score
#%% Define PVS class
class PVS():
# 1. Initialization
'''
x_train: training samples
data_type: Syn1 to Syn 6
'''
def __init__(self, x_train, data_type):
self.latent_dim1 = 100 # Dimension of actor (generator) network
self.latent_dim2 = 200 # Dimension of critic (discriminator) network
self.batch_size = 1000 # Batch size
self.epochs = 10000 # Epoch size (large epoch is needed due to the policy gradient framework)
self.lamda = 0.1 # Hyper-parameter for the number of selected features
self.input_shape = x_train.shape[1] # Input dimension
# Actionvation. (For Syn1 and 2, relu, others, selu)
self.activation = 'relu' if data_type in ['Syn1','Syn2'] else 'selu'
# Use Adam optimizer with learning rate = 0.0001
optimizer = Adam(0.0001)
# Build and compile the discriminator (critic)
self.discriminator = self.build_discriminator()
# Use categorical cross entropy as the loss
self.discriminator.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
# Build the generator (actor)
self.generator = self.build_generator()
# Use custom loss (my loss)
self.generator.compile(loss=self.my_loss, optimizer=optimizer)
# Build and compile the value function
self.valfunction = self.build_valfunction()
# Use categorical cross entropy as the loss
self.valfunction.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
#%% Custom loss definition
def my_loss(self, y_true, y_pred):
# dimension of the features
d = y_pred.shape[1]
# Put all three in y_true
# 1. selected probability
sel_prob = y_true[:,:d]
# 2. discriminator output
dis_prob = y_true[:,d:(d+2)]
# 3. valfunction output
val_prob = y_true[:,(d+2):(d+4)]
# 4. ground truth
y_final = y_true[:,(d+4):]
# A1. Compute the rewards of the actor network
Reward1 = tf.reduce_sum(y_final * tf.log(dis_prob + 1e-8), axis = 1)
# A2. Compute the rewards of the actor network
Reward2 = tf.reduce_sum(y_final * tf.log(val_prob + 1e-8), axis = 1)
# Difference is the rewards
Reward = Reward1 - Reward2
# B. Policy gradient loss computation.
loss1 = Reward * tf.reduce_sum( sel_prob * K.log(y_pred + 1e-8) + (1-sel_prob) * K.log(1-y_pred + 1e-8), axis = 1) - self.lamda * tf.reduce_mean(y_pred, axis = 1)
# C. Maximize the loss1
loss = tf.reduce_mean(-loss1)
return loss
#%% Generator (Actor)
def build_generator(self):
model = Sequential()
model.add(Dense(100, activation=self.activation, name = 's/dense1', kernel_regularizer=regularizers.l2(1e-3), input_dim = self.input_shape))
model.add(Dense(100, activation=self.activation, name = 's/dense2', kernel_regularizer=regularizers.l2(1e-3)))
model.add(Dense(self.input_shape, activation = 'sigmoid', name = 's/dense3', kernel_regularizer=regularizers.l2(1e-3)))
model.summary()
feature = Input(shape=(self.input_shape,), dtype='float32')
select_prob = model(feature)
return Model(feature, select_prob)
#%% Discriminator (Critic)
def build_discriminator(self):
model = Sequential()
model.add(Dense(200, activation=self.activation, name = 'dense1', kernel_regularizer=regularizers.l2(1e-3), input_dim = self.input_shape))
model.add(BatchNormalization()) # Use Batch norm for preventing overfitting
model.add(Dense(200, activation=self.activation, name = 'dense2', kernel_regularizer=regularizers.l2(1e-3)))
model.add(BatchNormalization())
model.add(Dense(2, activation ='softmax', name = 'dense3', kernel_regularizer=regularizers.l2(1e-3)))
model.summary()
# There are two inputs to be used in the discriminator
# 1. Features
feature = Input(shape=(self.input_shape,), dtype='float32')
# 2. Selected Features
select = Input(shape=(self.input_shape,), dtype='float32')
# Element-wise multiplication
model_input = Multiply()([feature, select])
prob = model(model_input)
return Model([feature, select], prob)
#%% Value Function
def build_valfunction(self):
model = Sequential()
model.add(Dense(200, activation=self.activation, name = 'v/dense1', kernel_regularizer=regularizers.l2(1e-3), input_dim = self.input_shape))
model.add(BatchNormalization()) # Use Batch norm for preventing overfitting
model.add(Dense(200, activation=self.activation, name = 'v/dense2', kernel_regularizer=regularizers.l2(1e-3)))
model.add(BatchNormalization())
model.add(Dense(2, activation ='softmax', name = 'v/dense3', kernel_regularizer=regularizers.l2(1e-3)))
model.summary()
# There are one inputs to be used in the value function
# 1. Features
feature = Input(shape=(self.input_shape,), dtype='float32')
# Element-wise multiplication
prob = model(feature)
return Model(feature, prob)
#%% Sampling the features based on the output of the generator
def Sample_M(self, gen_prob):
# Shape of the selection probability
n = gen_prob.shape[0]
d = gen_prob.shape[1]
# Sampling
samples = np.random.binomial(1, gen_prob, (n,d))
return samples
#%% Training procedure
def train(self, x_train, y_train):
# For each epoch (actually iterations)
for epoch in range(self.epochs):
#%% Train Discriminator
# Select a random batch of samples
idx = np.random.randint(0, x_train.shape[0], self.batch_size)
x_batch = x_train[idx,:]
y_batch = y_train[idx,:]
# Generate a batch of probabilities of feature selection
gen_prob = self.generator.predict(x_batch)
# Sampling the features based on the generated probability
sel_prob = self.Sample_M(gen_prob)
# Compute the prediction of the critic based on the sampled features (used for generator training)
dis_prob = self.discriminator.predict([x_batch, sel_prob])
# Train the discriminator
d_loss = self.discriminator.train_on_batch([x_batch, sel_prob], y_batch)
#%% Train Valud function
# Compute the prediction of the critic based on the sampled features (used for generator training)
val_prob = self.valfunction.predict(x_batch)
# Train the discriminator
v_loss = self.valfunction.train_on_batch(x_batch, y_batch)
#%% Train Generator
# Use three things as the y_true: sel_prob, dis_prob, and ground truth (y_batch)
y_batch_final = np.concatenate( (sel_prob, np.asarray(dis_prob), np.asarray(val_prob), y_batch), axis = 1 )
# Train the generator
g_loss = self.generator.train_on_batch(x_batch, y_batch_final)
#%% Plot the progress
dialog = 'Epoch: ' + str(epoch) + ', d_loss (Acc)): ' + str(d_loss[1]) + ', v_loss (Acc): ' + str(v_loss[1]) + ', g_loss: ' + str(np.round(g_loss,4))
if epoch % 100 == 0:
print(dialog)
#%% Selected Features
def output(self, x_train):
gen_prob = self.generator.predict(x_train)
return np.asarray(gen_prob)
#%% Prediction Results
def get_prediction(self, x_train, m_train):
val_prediction = self.valfunction.predict(x_train)
dis_prediction = self.discriminator.predict([x_train, m_train])
return np.asarray(val_prediction), np.asarray(dis_prediction)
#%% Main Function
if __name__ == '__main__':
# Data generation function import
from Data_Generation import generate_data
#%% Parameters
# Synthetic data type
idx = 5
data_sets = ['Syn1','Syn2','Syn3','Syn4','Syn5','Syn6']
data_type = data_sets[idx]
# Data output can be either binary (Y) or Probability (Prob)
data_out_sets = ['Y','Prob']
data_out = data_out_sets[0]
# Number of Training and Testing samples
train_N = 10000
test_N = 10000
# Seeds (different seeds for training and testing)
train_seed = 0
test_seed = 1
#%% Data Generation (Train/Test)
def create_data(data_type, data_out):
x_train, y_train, g_train = generate_data(n = train_N, data_type = data_type, seed = train_seed, out = data_out)
x_test, y_test, g_test = generate_data(n = test_N, data_type = data_type, seed = test_seed, out = data_out)
return x_train, y_train, g_train, x_test, y_test, g_test
x_train, y_train, g_train, x_test, y_test, g_test = create_data(data_type, data_out)
#%%
# 1. PVS Class call
PVS_Alg = PVS(x_train, data_type)
# 2. Algorithm training
PVS_Alg.train(x_train, y_train)
# 3. Get the selection probability on the testing set
Sel_Prob_Test = PVS_Alg.output(x_test)
# 4. Selected features
score = 1.*(Sel_Prob_Test > 0.5)
# 5. Prediction
val_predict, dis_predict = PVS_Alg.get_prediction(x_test, score)
#%% Performance Metrics
def performance_metric(score, g_truth):
n = len(score)
Temp_TPR = np.zeros([n,])
Temp_FDR = np.zeros([n,])
for i in range(n):
# TPR
TPR_Nom = np.sum(score[i,:] * g_truth[i,:])
TPR_Den = np.sum(g_truth[i,:])
Temp_TPR[i] = 100 * float(TPR_Nom)/float(TPR_Den+1e-8)
# FDR
FDR_Nom = np.sum(score[i,:] * (1-g_truth[i,:]))
FDR_Den = np.sum(score[i,:])
Temp_FDR[i] = 100 * float(FDR_Nom)/float(FDR_Den+1e-8)
return np.mean(Temp_TPR), np.mean(Temp_FDR), np.std(Temp_TPR), np.std(Temp_FDR)
#%% Output
TPR_mean, FDR_mean, TPR_std, FDR_std = performance_metric(score, g_test)
print('TPR mean: ' + str(np.round(TPR_mean,1)) + '\%, ' + 'TPR std: ' + str(np.round(TPR_std,1)) + '\%, ' )
print('FDR mean: ' + str(np.round(FDR_mean,1)) + '\%, ' + 'FDR std: ' + str(np.round(FDR_std,1)) + '\%, ' )
#%% Prediction Results
Predict_Out = np.zeros([20,3,2])
for i in range(20):
# different teat seed
test_seed = i+2
_, _, _, x_test, y_test, _ = create_data(data_type, data_out)
# 1. Get the selection probability on the testing set
Sel_Prob_Test = PVS_Alg.output(x_test)
# 2. Selected features
score = 1.*(Sel_Prob_Test > 0.5)
# 3. Prediction
val_predict, dis_predict = PVS_Alg.get_prediction(x_test, score)
# 4. Prediction Results
Predict_Out[i,0,0] = roc_auc_score(y_test[:,1], val_predict[:,1])
Predict_Out[i,1,0] = average_precision_score(y_test[:,1], val_predict[:,1])
Predict_Out[i,2,0] = accuracy_score(y_test[:,1], 1. * (val_predict[:,1]>0.5) )
Predict_Out[i,0,1] = roc_auc_score(y_test[:,1], dis_predict[:,1])
Predict_Out[i,1,1] = average_precision_score(y_test[:,1], dis_predict[:,1])
Predict_Out[i,2,1] = accuracy_score(y_test[:,1], 1. * (dis_predict[:,1]>0.5) )
# Mean / Var of 20 different testing sets
Output = np.round(np.concatenate((np.mean(Predict_Out,0),np.std(Predict_Out,0)),axis = 1),4)
print(Output)