##### Copyright 2020 The TensorFlow Authors.
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # ่ฎพ็ฝฎๆ—ฅๅฟ—็บงๅˆซไธบERROR๏ผŒไปฅๅ‡ๅฐ‘่ญฆๅ‘Šไฟกๆฏ
# ็ฆ็”จ Gemini ็š„ๅบ•ๅฑ‚ๅบ“๏ผˆgRPC ๅ’Œ Abseil๏ผ‰ๅœจๅˆๅง‹ๅŒ–ๆ—ฅๅฟ—่ญฆๅ‘Š
os.environ["GRPC_VERBOSITY"] = "ERROR"
os.environ["GLOG_minloglevel"] = "3"  # 0: INFO, 1: WARNING, 2: ERROR, 3: FATAL
os.environ["GLOG_minloglevel"] = "true"
import logging
import tensorflow as tf
tf.get_logger().setLevel(logging.ERROR)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
!export TF_FORCE_GPU_ALLOW_GROWTH=true
from pathlib import Path

temp_dir = Path(".temp")
temp_dir.mkdir(parents=True, exist_ok=True)

Keras Tuner ็ฎ€ไป‹#

ๅœจ TensorFlow.org ไธŠๆŸฅ็œ‹ ๅœจ Google Colab ไธญ่ฟ่กŒ ๅœจ GitHub ไธŠๆŸฅ็œ‹ๆบไปฃ็  ไธ‹่ฝฝ็ฌ”่ฎฐๆœฌ

ๆฆ‚่ฟฐ#

Keras Tuner ๆ˜ฏไธ€ไธชๅบ“๏ผŒๅฏๅธฎๅŠฉๆ‚จไธบ TensorFlow ็จ‹ๅบ้€‰ๆ‹ฉๆœ€ไฝณ็š„่ถ…ๅ‚ๆ•ฐ้›†ใ€‚ไธบๆ‚จ็š„ๆœบๅ™จๅญฆไน  (ML) ๅบ”็”จ้€‰ๆ‹ฉๆญฃ็กฎ็š„่ถ…ๅ‚ๆ•ฐ้›†๏ผŒ่ฟ™ไธ€่ฟ‡็จ‹็งฐไธบ่ถ…ๅ‚ๆ•ฐ่ฐƒ่Š‚ๆˆ–่ถ…่ฐƒใ€‚

่ถ…ๅ‚ๆ•ฐๆ˜ฏๆŽงๅˆถ่ฎญ็ปƒ่ฟ‡็จ‹ๅ’Œ ML ๆจกๅž‹ๆ‹“ๆ‰‘็š„ๅ˜้‡ใ€‚่ฟ™ไบ›ๅ˜้‡ๅœจ่ฎญ็ปƒ่ฟ‡็จ‹ไธญไฟๆŒไธๅ˜๏ผŒๅนถไผš็›ดๆŽฅๅฝฑๅ“ ML ็จ‹ๅบ็š„ๆ€ง่ƒฝใ€‚่ถ…ๅ‚ๆ•ฐๆœ‰ไธค็ง็ฑปๅž‹๏ผš

  1. ๆจกๅž‹่ถ…ๅ‚ๆ•ฐ๏ผšๅฝฑๅ“ๆจกๅž‹็š„้€‰ๆ‹ฉ๏ผŒไพ‹ๅฆ‚้š่—ๅฑ‚็š„ๆ•ฐ้‡ๅ’Œๅฎฝๅบฆ

  2. ็ฎ—ๆณ•่ถ…ๅ‚ๆ•ฐ๏ผšๅฝฑๅ“ๅญฆไน ็ฎ—ๆณ•็š„้€Ÿๅบฆๅ’Œ่ดจ้‡๏ผŒไพ‹ๅฆ‚้šๆœบๆขฏๅบฆไธ‹้™ (SGD) ็š„ๅญฆไน ็Ž‡ไปฅๅŠ k ่ฟ‘้‚ป (KNN) ๅˆ†็ฑปๅ™จ็š„่ฟ‘้‚ปๆ•ฐ

