|
a |
|
b/GP/run_cnn.py |
|
|
1 |
from CNN.cnn import acc_pearson_r as acc_pearson_r |
|
|
2 |
from CNN.cnn import cnn_main as cnn_main |
|
|
3 |
from CNN.cnn import cnn_main_cat as cnn_main_cat |
|
|
4 |
import os |
|
|
5 |
import pandas as pd |
|
|
6 |
import numpy as np |
|
|
7 |
import talos as ta |
|
|
8 |
|
|
|
9 |
from matplotlib import pyplot as plt |
|
|
10 |
from keras import backend as K |
|
|
11 |
from keras.models import Sequential |
|
|
12 |
from keras.layers import Dense, Activation |
|
|
13 |
from keras.layers import Dropout |
|
|
14 |
from keras import regularizers |
|
|
15 |
from keras.activations import relu, elu, linear, softmax, tanh, softplus |
|
|
16 |
from keras.callbacks import EarlyStopping, Callback |
|
|
17 |
from keras.wrappers.scikit_learn import KerasRegressor |
|
|
18 |
from keras.optimizers import Adam, Nadam, sgd,Adadelta, RMSprop |
|
|
19 |
from keras.losses import mean_squared_error, categorical_crossentropy, logcosh |
|
|
20 |
from keras.utils.np_utils import to_categorical |
|
|
21 |
from keras import metrics |
|
|
22 |
|
|
|
23 |
#keras to CNN |
|
|
24 |
from keras.layers import Flatten, Conv1D, MaxPooling1D |
|
|
25 |
# defining network |
|
|
26 |
from keras.layers import Flatten, Conv1D, MaxPooling1D |
|
|
27 |
from keras import regularizers |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
import wrangle as wr |
|
|
31 |
from talos.metrics.keras_metrics import fmeasure_acc |
|
|
32 |
from talos.model.layers import hidden_layers |
|
|
33 |
from talos import live |
|
|
34 |
from talos.model import lr_normalizer, early_stopper, hidden_layers |
|
|
35 |
import os |
|
|
36 |
from talos import Deploy |
|
|
37 |
|
|
|
38 |
def run_cnn_main(X_tr,y_tr,X_vl,y_val,output,main,prop,trait,cat, |
|
|
39 |
lr,dr_1,dr_2,reg_1,reg_2,reg_3,nconv,act_1,act_2,hn,ps,hl,ks, |
|
|
40 |
ns,epochs,nf,op,bs): |
|
|
41 |
"""Evolve a genome.""" |
|
|
42 |
# population: Number of networks/genomes in each generation. |
|
|
43 |
# we only need to train the new ones.... |
|
|
44 |
# generations: Number of times to evolve the population. |
|
|
45 |
# hyperparameters |
|
|
46 |
p = {'hidden_neurons':hn, |
|
|
47 |
'reg1': reg_1, |
|
|
48 |
'reg2': reg_2, |
|
|
49 |
'reg3': reg_3, |
|
|
50 |
'pool':ps, |
|
|
51 |
'lr': lr, |
|
|
52 |
'nconv':nconv, |
|
|
53 |
'hidden_layers': hl, |
|
|
54 |
'kernel_size': ks, |
|
|
55 |
'nStride': ns, # stride between convolutions |
|
|
56 |
'nFilter': nf, # no. of convolutions' |
|
|
57 |
'batch_size': bs, |
|
|
58 |
'epochs': epochs, |
|
|
59 |
'dropout_1':dr_1, |
|
|
60 |
'dropout_2': dr_2, |
|
|
61 |
'optimizer': op, #[Adam,Nadam],# |
|
|
62 |
'activation_1':act_1, |
|
|
63 |
'activation_2': act_2, |
|
|
64 |
'last_activation': [linear]} |
|
|
65 |
|
|
|
66 |
print(p) |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
if cat is False: |
|
|
70 |
talos_cnn = ta.Scan(x=X_tr, |
|
|
71 |
y=y_tr, |
|
|
72 |
model=cnn_main, params=p, |
|
|
73 |
grid_downsample=prop, |
|
|
74 |
print_params=True, |
|
|
75 |
dataset_name='cnn_model') |
|
|
76 |
|
|
|
77 |
#best_model = talos_cnn.best_model(metric='val_acc_pearson_r', asc=False) |
|
|
78 |
best_model = talos_cnn.best_model(metric='val_loss', asc=True) |
|
|
79 |
else: |
|
|
80 |
talos_cnn = ta.Scan(x=X_tr, |
|
|
81 |
y=y_tr, |
|
|
82 |
model=cnn_main_cat, params=p, |
|
|
83 |
grid_downsample=prop, |
|
|
84 |
print_params=True, |
|
|
85 |
dataset_name='cnn_model') |
|
|
86 |
best_model = talos_cnn.best_model(metric='val_acc') |
|
|
87 |
|
|
|
88 |
|
|
|
89 |
x_val = np.expand_dims(np.asarray(X_vl), axis=2) |
|
|
90 |
yy_hat = best_model.predict(x_val, batch_size=32) |
|
|
91 |
|
|
|
92 |
# save model |
|
|
93 |
|
|
|
94 |
talos_Data = pd.DataFrame(talos_cnn.data) |
|
|
95 |
talos_Data["val_loss"] = pd.to_numeric(talos_Data["val_loss"], errors="coerce") |
|
|
96 |
if cat is False: |
|
|
97 |
talos_Data["acc_pearson_r"] = pd.to_numeric(talos_Data["acc_pearson_r"], errors="coerce") |
|
|
98 |
talos_Data["val_acc_pearson_r"] = pd.to_numeric(talos_Data["val_acc_pearson_r"], errors="coerce") |
|
|
99 |
|
|
|
100 |
os.chdir(output) |
|
|
101 |
|
|
|
102 |
if not os.path.exists("cnn"): |
|
|
103 |
os.makedirs("cnn") |
|
|
104 |
dir = output + "cnn/" |
|
|
105 |
|
|
|
106 |
os.chdir(os.path.join(output, 'cnn/')) |
|
|
107 |
|
|
|
108 |
# write output |
|
|
109 |
talos_Data.to_csv("cnn_prediction_talos.csv", index=False) |
|
|
110 |
|
|
|
111 |
r = ta.Reporting("cnn_prediction_talos.csv") |
|
|
112 |
if cat is False: |
|
|
113 |
|
|
|
114 |
if not os.path.exists("figures"): |
|
|
115 |
os.makedirs("figures") |
|
|
116 |
dir = output + "cnn/figures/" |
|
|
117 |
|
|
|
118 |
os.chdir(os.path.join(output, 'cnn/figures/')) |
|
|
119 |
try: |
|
|
120 |
number= y_tr.shape[1] |
|
|
121 |
for i in range(0, number): |
|
|
122 |
corr = np.corrcoef(y_val[:, i], yy_hat[:, i])[0, 1] |
|
|
123 |
# correlation btw predicted and observed |
|
|
124 |
fig = plt.figure() |
|
|
125 |
# plot observed vs. predicted targets |
|
|
126 |
plt.title('CNN: Observed vs Predicted Y trait_' + str(i) + 'cor:' + str(corr)) |
|
|
127 |
plt.ylabel('Predicted') |
|
|
128 |
plt.xlabel('Observed') |
|
|
129 |
plt.scatter(y_val[:, i], yy_hat[:, i], marker='o') |
|
|
130 |
fig.savefig("CNN_Tunne_trait_" + str(i) + '.png', |
|
|
131 |
dpi=300) |
|
|
132 |
plt.close(fig) |
|
|
133 |
os.chdir(output) |
|
|
134 |
except IndexError: |
|
|
135 |
yy_hat = np.reshape(yy_hat, -1) |
|
|
136 |
corr = np.corrcoef(y_val, yy_hat)[0, 1] |
|
|
137 |
# correlation btw predicted and observed |
|
|
138 |
fig = plt.figure() |
|
|
139 |
# plot observed vs. predicted targets |
|
|
140 |
plt.title('cnn: Observed vs Predicted Y trait_' + str(trait) + 'cor:' + str(corr)) |
|
|
141 |
plt.ylabel('Predicted') |
|
|
142 |
plt.xlabel('Observed') |
|
|
143 |
plt.scatter(y_val, yy_hat, marker='o') |
|
|
144 |
fig.savefig("cnn_Tunne_trait_" + str(trait) + '.png', |
|
|
145 |
dpi=300) |
|
|
146 |
plt.close(fig) |
|
|
147 |
|
|
|
148 |
os.chdir(output) |
|
|
149 |
if os.path.exists("best_model"): |
|
|
150 |
os.rmdir("best_model") |
|
|
151 |
os.makedirs("best_model") |
|
|
152 |
if not os.path.exists("best_model"): |
|
|
153 |
os.makedirs("best_model") |
|
|
154 |
|
|
|
155 |
os.chdir(os.path.join(output, 'best_model/')) |
|
|
156 |
if cat is False: |
|
|
157 |
Deploy(talos_cnn, 'CNN_optimize', metric='val_acc_pearson_r', asc=False) |
|
|
158 |
else: |
|
|
159 |
Deploy(talos_cnn,'CNN_optimize') |