-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML_functions01.py
604 lines (348 loc) · 15.6 KB
/
ML_functions01.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
import numpy as np
import pandas as pd
from keras.models import Model
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling3D,Input, BatchNormalization
from keras.layers.pooling import MaxPool2D
from keras.layers.merge import concatenate
from keras import losses
from keras.models import load_model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import backend as K
#cross-validation liibrary
from sklearn.model_selection import KFold
import feature_extract_01
def CNN_model01(nz, nx, channels):
#shared CNN weights
conv_1 = Conv2D(4, kernel_size=1, activation='relu')
conv_2 = Conv2D(4, kernel_size=2, activation='relu')
#conv_1 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#conv_2 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#first input model
input_1 = Input(shape=(nz, nx, channels))
conv_11 = conv_1(input_1)
pool_11 = MaxPool2D(pool_size=(2, 2))(conv_11)
conv_12 = conv_2(pool_11)
#pool_12 = MaxPool2D(pool_size=(1, 1))(conv_12)
#f1 = Flatten()(pool_12)
f1 = Flatten()(conv_12)
#second input model
input_2 = Input(shape=(nz, nx, channels))
conv_21 = conv_1(input_2)
pool_21 = MaxPool2D(pool_size=(2,2))(conv_21)
conv_22 = conv_2(pool_21)
#pool_22 = MaxPool2D(pool_size=(1, 1))(conv_22)
#f2 = Flatten()(pool_22)
f2 = Flatten()(conv_22)
#merge two layers and add dense
merge_layer = concatenate([f1, f2])
dense_1 = Dense(40, activation='relu')(merge_layer)
out_1 = Dense(1, activation='linear')(dense_1)
model_cnn = Model(inputs=[input_1, input_2], outputs=out_1)
print( model_cnn.summary())
return model_cnn
def CNN_model02(nz, nx, channels):
#shared CNN weights
conv_1 = Conv2D(10, kernel_size=1, activation='relu')
conv_2 = Conv2D(4, kernel_size=2, activation='relu')
#conv_1 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#conv_2 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#first input model
input_1 = Input(shape=(nz, nx, channels))
conv_11 = conv_1(input_1)
pool_11 = MaxPool2D(pool_size=(2, 2))(conv_11)
conv_12 = conv_2(pool_11)
#pool_12 = MaxPool2D(pool_size=(1, 1))(conv_12)
#f1 = Flatten()(pool_12)
f1 = Flatten()(conv_12)
#second input model
input_2 = Input(shape=(nz, nx, channels))
conv_21 = conv_1(input_2)
pool_21 = MaxPool2D(pool_size=(2,2))(conv_21)
conv_22 = conv_2(pool_21)
#pool_22 = MaxPool2D(pool_size=(1, 1))(conv_22)
#f2 = Flatten()(pool_22)
f2 = Flatten()(conv_22)
#merge two layers and add dense
merge_layer = concatenate([f1, f2])
dense_1 = Dense(20, activation='relu')(merge_layer)
out_1 = Dense(1, activation='linear')(dense_1)
model_cnn = Model(inputs=[input_1, input_2], outputs=out_1)
print( model_cnn.summary())
return model_cnn
def CNN_model02B(nz, nx, channels, ng):
#CNN to concatenate global features
#ng is the number of global features
#shared CNN weights
conv_1 = Conv2D(10, kernel_size=1, activation='relu')
conv_2 = Conv2D(5, kernel_size=2, activation='relu')
batch_norm_layer = BatchNormalization()
#conv_1 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#conv_2 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#first input model
input_1 = Input(shape=(nz, nx, channels))
conv_11 = conv_1(input_1)
pool_11 = MaxPool2D(pool_size=(2, 2))(conv_11)
conv_12 = conv_2(pool_11)
#pool_12 = MaxPool2D(pool_size=(1, 1))(conv_12)
#f1 = Flatten()(pool_12)
f1 = Flatten()(conv_12)
#second input model
input_2 = Input(shape=(nz, nx, channels))
conv_21 = conv_1(input_2)
pool_21 = MaxPool2D(pool_size=(2,2))(conv_21)
conv_22 = conv_2(pool_21)
pool_22 = MaxPool2D(pool_size=(2, 1))(conv_22)
f2 = Flatten()(pool_22)
# f2 = Flatten()(conv_22)
#add global features
input_3 = Input(shape=(ng, ))
#merge two layers and add dense
merge_layer = concatenate([f1, f2, input_3])
#add batch norm here
#batch_norm = batch_norm_layer(merge_layer)
dense_1 = Dense(20, activation='relu')(merge_layer)
#dense_1 = Dense(20, activation='relu')(batch_norm)
out_1 = Dense(1, activation='linear')(dense_1)
model_cnn = Model(inputs=[input_1, input_2, input_3], outputs=out_1)
print( model_cnn.summary())
return model_cnn
def CNN_model02C(nz, nx, channels, ng):
#CNN to concatenate global features
#ng is the number of global features
#shared CNN weights
conv_1 = Conv2D(4, kernel_size=1, activation='relu', kernel_initializer='glorot_normal')
conv_2 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
drop_l = Dropout(rate=0.25)
#batch_norm_layer = BatchNormalization()
#conv_1 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#conv_2 = Conv2D(4, kernel_size=2, activation='relu', kernel_initializer='glorot_normal')
#first input model
input_1 = Input(shape=(nz, nx, channels))
drop_11 = drop_l(input_1)
conv_11 = conv_1(input_1)
#conv_11 = conv_1(drop_11)
pool_11 = MaxPool2D(pool_size=(2, 2))(conv_11)
conv_12 = conv_2(pool_11)
#pool_12 = MaxPool2D(pool_size=(2, 1))(conv_12)
f1 = Flatten()(conv_12)
#f1 = Flatten()(pool_12)
#second input model
input_2 = Input(shape=(nz, nx, channels))
drop_21 = drop_l(input_2)
conv_21 = conv_1(input_2)
#conv_21 = conv_1(drop_21)
pool_21 = MaxPool2D(pool_size=(2, 2))(conv_21)
conv_22 = conv_2(pool_21)
#pool_22 = MaxPool2D(pool_size=(2, 1))(conv_22)
#f2 = Flatten()(pool_22)
f2 = Flatten()(conv_22)
#add global features
input_3 = Input(shape=(ng, ))
#merge two layers and add dense
merge_layer = concatenate([f1, f2, input_3])
merge_layer = drop_l(merge_layer)
#add batch norm here
#batch_norm = batch_norm_layer(merge_layer)
dense_1 = Dense(16, activation='relu', kernel_initializer='glorot_normal')(merge_layer)
#dense_1 = Dense(20, activation='relu')(batch_norm)
out_1 = Dense(1, activation='linear')(dense_1)
model_cnn = Model(inputs=[input_1, input_2, input_3], outputs=out_1)
print( model_cnn.summary())
return model_cnn
def fit_model(model, X1, X2, Y, split_val=0.1):
#compile model with mse and adam
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit([X1, X2], [Y], epochs=10, batch_size=16, validation_split=0.1)
return model
def fit_model02(model, X1, X2, Y, split_val=0.1, attempt_max=3):
val_thres = 1.00
for i in range(attempt_max):
init_model = model
#compile model with mse and adam
model.compile(loss='mean_squared_error', optimizer='adam')
train_history = init_model.fit([X1, X2], [Y], epochs=50, batch_size=16, validation_split=0.1)
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
save_model = init_model
val_thres = val_loss
return save_model
def fit_model03(RBF, X1, X2, Y, iter_max=3):
val_thres = 1.00
for i in range(iter_max):
#model_cnn = CNN_model01(RBF.shape[1], RBF.shape[2], RBF.shape[3])
model_cnn = CNN_model02(RBF.shape[1], RBF.shape[2], RBF.shape[3])
model_cnn.compile(loss='mean_squared_error', optimizer='adam')
train_history = model_cnn.fit([X1, X2], [Y], epochs=40, batch_size=16, validation_split=0.1)
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
print( val_loss)
save_model = model_cnn
val_thres = val_loss
return save_model
###-----concatenated Model--------
def fit_model02B(RBF, X1, X2, Xg, Y, iter_max=3):
val_thres = 1.00
for i in range(iter_max):
#model_cnn = CNN_model01(RBF.shape[1], RBF.shape[2], RBF.shape[3])
#model_cnn = CNN_model02B(RBF.shape[1], RBF.shape[2], RBF.shape[3], Xg.shape[1])
model_cnn = CNN_model02C(RBF.shape[1], RBF.shape[2], RBF.shape[3], Xg.shape[1])
model_cnn.compile(loss='mean_squared_error', optimizer='adam')
callbacks = [EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)]
train_history = model_cnn.fit([X1, X2, Xg], [Y], epochs=5, batch_size=16, callbacks=callbacks, validation_split=0.2)
#train_history = model_cnn.fit([X1, X2, Xg], [Y], epochs=10, batch_size=16, validation_split=0.1)
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
print( val_loss)
save_model = model_cnn
val_thres = val_loss
return save_model
####---fit model with validation---------
def fit_model02C(RBF, X1, X2, Xg, Y, X1_v, X2_v, Xg_v, Y_v, iter_max=3):
val_thres = 1.00
for i in range(iter_max):
# model_cnn = CNN_model01(RBF.shape[1], RBF.shape[2], RBF.shape[3])
callbacks = [EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)]
#model_cnn = CNN_model02B(RBF.shape[1], RBF.shape[2], RBF.shape[3], Xg.shape[1])
model_cnn = CNN_model02C(RBF.shape[1], RBF.shape[2], RBF.shape[3], Xg.shape[1])
model_cnn.compile(loss='mean_squared_error', optimizer='adam')
train_history = model_cnn.fit([X1, X2, Xg], [Y], epochs=3, batch_size=16, callbacks=callbacks, validation_data=([X1_v, X2_v, Xg_v], Y_v))
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
print( val_loss)
save_model = model_cnn
val_thres = val_loss
return save_model
#####fit_model04 -> contains
def fit_model04(RBF, X1, X2, Y, X_v1, X_v2, Y_v, iter_max=5):
val_thres = 1.00
for i in range(iter_max):
#model_cnn = CNN_model01(RBF.shape[1], RBF.shape[2], RBF.shape[3])
model_cnn = CNN_model02(RBF.shape[1], RBF.shape[2], RBF.shape[3])
model_cnn.compile(loss='mean_squared_error', optimizer='adam')
train_history = model_cnn.fit([X1, X2], [Y], epochs=40, batch_size=16, validation_data=([X_v1, X_v2], [Y_v]))
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
print( val_loss)
save_model = model_cnn
val_thres = val_loss
return save_model
def predict_model(model, X):
Y_p = model.predict(X)
return Y_p
def get_model_weights(model):
W = model.get_weights()
return W
######cross-validation split
def kfold_index(X, Y):
kf = KFold(n_splits=10)
#train+
return None
####
####---------------------------------------------
##--Functions to define space action space
def global_NN01(S_l, A_l, S_g, A_g):
#S_g -> Global Encoding, into a flattened array
#S_l -> S is a flattened array containing the local information
#A_l -> Local action
#A_g -> global action
#global actions
input_Sg = Input(shape=(S_g.shape[0]))
input_Ag =Input(shape=(A_g.shape[0]))
merge_global = concatenate([input_Sg, input_Ag])
dense_global = Dense(40, activation='relu')(merge_global)
#lets start with the local actions
input_Sl = Input(shape=(S_l.shape[0]))
input_Al = Input(shape=(A_l.shape[0]))
merge_local = concatenate([input_Sl, input_Al, dense_global])
dense_local = Dense(40, activation='relu')(merge_local)
out_1 = Dense(S_l.shape[0], activation='linear')(dense_local)
model_out = Model(inputs=[input_Sg, input_Ag, input_Sl, input_Al], outputs=out_1)
return model_out
def local_NN01(S_l, A_l):
input_Sl = Input(shape=(S_l.shape[0]))
input_Al = Input(shape=(A_l.shape[0]))
merge_local = concatenate([input_Sl, input_Al])
dense_local = Dense(40, activation='relu')(merge_local)
out_1 = Dense(S_l.shape[0], activation='linear')(dense_local)
return None
def autoencoder_01(X_t, encoding_dim=20, dim_1=64):
input_x = Input(shape=(X_t.shape[1],))
# "encoded" is the encoded representation of the input
int_layer = Dense(dim_1, activation='relu')(input_x)
encoded = Dense(encoding_dim, activation='linear')(int_layer)
layer_2 = Dense(dim_1, activation='relu')(encoded)
# "decoded" is the lossy reconstruction of the input
#decoded = Dense(X_t.shape[1], activation='linear')(encoded)
decoded = Dense(X_t.shape[1], activation='linear')(layer_2)
# this model maps an input to its reconstruction
autoencoder = Model(input_x, decoded)
print( autoencoder.summary())
return autoencoder
def fit_AE01(X_t, iter_max=1):
val_thres = 1.00
for i in range(iter_max):
#model_cnn = CNN_model01(RBF.shape[1], RBF.shape[2], RBF.shape[3])
model_AE = autoencoder_01(X_t=X_t)
model_AE.compile(loss='mean_squared_error', optimizer='adam')
train_history = model_AE.fit(X_t, X_t, epochs=5, batch_size=16, validation_split=0.2)
val_loss = train_history.history['val_loss'][-1]
if val_loss < val_thres:
print( val_loss)
save_model = model_AE
val_thres = val_loss
return save_model
#####make functions to extract the intermediate features
def extract_AE_features(model, RBF_1, RBF_2, encoded_dim=20):
print( "RBF shape")
print( RBF_1.shape)
X1 = np.zeros((RBF_1.shape[0], RBF_1.shape[1], RBF_1.shape[2], encoded_dim))
X2 = np.zeros_like(X1)
encoder_layer = K.function([model.layers[0].input], [model.layers[2].output])
for m in range(0, RBF_1.shape[0]): #sample size
for i in range(0, RBF_1.shape[1]): #z-dir
for j in range(RBF_1.shape[2]):
z_1 = RBF_1[m, i, j, :]
z_1 = z_1[None, :]
z_2 = RBF_2[m, i, j, :]
z_2 = z_2[None, :]
#print "encoded layer: "
#print encoder_layer([z_1])[0]
X1[m, i, j, :] = encoder_layer([z_1])[0]
X2[m, i, j, :] = encoder_layer([z_2])[0]
return X1, X2
def load_NN(filepath):
model = load_model(filepath=filepath)
return model
def AE_features02(model, RBF_1, RBF_2, encoded_dim=20):
encoder_layer = K.function([model.layers[0].input], [model.layers[2].output])
z1 = RBF_1.copy()
z2 = RBF_2.copy()
#reshape input arrays
Z1 = z1.reshape(-1, RBF_1.shape[3])
Z2 = z2.reshape(-1, RBF_2.shape[3])
x1 = (encoder_layer([Z1])[0]).reshape(RBF_1.shape[0], RBF_1.shape[1], RBF_1.shape[2], encoded_dim)
x2 = (encoder_layer([Z2])[0]).reshape(RBF_2.shape[0], RBF_2.shape[1], RBF_2.shape[2], encoded_dim)
return x1, x2
####------------
###cross-validation to make sure that
def val_check01(x1, x2, crosslink_mat, y_out, trial_max=5):
val_thres = 1.00
for trial_num in range(trial_max):
X_t1, X_t2, Xg_t, Y_t, X_v1, X_v2, Xg_v, Y_v, X_e1, X_e2, Xg_e, Y_e = feature_extract_01.partition_data03(
X=x1, X2=x2, Xg=crosslink_mat, Y=y_out, n=77, n2=87)
X_t1, X_t2, Xg_t, Y_t = feature_extract_01.shuffle_data02B(X1=X_t1, X2=X_t2, Xg=Xg_t, Y=Y_t)
model_2, val_loss = fit_model02C(RBF=x1, X1=X_t1, X2=X_t2, Xg=Xg_t, Y=Y_t, X1_v=X_v1, X2_v=X_v2, Xg_v=Xg_v, Y_v=Y_v)
if val_loss < val_thres:
save_model = model_2
val_thres = val_loss
return save_model
def predict_with_uncertainty(f, X, num_out=1, n_iter=500):
result = np.zeros((n_iter, num_out))
x1 = X[0]
x2 = X[1]
x3 = X[2]
for i in range(n_iter):
result[i, :] = f([x1, x2, x3, 1])[0]
prediction = result.mean(axis=0)
uncertainty = result.std(axis=0)
return prediction, uncertainty