ๅœจๆœฌๆ•™็จ‹ไธญ๏ผŒๆ‚จๅฐ†ไฝฟ็”จ Keras Tuner ๅฏนๅ›พๅƒๅˆ†็ฑปๅบ”็”จๆ‰ง่กŒ่ถ…่ฐƒใ€‚

่ฎพ็ฝฎ#

import tensorflow as tf
from tensorflow import keras

ๅฎ‰่ฃ…ๅนถๅฏผๅ…ฅ Keras Tunerใ€‚

!pip install -q -U keras-tuner
import keras_tuner as kt

ไธ‹่ฝฝๅนถๅ‡†ๅค‡ๆ•ฐๆฎ้›†#

ๅœจๆœฌๆ•™็จ‹ไธญ๏ผŒๆ‚จๅฐ†ไฝฟ็”จ Keras Tuner ไธบๆŸไธชๅฏน Fashion MNIST ๆ•ฐๆฎ้›†ๅ†…็š„ๆœ่ฃ…ๅ›พๅƒ่ฟ›่กŒๅˆ†็ฑป็š„ๆœบๅ™จๅญฆไน ๆจกๅž‹ๆ‰พๅˆฐๆœ€ไฝณ่ถ…ๅ‚ๆ•ฐใ€‚

ๅŠ ่ฝฝๆ•ฐๆฎใ€‚

