¡El Día de la Comunidad de ML es el 9 de noviembre! Únase a nosotros para recibir actualizaciones de TensorFlow, JAX, y más Más información

Introducción al afinador Keras

Ver en TensorFlow.org Ejecutar en Google Colab Ver fuente en GitHub Descargar cuaderno

Descripción general

Keras Tuner es una biblioteca que lo ayuda a elegir el conjunto óptimo de hiperparámetros para su programa TensorFlow. El proceso de seleccionar el conjunto adecuado de hiperparámetros para su aprendizaje máquina de aplicación (ML) se llama la sintonización hiperparámetro o hypertuning.

Los hiperparámetros son las variables que gobiernan el proceso de entrenamiento y la topología de un modelo ML. Estas variables permanecen constantes durante el proceso de capacitación e impactan directamente en el rendimiento de su programa de AA. Los hiperparámetros son de dos tipos:

  1. Hiperparámetros modelo que de selección influencia modelo tales como el número y anchura de las capas ocultas
  2. Hiperparámetros algoritmo que influyen en la velocidad y la calidad del algoritmo de aprendizaje, tales como la tasa de aprendizaje para estocástico pendiente de descenso (SGD) y el número de vecinos más cercanos para ak vecinos más próximos (KNN) clasificador

En este tutorial, usará Keras Tuner para realizar un hipertuning para una aplicación de clasificación de imágenes.

Configuración

import tensorflow as tf
from tensorflow import keras

Instale e importe el Keras Tuner.

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

Descargue y prepare el conjunto de datos

En este tutorial, usará el Keras sintonizador para encontrar las mejores hiperparámetros para un modelo de aprendizaje automático que clasifica las imágenes de la ropa de la base de datos moda MNIST .

Cargue los datos.

(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

Definir el modelo

Cuando crea un modelo para el hipertuning, también define el espacio de búsqueda de hiperparámetros además de la arquitectura del modelo. El modelo que ha configurado para hypertuning se denomina HyperModel.

Puede definir un hipermodelo a través de dos enfoques:

  • Mediante el uso de una función de generador de modelos
  • Subclasificando la HyperModel de clase de la API Keras sintonizador

También puede utilizar dos predefinidos HyperModel clases - HyperXception y HyperResNet para aplicaciones de visión por ordenador.

En este tutorial, utiliza una función de generador de modelos para definir el modelo de clasificación de imágenes. La función del generador de modelos devuelve un modelo compilado y usa los hiperparámetros que define en línea para hipertintonizar el modelo.

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

Cree una instancia del sintonizador y realice un hipertuning

Cree una instancia del sintonizador para realizar el hipertuning. El Keras sintonizador tiene cuatro sintonizadores disponibles - RandomSearch , Hyperband , BayesianOptimization y Sklearn . En este tutorial, se utiliza el Hiperbanda sintonizador.

Para crear una instancia del sintonizador Hiperbanda, se debe especificar el HyperModel, el objective de optimizar y el número máximo de épocas de tren ( max_epochs ).

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

El algoritmo de ajuste de hiperbanda utiliza la asignación de recursos adaptativa y la detención anticipada para converger rápidamente en un modelo de alto rendimiento. Esto se hace usando un soporte estilo campeonato deportivo. El algoritmo entrena una gran cantidad de modelos durante algunas épocas y lleva solo la mitad de los modelos con mejor rendimiento a la siguiente ronda. Hiperbanda determina el número de modelos de entrenar en un soporte mediante el cálculo de 1 + log factor ( max_epochs ) y redondeo hacia arriba al entero más cercano.

Cree una devolución de llamada para detener el entrenamiento antes de alcanzar un cierto valor para la pérdida de validación.

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

Ejecute la búsqueda de hiperparámetros. Los argumentos para el método de búsqueda son los mismos que los utilizados para tf.keras.model.fit además de la devolución de llamada anteriormente.

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.

Entrena el modelo

Encuentre el número óptimo de épocas para entrenar el modelo con los hiperparámetros obtenidos de la búsqueda.

# 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

Vuelva a crear una instancia del hipermodelo y entrénelo con el número óptimo de épocas desde arriba.

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>

Para finalizar este tutorial, evalúe el hipermodelo en los datos de prueba.

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]

El my_dir/intro_to_kt directorio contiene los registros y los puntos de control que se detallan para cada prueba (configuración del modelo) se ejecutan durante la búsqueda hiperparámetro. Si vuelve a ejecutar la búsqueda de hiperparámetros, Keras Tuner utiliza el estado existente de estos registros para reanudar la búsqueda. Para desactivar este comportamiento, pasar un adicional overwrite=True argumento al crear la instancia del sintonizador.

Resumen

En este tutorial, aprendió a usar Keras Tuner para ajustar los hiperparámetros de un modelo. Para obtener más información sobre Keras Tuner, consulte estos recursos adicionales:

También puedes ver el HParams tablero de instrumentos en TensorBoard para sintonizar de forma interactiva sus hiperparámetros modelo.