<jupyter_start><jupyter_text>[Keras Tuner の基礎](https://www.tensorflow.org/tutorials/keras/keras_tuner?hl=ja)<jupyter_code>import tensorflow as tf
from tensorflow import keras

import IPython

!pip install -q -U keras-tuner
import kerastuner as kt<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()

img_train = img_train.astype('float32') / 255.0
img_test  = img_test.astype('float32') / 255.0<jupyter_output><empty_output><jupyter_text>Modeling and Tuner Configuration<jupyter_code>def model_builder(hp):
    model = keras.Sequential()
    model.add(keras.layers.Flatten(input_shape=(28, 28)))

    # Keras Tuner Int method
    # parameter: unique_name, min_value, max_value, step
    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))
        
    # Keras Tuner Choice method
    # parameter: unique_name, values(array)
    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


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

class ClearTrainingOutput(tf.keras.callbacks.Callback):
    def on_train_end(*args, **kwargs):
        IPython.display.clear_output(wait = True)
        
tuner.search(img_train, label_train, epochs=10, validation_data=(img_test, label_test), callbacks=[ClearTrainingOutput()])
best_hp = tuner.get_best_hyperparameters(num_trials = 1)[0]

print(best_hp)
model = tuner.hypermodel.build(best_hp)
model.fit(img_train, label_train, epochs=10, validation_data=(img_test, label_test))<jupyter_output>Epoch 1/10
1875/1875 [==============================] - 7s 3ms/step - loss: 0.5867 - accuracy: 0.7931 - val_loss: 0.4331 - val_accuracy: 0.8395
Epoch 2/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3688 - accuracy: 0.8635 - val_loss: 0.3736 - val_accuracy: 0.8639
Epoch 3/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3301 - accuracy: 0.8793 - val_loss: 0.3615 - val_accuracy: 0.8699
Epoch 4/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2992 - accuracy: 0.8878 - val_loss: 0.3570 - val_accuracy: 0.8715
Epoch 5/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2752 - accuracy: 0.8978 - val_loss: 0.3443 - val_accuracy: 0.8767
Epoch 6/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2575 - accuracy: 0.9041 - val_loss: 0.3411 - val_accuracy: 0.8752
Epoch 7/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2528 - accuracy: 0.9055 - val_loss: 0.3454 - val_accuracy:[...]
{'fraction_non_alphanumeric': 0.1972538513060951, 'fraction_numerical': 0.08908238446081715, 'mean_word_length': 4.868369351669941, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '121831', 'license': 'MIT', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1097, 'n_words': 197, 'path': 'TensorFlow/basic-tutorial/tensorflow-tutorial-keras-tuner.ipynb', 'repo_name': 'hayatochigi/Kaggle', 'size': '2986'}