Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/guides/keras_tuner/visualize_tuning.py
3293 views
1
"""
2
Title: Visualize the hyperparameter tuning process
3
Author: Haifeng Jin
4
Date created: 2021/06/25
5
Last modified: 2021/06/05
6
Description: Using TensorBoard to visualize the hyperparameter tuning process in KerasTuner.
7
Accelerator: GPU
8
"""
9
10
"""shell
11
pip install keras-tuner -q
12
"""
13
14
"""
15
## Introduction
16
17
KerasTuner prints the logs to screen including the values of the
18
hyperparameters in each trial for the user to monitor the progress. However,
19
reading the logs is not intuitive enough to sense the influences of
20
hyperparameters have on the results, Therefore, we provide a method to
21
visualize the hyperparameter values and the corresponding evaluation results
22
with interactive figures using TensorBoard.
23
24
[TensorBoard](https://www.tensorflow.org/tensorboard) is a useful tool for
25
visualizing the machine learning experiments. It can monitor the losses and
26
metrics during the model training and visualize the model architectures.
27
Running KerasTuner with TensorBoard will give you additional features for
28
visualizing hyperparameter tuning results using its HParams plugin.
29
"""
30
31
"""
32
We will use a simple example of tuning a model for the MNIST image
33
classification dataset to show how to use KerasTuner with TensorBoard.
34
35
The first step is to download and format the data.
36
"""
37
38
import numpy as np
39
import keras_tuner
40
import keras
41
from keras import layers
42
43
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
44
# Normalize the pixel values to the range of [0, 1].
45
x_train = x_train.astype("float32") / 255
46
x_test = x_test.astype("float32") / 255
47
# Add the channel dimension to the images.
48
x_train = np.expand_dims(x_train, -1)
49
x_test = np.expand_dims(x_test, -1)
50
# Print the shapes of the data.
51
print(x_train.shape)
52
print(y_train.shape)
53
print(x_test.shape)
54
print(y_test.shape)
55
56
"""
57
Then, we write a `build_model` function to build the model with hyperparameters
58
and return the model. The hyperparameters include the type of model to use
59
(multi-layer perceptron or convolutional neural network), the number of layers,
60
the number of units or filters, whether to use dropout.
61
"""
62
63
64
def build_model(hp):
65
inputs = keras.Input(shape=(28, 28, 1))
66
# Model type can be MLP or CNN.
67
model_type = hp.Choice("model_type", ["mlp", "cnn"])
68
x = inputs
69
if model_type == "mlp":
70
x = layers.Flatten()(x)
71
# Number of layers of the MLP is a hyperparameter.
72
for i in range(hp.Int("mlp_layers", 1, 3)):
73
# Number of units of each layer are
74
# different hyperparameters with different names.
75
x = layers.Dense(
76
units=hp.Int(f"units_{i}", 32, 128, step=32),
77
activation="relu",
78
)(x)
79
else:
80
# Number of layers of the CNN is also a hyperparameter.
81
for i in range(hp.Int("cnn_layers", 1, 3)):
82
x = layers.Conv2D(
83
hp.Int(f"filters_{i}", 32, 128, step=32),
84
kernel_size=(3, 3),
85
activation="relu",
86
)(x)
87
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
88
x = layers.Flatten()(x)
89
90
# A hyperparamter for whether to use dropout layer.
91
if hp.Boolean("dropout"):
92
x = layers.Dropout(0.5)(x)
93
94
# The last layer contains 10 units,
95
# which is the same as the number of classes.
96
outputs = layers.Dense(units=10, activation="softmax")(x)
97
model = keras.Model(inputs=inputs, outputs=outputs)
98
99
# Compile the model.
100
model.compile(
101
loss="sparse_categorical_crossentropy",
102
metrics=["accuracy"],
103
optimizer="adam",
104
)
105
return model
106
107
108
"""
109
We can do a quick test of the models to check if it build successfully for both
110
CNN and MLP.
111
"""
112
113
114
# Initialize the `HyperParameters` and set the values.
115
hp = keras_tuner.HyperParameters()
116
hp.values["model_type"] = "cnn"
117
# Build the model using the `HyperParameters`.
118
model = build_model(hp)
119
# Test if the model runs with our data.
120
model(x_train[:100])
121
# Print a summary of the model.
122
model.summary()
123
124
# Do the same for MLP model.
125
hp.values["model_type"] = "mlp"
126
model = build_model(hp)
127
model(x_train[:100])
128
model.summary()
129
130
"""
131
Initialize the `RandomSearch` tuner with 10 trials and using validation
132
accuracy as the metric for selecting models.
133
"""
134
135
tuner = keras_tuner.RandomSearch(
136
build_model,
137
max_trials=10,
138
# Do not resume the previous search in the same directory.
139
overwrite=True,
140
objective="val_accuracy",
141
# Set a directory to store the intermediate results.
142
directory="/tmp/tb",
143
)
144
145
"""
146
Start the search by calling `tuner.search(...)`. To use TensorBoard, we need
147
to pass a `keras.callbacks.TensorBoard` instance to the callbacks.
148
"""
149
150
tuner.search(
151
x_train,
152
y_train,
153
validation_split=0.2,
154
epochs=2,
155
# Use the TensorBoard callback.
156
# The logs will be write to "/tmp/tb_logs".
157
callbacks=[keras.callbacks.TensorBoard("/tmp/tb_logs")],
158
)
159
160
"""
161
If running in Colab, the following two commands will show you the TensorBoard
162
inside Colab.
163
164
`%load_ext tensorboard`
165
166
`%tensorboard --logdir /tmp/tb_logs`
167
168
You have access to all the common features of the TensorBoard. For example, you
169
can view the loss and metrics curves and visualize the computational graph of
170
the models in different trials.
171
172
![Loss and metrics curves](https://i.imgur.com/ShulDtI.png)
173
![Computational graphs](https://i.imgur.com/8sRiT1I.png)
174
175
In addition to these features, we also have a HParams tab, in which there are
176
three views. In the table view, you can view the 10 different trials in a
177
table with the different hyperparameter values and evaluation metrics.
178
179
![Table view](https://i.imgur.com/OMcQdOw.png)
180
181
On the left side, you can specify the filters for certain hyperparameters. For
182
example, you can specify to only view the MLP models without the dropout layer
183
and with 1 to 2 dense layers.
184
185
![Filtered table view](https://i.imgur.com/yZpfaxN.png)
186
187
Besides the table view, it also provides two other views, parallel coordinates
188
view and scatter plot matrix view. They are just different visualization
189
methods for the same data. You can still use the panel on the left to filter
190
the results.
191
192
In the parallel coordinates view, each colored line is a trial.
193
The axes are the hyperparameters and evaluation metrics.
194
195
![Parallel coordinates view](https://i.imgur.com/PJ7HQUQ.png)
196
197
In the scatter plot matrix view, each dot is a trial. The plots are projections
198
of the trials on planes with different hyperparameter and metrics as the axes.
199
200
![Scatter plot matrix view](https://i.imgur.com/zjPjh6o.png)
201
202
"""
203
204