Wprowadzenie do tunera Keras

Zobacz na TensorFlow.org Uruchom w Google Colab Wyświetl źródło na GitHub Pobierz notatnik

Przegląd

Keras Tuner to biblioteka, która pomaga wybrać optymalny zestaw hiperparametrów dla programu TensorFlow. Proces wyboru odpowiedniego zestawu hiperparametrów dla Twojego uczenia maszynowego (ML) aplikacji nazywa strojenie hyperparameter lub hypertuning.

Hiperparametry to zmienne, które zarządzają procesem uczenia i topologią modelu ML. Zmienne te pozostają stałe w trakcie procesu szkoleniowego i bezpośrednio wpływają na wydajność programu ML. Hiperparametry są dwojakiego rodzaju:

  1. Model hiperparametrów których wybór modelu wpływ takie jak liczby i szerokości warstw ukrytych
  2. Algorytm hiperparametrów wpływające na szybkość i jakość algorytmu uczenia, takich jak szybkość uczenia się dla Stochastic Gradient Descent (SGD) a liczbą najbliższych sąsiadów do AK najbliższych sąsiadów (KNN) klasyfikatora

W tym samouczku użyjesz Keras Tuner do wykonania hipertuningu dla aplikacji do klasyfikacji obrazów.

Ustawiać

import tensorflow as tf
from tensorflow import keras

Zainstaluj i zaimportuj Keras Tuner.

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

Pobierz i przygotuj zbiór danych

W tym ćwiczeniu użyjesz Keras Tuner znaleźć najlepszą hiperparametrów dla modelu uczenia się maszyna, która klasyfikuje zdjęcia ubrań z zestawu danych Moda MNIST .

Załaduj dane.

(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0

Zdefiniuj model

Podczas budowania modelu do hipertuningu oprócz architektury modelu definiuje się również przestrzeń wyszukiwania hiperparametrów. Model skonfigurować dla hypertuning nazywa się hypermodel.

Hipermodel można zdefiniować na dwa sposoby:

  • Korzystając z funkcji konstruktora modeli
  • Przez instacji HyperModel klasę API Keras Tuner

Można również użyć dwóch predefiniowanych HyperModel ćwiczenia - HyperXception i HyperResNet do zastosowań wizyjnych komputer.

W tym samouczku użyjesz funkcji konstruktora modelu do zdefiniowania modelu klasyfikacji obrazów. Funkcja konstruktora modelu zwraca skompilowany model i używa hiperparametrów zdefiniowanych w tekście, aby dostroić model.

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

Utwórz wystąpienie tunera i wykonaj hipertuning

Utwórz instancję tunera, aby wykonać hipertuning. Keras Tuner posiada cztery tunery dostępne - RandomSearch , Hyperband , BayesianOptimization i Sklearn . W tym samouczku użyć HYPERBAND tuner.

Instancję tuner HYPERBAND, należy określić hypermodel, tym objective , aby zoptymalizować i maksymalna ilość epok trenować ( max_epochs ).

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

Algorytm strojenia Hyperband wykorzystuje adaptacyjną alokację zasobów i wczesne zatrzymywanie, aby szybko uzyskać zbieżność w modelu o wysokiej wydajności. Odbywa się to za pomocą wspornika w stylu mistrzostw sportowych. Algorytm szkoli dużą liczbę modeli przez kilka epok i przenosi do następnej rundy tylko najskuteczniejszą połowę modeli. Hyperband określa liczbę modeli trenować w uchwycie obliczając 1 + log factor ( max_epochs ) i zaokrąglenie jej w górę do najbliższej liczby całkowitej.

Utwórz wywołanie zwrotne, aby zatrzymać trenowanie wcześniej po osiągnięciu określonej wartości utraty walidacji.

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

Uruchom wyszukiwanie hiperparametrów. Argumenty dotyczące sposobu wyszukiwania są takie same jak te używane do tf.keras.model.fit oprócz zwrotnego powyżej.

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 27s]
val_accuracy: 0.8523333072662354

Best val_accuracy So Far: 0.8889999985694885
Total elapsed time: 00h 05m 35s
INFO:tensorflow:Oracle triggered exit

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

Trenuj modelkę

Znajdź optymalną liczbę epok do trenowania modelu za pomocą hiperparametrów uzyskanych z wyszukiwania.

