-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_cancer_dae.py
148 lines (108 loc) · 4.35 KB
/
keras_cancer_dae.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
import numpy as np
from keras.models import Model
from keras.callbacks import ModelCheckpoint
from keras.layers import (Input, Dense, Concatenate)
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score, GridSearchCV, cross_validate, train_test_split
from keras.datasets import mnist
from sklearn.preprocessing import MinMaxScaler
# input image dimensions
img_rows, img_cols = 28, 28
input_shape = (3922, )
#cancer = open("cluster_features/Normal_Early/Hierarchical_200_Normal_Early.csv","r")
cancer = open("early_normal.csv","r")
next(cancer)
cancer = cancer.readlines()
cancer_main=[]
for i in cancer:
i=i.strip("\n").split(",")
if i[1] == '1':
print(i[1])
i[1] = 1
else:
i[1] = 0
cancer_main.append(i)
cancer_x=[]
for i in cancer_main:
cancer_x.append(i[3:])
cancer_met=[]
for i in cancer_x:
cancer_met.append([float(j) for j in i])
cancer_target=[]
for i in cancer_main:
cancer_target.append(i[1])
scaling = MinMaxScaler(feature_range=(-1,1)).fit(cancer_met)
#X_train = scaling.transform(X_train)
#X_test = scaling.transform(X_test)
cancer_met = scaling.transform(cancer_met)
# the data, shuffled and split between train and test sets
x_train, x_test, y_train, y_test = train_test_split(cancer_met, cancer_target, test_size=0.2, random_state=0)
x_train = np.array(x_train)
x_test = np.array(x_test)
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=np.shape(x_train))
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=np.shape(x_test))
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
x_feat_train = np.concatenate((x_train, x_test), axis=0)
x_feat_train_noisy = np.concatenate((x_train_noisy, x_test_noisy), axis=0)
print(x_feat_train_noisy.shape[0], ' dae train samples')
def DEEP_DAE(features_shape, act='relu'):
# Input
x = Input(name='inputs', shape=features_shape, dtype='float32')
o = x
o = Dense(512, activation=act, name='dense1')(o)
o = Dense(512, activation=act, name='dense2')(o)
o = Dense(512, activation=act, name='dense3')(o)
dec = Dense(3922, activation='sigmoid', name='dense_dec')(o)
# Print network summary
Model(inputs=x, outputs=dec).summary()
return Model(inputs=x, outputs=dec)
batch_size = 32
epochs = 40
autoenc = DEEP_DAE(input_shape)
autoenc.compile(optimizer='adadelta', loss='binary_crossentropy')
autoenc.fit(x_feat_train_noisy, x_feat_train, epochs=epochs,
batch_size=batch_size, shuffle=True)
def FEATURES(model):
input_ = model.get_layer('inputs').input
feat1 = model.get_layer('dense1').output
feat2 = model.get_layer('dense2').output
feat3 = model.get_layer('dense3').output
feat = Concatenate(name='concat')([feat1, feat2, feat3])
model = Model(inputs=[input_],
outputs=[feat1])
return model
_model = FEATURES(autoenc)
features_train = _model.predict(x_train)
features_test = _model.predict(x_test)
print(features_train.shape, ' train samples shape')
print(features_test.shape, ' train samples shape')
def DNN(features_shape, num_classes, act='relu'):
# Input
x = Input(name='inputs', shape=features_shape, dtype='float32')
o = x
# Encoder / Decoder
o = Dense(512, activation=act, name='dense1')(o)
o = Dense(512, activation=act, name='dense2')(o)
o = Dense(512, activation=act, name='dense3')(o)
y_pred = Dense(num_classes, activation='sigmoid', name='pred')(o)
# Print network summary
Model(inputs=x, outputs=y_pred).summary()
return Model(inputs=x, outputs=y_pred)
input_shape2 = (features_train.shape[1], )
num_classes = 2
y_train_ohe = np_utils.to_categorical(y_train, num_classes)
y_test_ohe = np_utils.to_categorical(y_test, num_classes)
batch_size = 128
epochs = 20
model_fname = 'dnn'
callbacks = [ModelCheckpoint(monitor='val_acc', filepath=model_fname + '.hdf5',
save_best_only=True, save_weights_only=True,
mode='min')]
deep = DNN(input_shape2, num_classes)
deep.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['acc'])
history = deep.fit(features_train, y_train_ohe, epochs=epochs,
batch_size=batch_size, shuffle=True,
validation_data=(features_test, y_test_ohe),
callbacks=callbacks)