Skip to content

Commit 75e8b68

Browse files
authored
Merge Chapter13
MLP Tuning with Keras Tuner
2 parents d15bdd9 + b0afa9a commit 75e8b68

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

Chapter13/chapter_13.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
import keras_tuner as kt
4+
from tensorflow import keras
5+
from tensorflow.keras import layers
6+
7+
8+
class SimpleMLP(kt.HyperModel):
9+
def __init__(self, num_classes):
10+
self.num_classes = num_classes
11+
12+
def build(self, hp):
13+
units = hp.Int(name="units", min_value=16, max_value=64, step=16)
14+
optimizer = hp.Choice(name="optimizer", values=["rmsprop", "adam"])
15+
16+
model = keras.Sequential([
17+
layers.Dense(units, activation="relu"),
18+
layers.Dense(self.num_classes, activation="softmax")
19+
])
20+
21+
model.compile(
22+
optimizer=optimizer,
23+
loss="sparse_categorical_crossentropy",
24+
metrics=["accuracy"]
25+
)
26+
return model
27+
28+
class HyperparameterTuner:
29+
def __init__(self, hypermodel, max_trials=100, executions_per_trial=2, directory="mnist_kt_test"):
30+
self.tuner = kt.BayesianOptimization(
31+
hypermodel,
32+
objective="val_accuracy",
33+
max_trials=max_trials,
34+
executions_per_trial=executions_per_trial,
35+
directory=directory,
36+
overwrite=True
37+
)
38+
39+
def search(self, x_train, y_train, x_val, y_val):
40+
callbacks = [keras.callbacks.EarlyStopping(monitor="val_loss", patience=5)]
41+
42+
self.tuner.search(
43+
x_train, y_train,
44+
batch_size=128,
45+
epochs=100,
46+
validation_data=(x_val, y_val),
47+
callbacks=callbacks,
48+
verbose=2
49+
)
50+
51+
def get_best_hyperparameters(self, top_n=4):
52+
return self.tuner.get_best_hyperparameters(top_n)
53+
54+
def get_best_models(self, top_n=4):
55+
return self.tuner.get_best_models(top_n)
56+
57+
class ModelTrainer:
58+
def __init__(self, x_train_full, y_train_full, x_train, y_train, x_val, y_val):
59+
self.x_train_full = x_train_full
60+
self.y_train_full = y_train_full
61+
self.x_train = x_train
62+
self.y_train = y_train
63+
self.x_val = x_val
64+
self.y_val = y_val
65+
66+
def get_best_epoch(self, model_builder, hp):
67+
model = model_builder.build(hp)
68+
callbacks = [keras.callbacks.EarlyStopping(monitor="val_loss", mode="min", patience=10)]
69+
70+
history = model.fit(
71+
self.x_train, self.y_train,
72+
validation_data=(self.x_val, self.y_val),
73+
epochs=100,
74+
batch_size=128,
75+
callbacks=callbacks
76+
)
77+
78+
val_loss_per_epoch = history.history["val_loss"]
79+
best_epoch = val_loss_per_epoch.index(min(val_loss_per_epoch)) + 1
80+
print(f"Best epoch: {best_epoch}")
81+
return best_epoch
82+
83+
def get_best_trained_model(self, model_builder, hp):
84+
"""آموزش مدل با بهترین تعداد epoch."""
85+
best_epoch = self.get_best_epoch(model_builder, hp)
86+
model = model_builder.build(hp)
87+
88+
model.fit(
89+
self.x_train_full, self.y_train_full,
90+
batch_size=128,
91+
epochs=int(best_epoch * 1.2)
92+
)
93+
return model
94+
95+
def prepare_data():
96+
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
97+
x_train = x_train.reshape((-1, 28 * 28)).astype("float32") / 255
98+
x_test = x_test.reshape((-1, 28 * 28)).astype("float32") / 255
99+
100+
x_train_full = x_train[:]
101+
y_train_full = y_train[:]
102+
103+
num_val_samples = 10000
104+
x_train, x_val = x_train[:-num_val_samples], x_train[-num_val_samples:]
105+
y_train, y_val = y_train[:-num_val_samples], y_train[-num_val_samples:]
106+
107+
return x_train_full, y_train_full, x_train, y_train, x_val, y_val, x_test, y_test
108+
109+
110+
111+
keras.mixed_precision.set_global_policy("mixed_float16")
112+
x_train_full, y_train_full, x_train, y_train, x_val, y_val, x_test, y_test = prepare_data()
113+
114+
hypermodel = SimpleMLP(num_classes=10)
115+
tuner = HyperparameterTuner(hypermodel)
116+
tuner.search(x_train, y_train, x_val, y_val)
117+
118+
best_hps = tuner.get_best_hyperparameters(top_n=4)
119+
trainer = ModelTrainer(x_train_full, y_train_full, x_train, y_train, x_val, y_val)
120+
121+
best_models = []
122+
for hp in best_hps:
123+
model = trainer.get_best_trained_model(hypermodel, hp)
124+
model.evaluate(x_test, y_test)
125+
best_models.append(model)
126+
127+
best_models = tuner.get_best_models(top_n=4)

0 commit comments

Comments
 (0)