# 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 [==============================] - 3s 2ms/step - loss: 0.4977 - accuracy: 0.8242 - val_loss: 0.4863 - val_accuracy: 0.8190
Epoch 2/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.3720 - accuracy: 0.8651 - val_loss: 0.3629 - val_accuracy: 0.8681
Epoch 3/50
1500/1500 [==============================] - 3s 2ms/step - loss: 0.3329 - accuracy: 0.8783 - val_loss: 0.3530 - val_accuracy: 0.8718
Epoch 4/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.3087 - accuracy: 0.8857 - val_loss: 0.3588 - val_accuracy: 0.8673
Epoch 5/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2900 - accuracy: 0.8903 - val_loss: 0.3117 - val_accuracy: 0.8876
Epoch 6/50
1500/1500 [==============================] - 3s 2ms/step - loss: 0.2742 - accuracy: 0.8971 - val_loss: 0.3571 - val_accuracy: 0.8754
Epoch 7/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2591 - accuracy: 0.9045 - val_loss: 0.3187 - val_accuracy: 0.8873
Epoch 8/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2470 - accuracy: 0.9074 - val_loss: 0.3161 - val_accuracy: 0.8888
Epoch 9/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2368 - accuracy: 0.9112 - val_loss: 0.3652 - val_accuracy: 0.8741
Epoch 10/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2273 - accuracy: 0.9150 - val_loss: 0.3198 - val_accuracy: 0.8898
Epoch 11/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2168 - accuracy: 0.9183 - val_loss: 0.3274 - val_accuracy: 0.8837
Epoch 12/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2073 - accuracy: 0.9225 - val_loss: 0.3253 - val_accuracy: 0.8887
Epoch 13/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2018 - accuracy: 0.9236 - val_loss: 0.3616 - val_accuracy: 0.8821
Epoch 14/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1924 - accuracy: 0.9268 - val_loss: 0.3484 - val_accuracy: 0.8904
Epoch 15/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1854 - accuracy: 0.9298 - val_loss: 0.3100 - val_accuracy: 0.8960
Epoch 16/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1767 - accuracy: 0.9337 - val_loss: 0.3314 - val_accuracy: 0.8928
Epoch 17/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1737 - accuracy: 0.9336 - val_loss: 0.3347 - val_accuracy: 0.8932
Epoch 18/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1665 - accuracy: 0.9373 - val_loss: 0.3376 - val_accuracy: 0.8933
Epoch 19/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1611 - accuracy: 0.9395 - val_loss: 0.3484 - val_accuracy: 0.8938
Epoch 20/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1569 - accuracy: 0.9420 - val_loss: 0.3904 - val_accuracy: 0.8802
Epoch 21/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1517 - accuracy: 0.9429 - val_loss: 0.3665 - val_accuracy: 0.8904
Epoch 22/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1466 - accuracy: 0.9452 - val_loss: 0.3482 - val_accuracy: 0.8959
Epoch 23/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1418 - accuracy: 0.9460 - val_loss: 0.3569 - val_accuracy: 0.8950
Epoch 24/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1390 - accuracy: 0.9481 - val_loss: 0.4292 - val_accuracy: 0.8806
Epoch 25/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1329 - accuracy: 0.9496 - val_loss: 0.3706 - val_accuracy: 0.8957
Epoch 26/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1302 - accuracy: 0.9509 - val_loss: 0.3662 - val_accuracy: 0.8929
Epoch 27/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1243 - accuracy: 0.9535 - val_loss: 0.3984 - val_accuracy: 0.8907
Epoch 28/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1244 - accuracy: 0.9537 - val_loss: 0.3822 - val_accuracy: 0.8964
Epoch 29/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1198 - accuracy: 0.9551 - val_loss: 0.4285 - val_accuracy: 0.8872
Epoch 30/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1166 - accuracy: 0.9563 - val_loss: 0.4269 - val_accuracy: 0.8918
Epoch 31/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1120 - accuracy: 0.9585 - val_loss: 0.4127 - val_accuracy: 0.8922
Epoch 32/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1118 - accuracy: 0.9575 - val_loss: 0.4294 - val_accuracy: 0.8931
Epoch 33/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1093 - accuracy: 0.9592 - val_loss: 0.4230 - val_accuracy: 0.8928
Epoch 34/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1043 - accuracy: 0.9602 - val_loss: 0.4282 - val_accuracy: 0.8947
Epoch 35/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1031 - accuracy: 0.9612 - val_loss: 0.4217 - val_accuracy: 0.8868
Epoch 36/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1029 - accuracy: 0.9609 - val_loss: 0.4487 - val_accuracy: 0.8957
Epoch 37/50
1500/1500 [==============================] - 3s 2ms/step - loss: 0.1010 - accuracy: 0.9623 - val_loss: 0.4623 - val_accuracy: 0.8908
Epoch 38/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0945 - accuracy: 0.9649 - val_loss: 0.4769 - val_accuracy: 0.8885
Epoch 39/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0932 - accuracy: 0.9654 - val_loss: 0.4907 - val_accuracy: 0.8908
Epoch 40/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0932 - accuracy: 0.9653 - val_loss: 0.4886 - val_accuracy: 0.8931
Epoch 41/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0890 - accuracy: 0.9666 - val_loss: 0.4780 - val_accuracy: 0.8917
Epoch 42/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0879 - accuracy: 0.9661 - val_loss: 0.4549 - val_accuracy: 0.8943
Epoch 43/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0876 - accuracy: 0.9669 - val_loss: 0.4959 - val_accuracy: 0.8936
Epoch 44/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0844 - accuracy: 0.9678 - val_loss: 0.4789 - val_accuracy: 0.8944
Epoch 45/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0803 - accuracy: 0.9705 - val_loss: 0.5110 - val_accuracy: 0.8939
Epoch 46/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0825 - accuracy: 0.9696 - val_loss: 0.4860 - val_accuracy: 0.8971
Epoch 47/50
1500/1500 [==============================] - 3s 2ms/step - loss: 0.0771 - accuracy: 0.9709 - val_loss: 0.5046 - val_accuracy: 0.8950
Epoch 48/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0775 - accuracy: 0.9709 - val_loss: 0.5245 - val_accuracy: 0.8918
Epoch 49/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0757 - accuracy: 0.9711 - val_loss: 0.5160 - val_accuracy: 0.8931
Epoch 50/50
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0775 - accuracy: 0.9716 - val_loss: 0.5132 - val_accuracy: 0.8959
Best epoch: 46