(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0

ๅฎšไน‰ๆจกๅž‹#

ๆž„ๅปบ็”จไบŽ่ถ…่ฐƒ็š„ๆจกๅž‹ๆ—ถ๏ผŒ้™คไบ†ๆจกๅž‹ๆžถๆž„ไน‹ๅค–๏ผŒ่ฟ˜่ฆๅฎšไน‰่ถ…ๅ‚ๆ•ฐๆœ็ดข็ฉบ้—ดใ€‚ๆ‚จไธบ่ถ…่ฐƒ่ฎพ็ฝฎ็š„ๆจกๅž‹็งฐไธบ่ถ…ๆจกๅž‹ใ€‚

ๆ‚จๅฏไปฅ้€š่ฟ‡ไธค็งๆ–นๅผๅฎšไน‰่ถ…ๆจกๅž‹๏ผš

  • ไฝฟ็”จๆจกๅž‹ๆž„ๅปบๅทฅๅ…ทๅ‡ฝๆ•ฐ

  • ๅฐ† Keras Tuner API ็š„ HyperModel ็ฑปๅญ็ฑปๅŒ–

ๆ‚จ่ฟ˜ๅฏไปฅๅฐ†ไธคไธช้ข„ๅฎšไน‰็š„ HyperModel ็ฑป HyperXception ๅ’Œ HyperResNet ็”จไบŽ่ฎก็ฎ—ๆœบ่ง†่ง‰ๅบ”็”จใ€‚

ๅœจๆœฌๆ•™็จ‹ไธญ๏ผŒๆ‚จๅฐ†ไฝฟ็”จๆจกๅž‹ๆž„ๅปบๅทฅๅ…ทๅ‡ฝๆ•ฐๆฅๅฎšไน‰ๅ›พๅƒๅˆ†็ฑปๆจกๅž‹ใ€‚ๆจกๅž‹ๆž„ๅปบๅทฅๅ…ทๅ‡ฝๆ•ฐๅฐ†่ฟ”ๅ›žๅทฒ็ผ–่ฏ‘็š„ๆจกๅž‹๏ผŒๅนถไฝฟ็”จๆ‚จไปฅๅ†…ๅตŒๆ–นๅผๅฎšไน‰็š„่ถ…ๅ‚ๆ•ฐๅฏนๆจกๅž‹่ฟ›่กŒ่ถ…่ฐƒใ€‚

def model_builder(hp):
  model = keras.Sequential()
  model.add(keras.layers.Flatten())

  # 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

ๅฎžไพ‹ๅŒ–่ฐƒ่Š‚ๅ™จๅนถๆ‰ง่กŒ่ถ…่ฐƒ#

ๅฎžไพ‹ๅŒ–่ฐƒ่Š‚ๅ™จไปฅๆ‰ง่กŒ่ถ…่ฐƒใ€‚Keras Tuner ๆไพ›ไบ†ๅ››็ง่ฐƒ่Š‚ๅ™จ๏ผšRandomSearchใ€Hyperbandใ€BayesianOptimization ๅ’Œ Sklearnใ€‚ๅœจๆœฌๆ•™็จ‹ไธญ๏ผŒๆ‚จๅฐ†ไฝฟ็”จ Hyperband ่ฐƒ่Š‚ๅ™จใ€‚

่ฆๅฎžไพ‹ๅŒ– Hyperband ่ฐƒ่Š‚ๅ™จ๏ผŒๅฟ…้กปๆŒ‡ๅฎš่ถ…ๆจกๅž‹ใ€่ฆไผ˜ๅŒ–็š„ objective ๅ’Œ่ฆ่ฎญ็ปƒ็š„ๆœ€ๅคงๅ‘จๆœŸๆ•ฐ (max_epochs)ใ€‚

tuner = kt.Hyperband(model_builder,
                     objective='val_accuracy',
                     max_epochs=10,
                     factor=3,
                     directory=temp_dir/'my_dir',
                     project_name='intro_to_kt')

Hyperband ่ฐƒ่Š‚็ฎ—ๆณ•ไฝฟ็”จ่‡ช้€‚ๅบ”่ต„ๆบๅˆ†้…ๅ’Œๆ—ฉๅœๆณ•ๆฅๅฟซ้€Ÿๆ”ถๆ•›ๅˆฐ้ซ˜ๆ€ง่ƒฝๆจกๅž‹ใ€‚่ฏฅ่ฟ‡็จ‹้‡‡็”จไบ†ไฝ“่‚ฒ็ซžๆŠ€ไบ‰ๅ† ๆจกๅผ็š„ๆŽ’้™คๆณ•ใ€‚็ฎ—ๆณ•ไผšๅฐ†ๅคง้‡ๆจกๅž‹่ฎญ็ปƒๅคšไธชๅ‘จๆœŸ๏ผŒๅนถไป…ๅฐ†ๆ€ง่ƒฝๆœ€้ซ˜็š„ไธ€ๅŠๆจกๅž‹้€ๅ…ฅไธ‹ไธ€่ฝฎ่ฎญ็ปƒใ€‚Hyperband ้€š่ฟ‡่ฎก็ฎ— 1 + logfactor(max_epochs) ๅนถๅฐ†ๅ…ถๅ‘ไธŠ่ˆๅ…ฅๅˆฐๆœ€ๆŽฅ่ฟ‘็š„ๆ•ดๆ•ฐๆฅ็กฎๅฎš่ฆ่ฎญ็ปƒ็š„ๆจกๅž‹็š„ๆ•ฐ้‡ใ€‚

ๅˆ›ๅปบๅ›ž่ฐƒไปฅๅœจ้ชŒ่ฏๆŸๅคฑ่พพๅˆฐ็‰นๅฎšๅ€ผๅŽๆๅ‰ๅœๆญข่ฎญ็ปƒใ€‚

stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)

่ฟ่กŒ่ถ…ๅ‚ๆ•ฐๆœ็ดขใ€‚้™คไบ†ไธŠ้ข็š„ๅ›ž่ฐƒๅค–๏ผŒๆœ็ดขๆ–นๆณ•็š„ๅ‚ๆ•ฐไนŸไธŽ tf.keras.model.fit ๆ‰€็”จๅ‚ๆ•ฐ็›ธๅŒใ€‚

tuner.search(img_train, label_train, epochs=50, validation_split=0.2, callbacks=[stop_early])

# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]

