Skip to content

Commit 6cc39c3

Browse files
authored
Merge pull request #827 from Kaggle/add-keras-tuner
Install keras-tuner
2 parents 10a4e41 + 446f044 commit 6cc39c3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ RUN apt-get install -y libfreetype6-dev && \
8888
pip install lightgbm==2.3.1 && \
8989
pip install git+git://github.com/Lasagne/Lasagne.git && \
9090
pip install keras && \
91+
pip install keras-tuner && \
9192
pip install flake8 && \
9293
#neon
9394
cd /usr/local/src && \

tests/test_keras_tuner.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
import tensorflow as tf
4+
import numpy as np
5+
6+
from kerastuner.tuners import RandomSearch
7+
8+
9+
class TestKerasTuner(unittest.TestCase):
10+
def test_search(self):
11+
def build_model(hp):
12+
x_train = np.random.random((100, 28, 28))
13+
y_train = np.random.randint(10, size=(100, 1))
14+
x_test = np.random.random((20, 28, 28))
15+
y_test = np.random.randint(10, size=(20, 1))
16+
17+
model = tf.keras.models.Sequential([
18+
tf.keras.layers.Flatten(input_shape=(28, 28)),
19+
tf.keras.layers.Dense(128, activation='relu'),
20+
tf.keras.layers.Dropout(hp.Choice('dropout_rate', values=[0.2, 0.4])),
21+
tf.keras.layers.Dense(10, activation='softmax')
22+
])
23+
24+
model.compile(
25+
optimizer='adam',
26+
loss='sparse_categorical_crossentropy',
27+
metrics=['accuracy'])
28+
29+
return model
30+
31+
tuner = RandomSearch(build_model, objective='accuracy', max_trials=1, executions_per_trial=1, seed=1)
32+
33+
tuner.search(x_train, y_train, epochs=1)
34+
35+
self.assertEqual(0.4, tuner.get_best_hyperparameters(1)[0].get('dropout_rate'))

0 commit comments

Comments
 (0)