Ponownie wykonaj instancję hipermodelu i wytrenuj go z optymalną liczbą epok z góry.

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 [==============================] - 3s 2ms/step - loss: 0.4972 - accuracy: 0.8242 - val_loss: 0.4372 - val_accuracy: 0.8413
Epoch 2/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.3681 - accuracy: 0.8646 - val_loss: 0.3778 - val_accuracy: 0.8640
Epoch 3/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.3322 - accuracy: 0.8771 - val_loss: 0.3637 - val_accuracy: 0.8618
Epoch 4/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.3065 - accuracy: 0.8869 - val_loss: 0.3397 - val_accuracy: 0.8799
Epoch 5/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2858 - accuracy: 0.8943 - val_loss: 0.3257 - val_accuracy: 0.8848
Epoch 6/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2724 - accuracy: 0.8983 - val_loss: 0.3138 - val_accuracy: 0.8856
Epoch 7/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2582 - accuracy: 0.9035 - val_loss: 0.3203 - val_accuracy: 0.8846
Epoch 8/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2466 - accuracy: 0.9074 - val_loss: 0.3291 - val_accuracy: 0.8896
Epoch 9/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2356 - accuracy: 0.9109 - val_loss: 0.3321 - val_accuracy: 0.8847
Epoch 10/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2256 - accuracy: 0.9157 - val_loss: 0.3395 - val_accuracy: 0.8873
Epoch 11/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2167 - accuracy: 0.9191 - val_loss: 0.3407 - val_accuracy: 0.8842
Epoch 12/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2096 - accuracy: 0.9208 - val_loss: 0.3269 - val_accuracy: 0.8913
Epoch 13/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.2012 - accuracy: 0.9237 - val_loss: 0.3243 - val_accuracy: 0.8948
Epoch 14/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1930 - accuracy: 0.9271 - val_loss: 0.3260 - val_accuracy: 0.8916
Epoch 15/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1862 - accuracy: 0.9305 - val_loss: 0.3384 - val_accuracy: 0.8828
Epoch 16/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1811 - accuracy: 0.9313 - val_loss: 0.3279 - val_accuracy: 0.8940
Epoch 17/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1737 - accuracy: 0.9345 - val_loss: 0.3451 - val_accuracy: 0.8914
Epoch 18/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1685 - accuracy: 0.9353 - val_loss: 0.3380 - val_accuracy: 0.8924
Epoch 19/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1639 - accuracy: 0.9374 - val_loss: 0.3551 - val_accuracy: 0.8927
Epoch 20/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1593 - accuracy: 0.9404 - val_loss: 0.3579 - val_accuracy: 0.8957
Epoch 21/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1527 - accuracy: 0.9423 - val_loss: 0.3822 - val_accuracy: 0.8841
Epoch 22/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1487 - accuracy: 0.9443 - val_loss: 0.3670 - val_accuracy: 0.8936
Epoch 23/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1460 - accuracy: 0.9455 - val_loss: 0.3735 - val_accuracy: 0.8911
Epoch 24/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1413 - accuracy: 0.9469 - val_loss: 0.3616 - val_accuracy: 0.8947
Epoch 25/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1359 - accuracy: 0.9482 - val_loss: 0.3641 - val_accuracy: 0.8956
Epoch 26/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1319 - accuracy: 0.9500 - val_loss: 0.3693 - val_accuracy: 0.8928
Epoch 27/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1288 - accuracy: 0.9508 - val_loss: 0.3755 - val_accuracy: 0.8937
Epoch 28/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1249 - accuracy: 0.9530 - val_loss: 0.3808 - val_accuracy: 0.8959
Epoch 29/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1218 - accuracy: 0.9546 - val_loss: 0.4050 - val_accuracy: 0.8923
Epoch 30/46
1500/1500 [==============================] - 3s 2ms/step - loss: 0.1192 - accuracy: 0.9547 - val_loss: 0.3844 - val_accuracy: 0.8967
Epoch 31/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1164 - accuracy: 0.9562 - val_loss: 0.4062 - val_accuracy: 0.8927
Epoch 32/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1126 - accuracy: 0.9565 - val_loss: 0.4070 - val_accuracy: 0.8974
Epoch 33/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1121 - accuracy: 0.9571 - val_loss: 0.4297 - val_accuracy: 0.8895
Epoch 34/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1056 - accuracy: 0.9600 - val_loss: 0.4263 - val_accuracy: 0.8962
Epoch 35/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1062 - accuracy: 0.9593 - val_loss: 0.4547 - val_accuracy: 0.8888
Epoch 36/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.1033 - accuracy: 0.9610 - val_loss: 0.4341 - val_accuracy: 0.8891
Epoch 37/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0987 - accuracy: 0.9629 - val_loss: 0.4396 - val_accuracy: 0.8894
Epoch 38/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0961 - accuracy: 0.9631 - val_loss: 0.4545 - val_accuracy: 0.8939
Epoch 39/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0935 - accuracy: 0.9638 - val_loss: 0.4612 - val_accuracy: 0.8915
Epoch 40/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0932 - accuracy: 0.9646 - val_loss: 0.4712 - val_accuracy: 0.8882
Epoch 41/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0904 - accuracy: 0.9653 - val_loss: 0.4784 - val_accuracy: 0.8941
Epoch 42/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0896 - accuracy: 0.9664 - val_loss: 0.4697 - val_accuracy: 0.8952
Epoch 43/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0851 - accuracy: 0.9674 - val_loss: 0.4728 - val_accuracy: 0.8913
Epoch 44/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0855 - accuracy: 0.9675 - val_loss: 0.4633 - val_accuracy: 0.8964
Epoch 45/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0834 - accuracy: 0.9693 - val_loss: 0.5373 - val_accuracy: 0.8875
Epoch 46/46
1500/1500 [==============================] - 2s 2ms/step - loss: 0.0826 - accuracy: 0.9696 - val_loss: 0.4981 - val_accuracy: 0.8917
<tensorflow.python.keras.callbacks.History at 0x7f5d0832be10>

Aby zakończyć ten samouczek, oceń hipermodel na danych testowych.

eval_result = hypermodel.evaluate(img_test, label_test)
print("[test loss, test accuracy]:", eval_result)
313/313 [==============================] - 1s 2ms/step - loss: 0.5843 - accuracy: 0.8865
[test loss, test accuracy]: [0.5842637419700623, 0.8865000009536743]

my_dir/intro_to_kt katalog zawiera szczegółowe dzienniki i punktów kontrolnych dla każdej próby (konfiguracja modelu) uruchamianych podczas wyszukiwania hyperparameter. Jeśli ponownie uruchomisz wyszukiwanie hiperparametrów, Keras Tuner użyje istniejącego stanu z tych dzienników, aby wznowić wyszukiwanie. Aby wyłączyć tę funkcję, należy przekazać dodatkowe overwrite=True argumentu podczas uruchamianiu tuner.

streszczenie

W tym samouczku nauczyłeś się używać Keras Tuner do dostrajania hiperparametrów modelu. Aby dowiedzieć się więcej o Keras Tuner, zapoznaj się z tymi dodatkowymi zasobami:

Sprawdź również HParams Dashboard w TensorBoard interaktywnie dostroić swój model hiperparametrów.