print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""")
Trial 30 Complete [00h 00m 40s]
val_accuracy: 0.8653333187103271

Best val_accuracy So Far: 0.8915833234786987
Total elapsed time: 00h 09m 06s

The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is 480 and the optimal learning rate for the optimizer
is 0.001.

่ฎญ็ปƒๆจกๅž‹#

ไฝฟ็”จไปŽๆœ็ดขไธญ่Žทๅพ—็š„่ถ…ๅ‚ๆ•ฐๆ‰พๅˆฐ่ฎญ็ปƒๆจกๅž‹็š„ๆœ€ไฝณๅ‘จๆœŸๆ•ฐใ€‚

# Build the model with the optimal hyperparameters and train it on the data for 50 epochs
model = tuner.hypermodel.build(best_hps)
history = model.fit(img_train, label_train, epochs=50, validation_split=0.2)

val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print('Best epoch: %d' % (best_epoch,))
Epoch 1/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 6s 3ms/step - accuracy: 0.7844 - loss: 0.6154 - val_accuracy: 0.8453 - val_loss: 0.4136
Epoch 2/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8609 - loss: 0.3789 - val_accuracy: 0.8729 - val_loss: 0.3500
Epoch 3/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8794 - loss: 0.3273 - val_accuracy: 0.8728 - val_loss: 0.3514
Epoch 4/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8872 - loss: 0.3048 - val_accuracy: 0.8827 - val_loss: 0.3235
Epoch 5/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.8939 - loss: 0.2821 - val_accuracy: 0.8809 - val_loss: 0.3286
Epoch 6/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 3ms/step - accuracy: 0.9030 - loss: 0.2598 - val_accuracy: 0.8760 - val_loss: 0.3422
Epoch 7/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9061 - loss: 0.2545 - val_accuracy: 0.8867 - val_loss: 0.3147
Epoch 8/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9094 - loss: 0.2420 - val_accuracy: 0.8893 - val_loss: 0.3141
Epoch 9/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9122 - loss: 0.2288 - val_accuracy: 0.8780 - val_loss: 0.3494
Epoch 10/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9165 - loss: 0.2218 - val_accuracy: 0.8899 - val_loss: 0.3222
Epoch 11/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9201 - loss: 0.2132 - val_accuracy: 0.8898 - val_loss: 0.3182
Epoch 12/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9235 - loss: 0.2020 - val_accuracy: 0.8924 - val_loss: 0.3073
Epoch 13/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9247 - loss: 0.1993 - val_accuracy: 0.8898 - val_loss: 0.3306
Epoch 14/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9291 - loss: 0.1879 - val_accuracy: 0.8894 - val_loss: 0.3475
Epoch 15/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9332 - loss: 0.1792 - val_accuracy: 0.8904 - val_loss: 0.3315
Epoch 16/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9332 - loss: 0.1797 - val_accuracy: 0.8928 - val_loss: 0.3508
Epoch 17/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9358 - loss: 0.1720 - val_accuracy: 0.8895 - val_loss: 0.3469
Epoch 18/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9390 - loss: 0.1633 - val_accuracy: 0.8905 - val_loss: 0.3497
Epoch 19/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9394 - loss: 0.1599 - val_accuracy: 0.8880 - val_loss: 0.3483
Epoch 20/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9436 - loss: 0.1496 - val_accuracy: 0.8885 - val_loss: 0.3815
Epoch 21/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9437 - loss: 0.1494 - val_accuracy: 0.8947 - val_loss: 0.3472
Epoch 22/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9457 - loss: 0.1457 - val_accuracy: 0.8883 - val_loss: 0.3934
Epoch 23/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9466 - loss: 0.1400 - val_accuracy: 0.8943 - val_loss: 0.3530
Epoch 24/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9487 - loss: 0.1352 - val_accuracy: 0.8901 - val_loss: 0.3810
Epoch 25/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9512 - loss: 0.1296 - val_accuracy: 0.8928 - val_loss: 0.3857
Epoch 26/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9515 - loss: 0.1283 - val_accuracy: 0.8956 - val_loss: 0.3769
Epoch 27/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9552 - loss: 0.1199 - val_accuracy: 0.8915 - val_loss: 0.3791
Epoch 28/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9553 - loss: 0.1173 - val_accuracy: 0.8946 - val_loss: 0.3851
Epoch 29/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9537 - loss: 0.1231 - val_accuracy: 0.8941 - val_loss: 0.4013
Epoch 30/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9576 - loss: 0.1152 - val_accuracy: 0.8942 - val_loss: 0.4023
Epoch 31/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9605 - loss: 0.1080 - val_accuracy: 0.8950 - val_loss: 0.4240
Epoch 32/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9596 - loss: 0.1069 - val_accuracy: 0.8918 - val_loss: 0.4392
Epoch 33/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9597 - loss: 0.1055 - val_accuracy: 0.8918 - val_loss: 0.4087
Epoch 34/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 3ms/step - accuracy: 0.9605 - loss: 0.1034 - val_accuracy: 0.8744 - val_loss: 0.5429
Epoch 35/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9623 - loss: 0.1027 - val_accuracy: 0.8938 - val_loss: 0.4789
Epoch 36/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9618 - loss: 0.0986 - val_accuracy: 0.8942 - val_loss: 0.4359
Epoch 37/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9657 - loss: 0.0934 - val_accuracy: 0.8948 - val_loss: 0.4627
Epoch 38/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9629 - loss: 0.0959 - val_accuracy: 0.8932 - val_loss: 0.4700
Epoch 39/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9639 - loss: 0.0953 - val_accuracy: 0.8938 - val_loss: 0.4628
Epoch 40/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9677 - loss: 0.0863 - val_accuracy: 0.8943 - val_loss: 0.4916
Epoch 41/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9695 - loss: 0.0828 - val_accuracy: 0.8948 - val_loss: 0.4793
Epoch 42/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9702 - loss: 0.0814 - val_accuracy: 0.8954 - val_loss: 0.4605
Epoch 43/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9699 - loss: 0.0805 - val_accuracy: 0.8959 - val_loss: 0.4710
Epoch 44/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9714 - loss: 0.0769 - val_accuracy: 0.8903 - val_loss: 0.4923
Epoch 45/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9699 - loss: 0.0777 - val_accuracy: 0.8861 - val_loss: 0.5372
Epoch 46/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9692 - loss: 0.0820 - val_accuracy: 0.8960 - val_loss: 0.5028
Epoch 47/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9710 - loss: 0.0748 - val_accuracy: 0.8913 - val_loss: 0.5318
Epoch 48/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9717 - loss: 0.0758 - val_accuracy: 0.8902 - val_loss: 0.5180
Epoch 49/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9728 - loss: 0.0718 - val_accuracy: 0.8907 - val_loss: 0.5315
Epoch 50/50
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9744 - loss: 0.0671 - val_accuracy: 0.8940 - val_loss: 0.5327
Best epoch: 46

้‡ๆ–ฐๅฎžไพ‹ๅŒ–่ถ…ๆจกๅž‹ๅนถไฝฟ็”จไธŠ้ข็š„ๆœ€ไฝณๅ‘จๆœŸๆ•ฐๅฏนๅ…ถ่ฟ›่กŒ่ฎญ็ปƒใ€‚

hypermodel = tuner.hypermodel.build(best_hps)

# Retrain the model
hypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2)
Epoch 1/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 6s 3ms/step - accuracy: 0.7809 - loss: 0.6163 - val_accuracy: 0.8484 - val_loss: 0.4384
Epoch 2/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8677 - loss: 0.3708 - val_accuracy: 0.8735 - val_loss: 0.3508
Epoch 3/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8762 - loss: 0.3344 - val_accuracy: 0.8788 - val_loss: 0.3403
Epoch 4/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8859 - loss: 0.3046 - val_accuracy: 0.8776 - val_loss: 0.3412
Epoch 5/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.8953 - loss: 0.2834 - val_accuracy: 0.8808 - val_loss: 0.3291
Epoch 6/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.8992 - loss: 0.2724 - val_accuracy: 0.8758 - val_loss: 0.3492
Epoch 7/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9066 - loss: 0.2501 - val_accuracy: 0.8848 - val_loss: 0.3247
Epoch 8/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9102 - loss: 0.2437 - val_accuracy: 0.8839 - val_loss: 0.3256
Epoch 9/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9117 - loss: 0.2329 - val_accuracy: 0.8885 - val_loss: 0.3255
Epoch 10/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9188 - loss: 0.2199 - val_accuracy: 0.8921 - val_loss: 0.3090
Epoch 11/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9197 - loss: 0.2120 - val_accuracy: 0.8873 - val_loss: 0.3218
Epoch 12/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9239 - loss: 0.2056 - val_accuracy: 0.8924 - val_loss: 0.3150
Epoch 13/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 3ms/step - accuracy: 0.9273 - loss: 0.1980 - val_accuracy: 0.8956 - val_loss: 0.3158
Epoch 14/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9293 - loss: 0.1931 - val_accuracy: 0.8917 - val_loss: 0.3301
Epoch 15/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9323 - loss: 0.1786 - val_accuracy: 0.8976 - val_loss: 0.3212
Epoch 16/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 3ms/step - accuracy: 0.9353 - loss: 0.1746 - val_accuracy: 0.8898 - val_loss: 0.3441
Epoch 17/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9347 - loss: 0.1709 - val_accuracy: 0.8947 - val_loss: 0.3288
Epoch 18/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9386 - loss: 0.1641 - val_accuracy: 0.8929 - val_loss: 0.3327
Epoch 19/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9424 - loss: 0.1533 - val_accuracy: 0.8949 - val_loss: 0.3435
Epoch 20/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9421 - loss: 0.1555 - val_accuracy: 0.8934 - val_loss: 0.3547
Epoch 21/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9448 - loss: 0.1469 - val_accuracy: 0.8953 - val_loss: 0.3412
Epoch 22/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9452 - loss: 0.1449 - val_accuracy: 0.8937 - val_loss: 0.3451
Epoch 23/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9477 - loss: 0.1407 - val_accuracy: 0.8945 - val_loss: 0.3516
Epoch 24/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9472 - loss: 0.1392 - val_accuracy: 0.8927 - val_loss: 0.3789
Epoch 25/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9506 - loss: 0.1318 - val_accuracy: 0.8898 - val_loss: 0.3972
Epoch 26/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9517 - loss: 0.1303 - val_accuracy: 0.8828 - val_loss: 0.4100
Epoch 27/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9533 - loss: 0.1262 - val_accuracy: 0.8924 - val_loss: 0.3892
Epoch 28/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9553 - loss: 0.1200 - val_accuracy: 0.8952 - val_loss: 0.3770
Epoch 29/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9571 - loss: 0.1156 - val_accuracy: 0.8959 - val_loss: 0.4118
Epoch 30/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9559 - loss: 0.1157 - val_accuracy: 0.8954 - val_loss: 0.3937
Epoch 31/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9579 - loss: 0.1112 - val_accuracy: 0.8932 - val_loss: 0.4362
Epoch 32/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9582 - loss: 0.1114 - val_accuracy: 0.8940 - val_loss: 0.4085
Epoch 33/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9594 - loss: 0.1066 - val_accuracy: 0.8959 - val_loss: 0.4060
Epoch 34/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 3ms/step - accuracy: 0.9607 - loss: 0.1041 - val_accuracy: 0.8966 - val_loss: 0.4171
Epoch 35/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9609 - loss: 0.1024 - val_accuracy: 0.8908 - val_loss: 0.4622
Epoch 36/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9613 - loss: 0.1018 - val_accuracy: 0.8754 - val_loss: 0.4987
Epoch 37/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9633 - loss: 0.0964 - val_accuracy: 0.8890 - val_loss: 0.4426
Epoch 38/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9644 - loss: 0.0931 - val_accuracy: 0.8934 - val_loss: 0.4596
Epoch 39/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9656 - loss: 0.0923 - val_accuracy: 0.8958 - val_loss: 0.4529
Epoch 40/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9687 - loss: 0.0821 - val_accuracy: 0.8915 - val_loss: 0.4697
Epoch 41/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 3s 2ms/step - accuracy: 0.9685 - loss: 0.0824 - val_accuracy: 0.8928 - val_loss: 0.4751
Epoch 42/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9662 - loss: 0.0861 - val_accuracy: 0.8842 - val_loss: 0.5023
Epoch 43/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9690 - loss: 0.0812 - val_accuracy: 0.8896 - val_loss: 0.4934
Epoch 44/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9684 - loss: 0.0836 - val_accuracy: 0.8953 - val_loss: 0.4849
Epoch 45/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9703 - loss: 0.0785 - val_accuracy: 0.8932 - val_loss: 0.5106
Epoch 46/46
1500/1500 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 4s 2ms/step - accuracy: 0.9717 - loss: 0.0782 - val_accuracy: 0.8932 - val_loss: 0.4991
<keras.src.callbacks.history.History at 0x7fb85c1195e0>

่ฆๅฎŒๆˆๆœฌๆ•™็จ‹๏ผŒ่ฏทๅœจๆต‹่ฏ•ๆ•ฐๆฎไธŠ่ฏ„ไผฐ่ถ…ๆจกๅž‹ใ€‚

eval_result = hypermodel.evaluate(img_test, label_test)
print("[test loss, test accuracy]:", eval_result)
313/313 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 2s 6ms/step - accuracy: 0.8867 - loss: 0.5731
[test loss, test accuracy]: [0.5481381416320801, 0.8877999782562256]

my_dir/intro_to_kt ็›ฎๅฝ•ไธญๅŒ…ๅซไบ†ๅœจ่ถ…ๅ‚ๆ•ฐๆœ็ดขๆœŸ้—ดๆฏๆฌก่ฏ•้ชŒ๏ผˆๆจกๅž‹้…็ฝฎ๏ผ‰่ฟ่กŒ็š„่ฏฆ็ป†ๆ—ฅๅฟ—ๅ’Œๆฃ€ๆŸฅ็‚นใ€‚ๅฆ‚ๆžœ้‡ๆ–ฐ่ฟ่กŒ่ถ…ๅ‚ๆ•ฐๆœ็ดข๏ผŒKeras Tuner ๅฐ†ไฝฟ็”จ่ฟ™ไบ›ๆ—ฅๅฟ—ไธญ่ฎฐๅฝ•็š„็Žฐๆœ‰็Šถๆ€ๆฅ็ปง็ปญๆœ็ดขใ€‚่ฆๅœ็”จๆญค่กŒไธบ๏ผŒ่ฏทๅœจๅฎžไพ‹ๅŒ–่ฐƒ่Š‚ๅ™จๆ—ถไผ ้€’ไธ€ไธช้™„ๅŠ ็š„ overwrite = True ๅ‚ๆ•ฐใ€‚

ๆ€ป็ป“#

ๅœจๆœฌๆ•™็จ‹ไธญ๏ผŒๆ‚จๅญฆไน ไบ†ๅฆ‚ไฝ•ไฝฟ็”จ Keras Tuner ่ฐƒ่Š‚ๆจกๅž‹็š„่ถ…ๅ‚ๆ•ฐใ€‚่ฆ่ฏฆ็ป†ไบ†่งฃ Keras Tuner๏ผŒ่ฏทๆŸฅ็œ‹ไปฅไธ‹ๅ…ถไป–่ต„ๆบ๏ผš

ๅฆ่ฏทๆŸฅ็œ‹ TensorBoard ไธญ็š„ HParams Dashboard๏ผŒไปฅไบคไบ’ๆ–นๅผ่ฐƒ่Š‚ๆจกๅž‹่ถ…ๅ‚ๆ•ฐใ€‚