-
Notifications
You must be signed in to change notification settings - Fork 975
/
Copy pathtest_keras_tuner.py
35 lines (25 loc) · 1.18 KB
/
test_keras_tuner.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
import unittest
import tensorflow as tf
import numpy as np
from kerastuner.tuners import RandomSearch
class TestKerasTuner(unittest.TestCase):
def test_search(self):
def build_model(hp):
x_train = np.random.random((100, 28, 28))
y_train = np.random.randint(10, size=(100, 1))
x_test = np.random.random((20, 28, 28))
y_test = np.random.randint(10, size=(20, 1))
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(hp.Choice('dropout_rate', values=[0.2, 0.4])),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
tuner = RandomSearch(build_model, objective='accuracy', max_trials=1, executions_per_trial=1, seed=1)
tuner.search(x_train, y_train, epochs=1)
self.assertEqual(0.4, tuner.get_best_hyperparameters(1)[0].get('dropout_rate'))