-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_with_sklearn_keras.py
56 lines (40 loc) · 1.71 KB
/
train_with_sklearn_keras.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
SEED = 7
import numpy as np
np.random.seed(SEED)
from keras.models import Sequential
from keras.layers import Dense, Dropout
def create_model(input_dim, hidden_units, kernel_initializer, activation, dropout_rate, loss, optimizer, metrics):
model = Sequential()
for index, hidden_unit in enumerate(hidden_units):
if index == 0:
model.add(Dense(hidden_unit, kernel_initializer=kernel_initializer, activation=activation, input_dim=input_dim))
else:
model.add(Dense(hidden_unit, kernel_initializer=kernel_initializer, activation=activation))
model.add(Dropout(dropout_rate))
model.add(Dense(1, kernel_initializer=kernel_initializer, activation='sigmoid'))
model.compile(loss=loss, optimizer=optimizer, metrics=metrics)
return model
def main():
from keras.wrappers.scikit_learn import KerasClassifier
model = KerasClassifier(build_fn=create_model
,input_dim=686
,hidden_units=[256, 256, 256]
,kernel_initializer='uniform'
,activation='relu'
,dropout_rate=0.4
,loss='binary_crossentropy'
,optimizer='adam'
,metrics=['accuracy']
,epochs=50
,batch_size=128
)
import hppi
hppids = hppi.read_data_sets("data/02-ct-bin", one_hot=False)
X = hppids.datas
Y = hppids.labels
from sklearn.model_selection import StratifiedKFold, cross_val_score
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=SEED)
results = cross_val_score(model, X, Y, cv=kfold)
print(np.average(results))
if __name__ == "__main__":
main()