-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08TripleSNN_semi_supervised_averaging.py
360 lines (271 loc) · 15.4 KB
/
08TripleSNN_semi_supervised_averaging.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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 13 09:12:43 2019
@author: anonymous
"""
#%tensorflow_version 1.2
from __future__ import print_function
from tensorflow.keras import models
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from tensorflow.keras import regularizers
import tensorflow.keras
import itertools
from data.data import *
import sys
from progressbar import ProgressBar as PB
from SNN_helper import *
from tensorflow.keras.callbacks import ModelCheckpoint,CSVLogger,ReduceLROnPlateau,EarlyStopping
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Subtract,Input,Lambda
from tensorflow.keras.layers import Conv2D, MaxPooling2D,Conv1D, MaxPooling1D
from tensorflow.keras.callbacks import ModelCheckpoint,CSVLogger,ReduceLROnPlateau,EarlyStopping
from sklearn.linear_model import LinearRegression
from tensorflow.keras import backend as K
import argparse
parser = argparse.ArgumentParser(description='TNN runner')
parser.add_argument('--dataset', help='Options are: bostonHousing, concreteData, energyEfficiency, proteinStructure, randomFunction', dest='dataset', default='bostonHousing')
parser.add_argument('--dataset_path', help='Relative path to datasets', dest='dataset_path', default='./data/')
parser.add_argument('--val_pct', help='Percentage of validation split.', dest='val_pct', default=0.05, type=float)
parser.add_argument('--test_pct', help='Percentage of test split.', dest='test_pct', default=0.05, type=float)
parser.add_argument('--l2', help='L2 Regularization weighting.', dest='l2', default=0.0, type=float)
parser.add_argument('--seed', help='Random seed', dest='seed', default=13, type=int)
parser.add_argument('--num', help='Number of datapoints for random function', default=1000, type=int)
parser = parser.parse_args()
Loop_weight=0.0
print(Loop_weight)
import time
print(parser.dataset)
start = time.time()
rmse_train_list=[]
rmse_val_list=[]
rmse_test_list=[]
rmse_test2_list=[]
### scaling factor 100,300,1000,3000,10000,30000,100000
n=1000
#(x_full, y_full) = getData(parser.dataset, parser.dataset_path)
#(x_full, y_full) = getData('randomFunction', './data/',n)
(x_full, y_full) = getData(parser.dataset, './data/',n)
#(x_full, y_full) = getData('testFunction', './data/',n)
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
for iteration in range(25):
parser.seed=iteration
# set the seed
print('Random seed:', parser.seed)
# 2. Set the `python` built-in pseudo-random generator at a fixed value
import random
random.seed(parser.seed)
# 3. Set the `numpy` pseudo-random generator at a fixed value
import numpy as np
np.random.seed(parser.seed)
# 4. Set the `tensorflow` pseudo-random generator at a fixed value
#import tensorflow as tf
tf.random.set_random_seed(parser.seed)
#(x_train_single, y_train_single), (x_val_single, y_val_single), (x_test_single, y_test_single) = splitData(x_full, y_full, val_pct=parser.val_pct, test_pct=parser.test_pct)
(x_train_single, y_train_single), (x_val_single, y_val_single), (x_test_single, y_test_single) = splitData(x_full, y_full, val_pct=0.1, test_pct=0.6,rand=True)
print(x_train_single[0])
##### divide test set
n_test_split=int(len(x_test_single)*5/6)
x_test2_single=x_test_single[n_test_split:]
y_test2_single=y_test_single[n_test_split:]
x_test_single=x_test_single[:n_test_split]
y_test_single=y_test_single[:n_test_split]
##### center and normalize
cn_transformer=CenterAndNorm()
x_train_single,y_train_single=cn_transformer.fittransform(x_train_single,y_train_single)
x_val_single,y_val_single=cn_transformer.transform(x_val_single,y_val_single)
x_test_single,y_test_single=cn_transformer.transform(x_test_single,y_test_single)
x_test2_single,y_test2_single=cn_transformer.transform(x_test2_single,y_test2_single)
##########################################
############ NN parameters
batch_size = 16
epochs = 2000
############### create NN
observer_a=Input(shape=(x_train_single.shape[-1],),name='observer_a')
observer_b=Input(shape=(x_train_single.shape[-1],),name='observer_b')
l2=parser.l2
merged_layer = tensorflow.keras.layers.Concatenate()([observer_a, observer_b])
merged_layer=Dense(128,activation='relu',kernel_regularizer=regularizers.l2(l2),activity_regularizer=regularizers.l2(l2))(merged_layer)
merged_layer=Dense(128,activation='relu',name='iout',kernel_regularizer=regularizers.l2(l2),activity_regularizer=regularizers.l2(l2))(merged_layer)
output=Dense(1,kernel_regularizer=regularizers.l2(l2),activity_regularizer=regularizers.l2(l2))(merged_layer)
model = Model(inputs=[observer_a, observer_b], outputs=output)
#model.summary()
observer_c=Input(shape=(x_train_single.shape[-1],),name='observer_c')
observer_d=Input(shape=(x_train_single.shape[-1],),name='observer_d')
observer_e=Input(shape=(x_train_single.shape[-1],),name='observer_e')
SNN1=model([observer_c,observer_d])
SNN2=model([observer_d,observer_e])
SNN3=model([observer_e,observer_c])
tri_layer = tensorflow.keras.layers.Concatenate()([SNN1,SNN2,SNN3])
TRImodel= Model(inputs=[observer_c, observer_d, observer_e], outputs=tri_layer)
#TRImodel.summary()
#### custom loss
def custom_loss(y_true,y_pred):
#eps=1.0e-6
#y_pred=K.clip(y_pred,eps,1.0-eps)
half=batch_size//2
#y_true.set_shape([16,3])
#y_pred.set_shape([16,3])
y_case=tf.constant([1.,1.,1.,1.,1.,1.,1.,1.,0.,0.,0.,0.,0.,0.,0.,0.])
y_case_odd=tf.constant([0.,0.,0.,0.,0.,0.,0.,0.,1.,1.,1.,1.,1.,1.,1.,1.])
#print(y_true.shape)
#print((y_case*K.mean(y_pred,axis=-1)).shape)
#return K.mean(K.square(tf.slice(y_pred-y_true,[0,0],[half,3]) ))+K.mean(K.square(K.mean(y_pred,axis=-1)))
#return K.sqrt(K.mean(K.square(tf.slice(y_pred-y_true,[0,0],[half,3]) )))+100*K.abs(K.square(K.mean(y_pred,axis=-1)))
#loss_A=K.mean(K.square(tf.slice(y_pred-y_true,[0,0],[half,3]) ),axis=-1)
#loss_B=K.square(K.mean(tf.slice(y_pred,[half,0],[half,3]),axis=-1))
loss_A=y_case*K.mean(K.square(y_pred - y_true), axis=-1)
loss_B=Loop_weight*y_case_odd*K.square(K.mean(y_pred,axis=-1))
#return loss_A+loss_B
#return y_case*K.mean(K.square(y_pred - y_true), axis=-1)+Loop_weight*y_case_odd*K.square(K.mean(y_pred,axis=-1))
#return loss_A+Loop_weight*loss_B
#return 0.1*K.sum( (K.sum(y_pred,axis=0)-batch_size/2)**2 ) - K.sum(y_pred*K.log(y_pred))
return K.in_train_phase(loss_A+loss_B, loss_A)
# Let's train the model
TRImodel.compile(loss=custom_loss
,optimizer=tensorflow.keras.optimizers.Adadelta(lr=1)
#,optimizer='rmsprop'
,metrics=['mse']
)
reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.5,
patience=int(1000/np.sqrt(n)+1), verbose=1,min_lr=0)
early_stop= EarlyStopping(monitor='val_loss', patience=2*int(1000/np.sqrt(n)+1), verbose=0)
mcp_save = ModelCheckpoint('mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='min', verbose=0)
history = TRImodel.fit_generator(triple_semi_supervised_generator_all_excluding_labelled_loops(x_train_single, y_train_single, np.concatenate((x_test_single,x_val_single),axis=0), batch_size),
#triple_generator(x_train_single, y_train_single,batch_size),
steps_per_epoch=len(x_train_single)*10/batch_size,
epochs=epochs,
validation_data=triple_semi_supervised_generator3(x_val_single, y_val_single, x_train_single, batch_size),
#validation_data=triple_generator(x_val_single, y_val_single, batch_size),
validation_steps=len(x_val_single)*100/batch_size,
callbacks=[reduce_lr,early_stop, mcp_save],verbose=0)
#TRImodel.load_weights('mdl_wts.hdf5')
# Plot training & test loss values
# plt.plot(history.history['loss'])
# plt.plot(history.history['val_loss'])
# plt.title('Model loss')
# plt.ylabel('Loss')
# plt.xlabel('Epoch')
# plt.legend(['Train', 'Val'], loc='upper left')
# # plt.show()
# plt.savefig('loss.pdf')
#####################
Y_pred_train=[]
Y_pred_r_train=[]
Y_pred_check_train=[]
Y_median_train=[]
Y_var_train=[]
Y_mse_train=[]
for i in range(len(x_train_single)):
pair_B=np.array([x_train_single[i]]*len(x_train_single))
diffA=model.predict([pair_B,x_train_single]).flatten()
diffB=model.predict([x_train_single,pair_B]).flatten()
Y_pred_train.append(np.average(0.5*diffA-0.5*diffB+y_train_single, weights=None))
Y_pred_r_train.append(np.average(-diffB+y_train_single))
Y_pred_check_train.append(np.var(0.5*diffA+0.5*diffB))
#Y_pred_check_train.append(np.average(np.abs(model.predict([pair_B,x_train_single])+model.predict([x_train_single,pair_B]))))
Y_median_train.append(np.median(diffA+y_train_single))
Y_var_train.append(np.var(0.5*diffA-0.5*diffB+y_train_single))
Y_mse_train.append((Y_pred_train[i]-y_train_single[i])**2)
Y_pred_train=cn_transformer.inversetransformY(np.array(Y_pred_train))
Y_pred_r_train=cn_transformer.inversetransformY(np.array(Y_pred_r_train))
Y_pred_check_train=np.array(Y_pred_check_train)*(cn_transformer.Ymax)
Y_median_train=cn_transformer.inversetransformY(np.array(Y_median_train))
Y_var_train=np.array(Y_var_train)*(cn_transformer.Ymax)**2
Y_mse_train=np.array(Y_mse_train)*(cn_transformer.Ymax)**2
Y_self_check_train=np.abs(np.array(model.predict([x_train_single,x_train_single]))).flatten()*(cn_transformer.Ymax)
#####################
Y_pred_val=[]
Y_pred_r_val=[]
Y_pred_check_val=[]
Y_median_val=[]
Y_var_val=[]
Y_mse_val=[]
for i in range(len(x_val_single)):
pair_B=np.array([x_val_single[i]]*len(x_train_single))
diffA=model.predict([pair_B,x_train_single]).flatten()
diffB=model.predict([x_train_single,pair_B]).flatten()
Y_pred_val.append(np.average(0.5*diffA-0.5*diffB+y_train_single, weights=None))
Y_pred_r_val.append(np.average(-diffB+y_train_single))
Y_pred_check_val.append(np.var(0.5*diffA+0.5*diffB))
#Y_pred_check_val.append(np.average(np.abs(model.predict([pair_B,x_train_single])+model.predict([x_train_single,pair_B]))))
Y_median_val.append(np.median(diffA+y_train_single))
Y_var_val.append(np.var(0.5*diffA-0.5*diffB+y_train_single))
Y_mse_val.append((Y_pred_val[i]-y_val_single[i])**2)
Y_pred_val=cn_transformer.inversetransformY(np.array(Y_pred_val))
Y_pred_r_val=cn_transformer.inversetransformY(np.array(Y_pred_r_val))
Y_pred_check_val=np.array(Y_pred_check_val)*(cn_transformer.Ymax)
Y_median_val=cn_transformer.inversetransformY(np.array(Y_median_val))
Y_var_val=np.array(Y_var_val)*(cn_transformer.Ymax)**2
Y_mse_val=np.array(Y_mse_val)*(cn_transformer.Ymax)**2
Y_self_check_val=np.abs(np.array(model.predict([x_val_single,x_val_single]))).flatten()*(cn_transformer.Ymax)
#####################
Y_pred_test=[]
Y_pred_r_test=[]
Y_pred_check_test=[]
Y_median_test=[]
Y_var_test=[]
Y_mse_test=[]
for i in range(len(x_test_single)):
pair_B=np.array([x_test_single[i]]*len(x_train_single))
diffA=model.predict([pair_B,x_train_single]).flatten()
diffB=model.predict([x_train_single,pair_B]).flatten()
Y_pred_test.append(np.average(0.5*diffA-0.5*diffB+y_train_single, weights=None))
Y_pred_r_test.append(np.average(-diffB+y_train_single))
Y_pred_check_test.append(np.var(0.5*diffA+0.5*diffB))
#Y_pred_check_test.append(np.average(np.abs(model.predict([pair_B,x_train_single])+model.predict([x_train_single,pair_B]))))
Y_median_test.append(np.median(diffA+y_train_single))
Y_var_test.append(np.var(0.5*diffA-0.5*diffB+y_train_single))
Y_mse_test.append((Y_pred_test[i]-y_test_single[i])**2)
Y_pred_test=cn_transformer.inversetransformY(np.array(Y_pred_test))
Y_pred_r_test=cn_transformer.inversetransformY(np.array(Y_pred_r_test))
Y_pred_check_test=np.array(Y_pred_check_test)*(cn_transformer.Ymax)
Y_median_test=cn_transformer.inversetransformY(np.array(Y_median_test))
Y_var_test=np.array(Y_var_test)*(cn_transformer.Ymax)**2
Y_mse_test=np.array(Y_mse_test)*(cn_transformer.Ymax)**2
Y_self_check_test=np.abs(np.array(model.predict([x_test_single,x_test_single]))).flatten()*(cn_transformer.Ymax)
#####################
Y_pred_test2=[]
Y_pred_r_test2=[]
Y_pred_check_test2=[]
Y_median_test2=[]
Y_var_test2=[]
Y_mse_test2=[]
for i in range(len(x_test2_single)):
pair_B=np.array([x_test2_single[i]]*len(x_train_single))
diffA=model.predict([pair_B,x_train_single]).flatten()
diffB=model.predict([x_train_single,pair_B]).flatten()
Y_pred_test2.append(np.average(0.5*diffA-0.5*diffB+y_train_single, weights=None))
Y_pred_r_test2.append(np.average(-diffB+y_train_single))
Y_pred_check_test2.append(np.var(0.5*diffA+0.5*diffB))
#Y_pred_check_test2.append(np.average(np.abs(model.predict([pair_B,x_train_single])+model.predict([x_train_single,pair_B]))))
Y_median_test2.append(np.median(diffA+y_train_single))
Y_var_test2.append(np.var(0.5*diffA-0.5*diffB+y_train_single))
Y_mse_test2.append((Y_pred_test2[i]-y_test2_single[i])**2)
Y_pred_test2=cn_transformer.inversetransformY(np.array(Y_pred_test2))
Y_pred_r_test2=cn_transformer.inversetransformY(np.array(Y_pred_r_test2))
Y_pred_check_test2=np.array(Y_pred_check_test2)*(cn_transformer.Ymax)
Y_median_test2=cn_transformer.inversetransformY(np.array(Y_median_test2))
Y_var_test2=np.array(Y_var_test2)*(cn_transformer.Ymax)**2
Y_mse_test2=np.array(Y_mse_test2)*(cn_transformer.Ymax)**2
Y_self_check_test2=np.abs(np.array(model.predict([x_test2_single,x_test2_single]))).flatten()*(cn_transformer.Ymax)
print('Train RMSE:', np.average(Y_mse_train)**0.5)
print('Val RMSE:',np.average(Y_mse_val)**0.5)
print('Test RMSE:',np.average(Y_mse_test)**0.5)
print('Test2 RMSE:',np.average(Y_mse_test2)**0.5)
trainrmse=np.average(Y_mse_train)**0.5
valrmse=np.average(Y_mse_val)**0.5
testrmse=np.average(Y_mse_test)**0.5
test2rmse=np.average(Y_mse_test2)**0.5
rmse_train_list.append(trainrmse)
rmse_val_list.append(valrmse)
rmse_test_list.append(testrmse)
rmse_test2_list.append(test2rmse)
print("prelim mean test rmse",np.mean(rmse_test_list),"+-",np.std(rmse_test_list))
print("FINAL tnn mean train rmse",np.mean(rmse_train_list),"+-",np.std(rmse_train_list))
print("FINAL tnn mean val rmse",np.mean(rmse_val_list),"+-",np.std(rmse_val_list))
print("FINAL tnn mean test rmse",np.mean(rmse_test_list),"+-",np.std(rmse_test_list))
print("FINAL tnn mean test2 rmse",np.mean(rmse_test2_list),"+-",np.std(rmse_test2_list))
end = time.time()
print("time in seconds",end - start)