-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_keras.py
73 lines (49 loc) · 2.07 KB
/
search_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV
import tensorflow as tf
from tensorflow import keras
from skopt import BayesSearchCV
# In[ ]:
data = pd.read_feather('../../data_ugi/data10.feather')
# In[ ]:
y = data['READ30']
X = data.drop(['READ30'], axis=1)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=1)
# In[ ]:
input_shape = [X_train.shape[1]]
def build_model(n_hidden=1, n_neurons=100, dropout=0.4, activation = "relu", learning_rate=3e-3):
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=input_shape))
model.add(keras.layers.BatchNormalization())
for layer in range(n_hidden):
model.add(keras.layers.Dense(n_neurons))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Dropout(dropout))
model.add(keras.layers.Activation(activation))
model.add(keras.layers.Dense(1, activation="sigmoid"))
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
model.compile(loss="binary_crossentropy", metrics=['AUC'], optimizer=optimizer)
return model
# In[ ]:
keras_clf = tf.keras.wrappers.scikit_learn.KerasClassifier(build_model)
# In[ ]:
param_distribs = {
"n_hidden": [1, 2, 3, 4],
"n_neurons": [25, 50, 200, 500, 1000, 1500],
"dropout": [0.2, 0.4, 0.6, 0.8],
"activation": ["relu", "elu"],
"learning_rate": [3e-5, 3e-4, 3e-3, 3e-2],
}
# In[ ]:
early_stopping = keras.callbacks.EarlyStopping(
patience=15,
min_delta=1e-6,
restore_best_weights=True,)
rnd_search_cv = RandomizedSearchCV(keras_clf, param_distribs, n_iter=100, scoring='roc_auc', cv=5, n_jobs=-1, verbose=2)
rnd_search_cv.fit(X_train, y_train, epochs=100, batch_size=512,
validation_data=(X_valid, y_valid),
callbacks=[early_stopping])
results = pd.DataFrame(rnd_search_cv.cv_results_)
results.sort_values(by='rank_test_score').to_csv('../../results_ugi/results_keras3_data10.csv')