-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial08_kerasTuner.py
46 lines (33 loc) · 1.86 KB
/
tutorial08_kerasTuner.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
# https://www.tensorflow.org/tutorials/keras/keras_tuner?hl=ko
import tensorflow as tf
from tensorflow import keras
import IPython
import kerastuner as kt
(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0
def model_builder(hp):
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28, 28)))
# Tune the number of units in the first Dense layer
# Choose an optimal value between 32-512
hp_units = hp.Int('units', min_value = 32, max_value = 512, step = 32)
model.add(keras.layers.Dense(units = hp_units, activation = 'relu'))
model.add(keras.layers.Dense(10))
# Tune the learning rate for the optimizer
# Choose an optimal value from 0.01, 0.001, or 0.0001
hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-3, 1e-4])
model.compile(optimizer = keras.optimizers.Adam(learning_rate = hp_learning_rate),
loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True),
metrics = ['accuracy'])
return model
tuner = kt.Hyperband(model_builder, objective = 'val_accuracy', max_epochs = 10, factor = 3,
directory = 'my_dir', project_name = 'intro_to_kt')
class ClearTrainingOutput(tf.keras.callbacks.Callback):
def on_train_end(self, *args, **kwargs): # 이거 에러 안나는 데..? 이클립스가 self 넣으래서 추가함.
IPython.display.clear_output(wait = True)
tuner.search(img_train, label_train, epochs = 10, validation_data = (img_test, label_test),
callbacks = [ClearTrainingOutput()])
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]
model = tuner.hypermodel.build(best_hps)
model.fit(img_train, label_train, epochs = 10, validation_data = (img_test, label_test))