简体   繁体   中英

No hparams data was found when using tensorboard with keras-tuner

versions: tensorboard==2.9.0 , keras-tuner==1.1.2

Here is simple model of binary classification with hyperparameters to search added in the model by using keras-tuner.

def build_model(hp):
n_layers = 4

n_features = len(X_train.columns)
inputs = tf.keras.Input(shape=(n_features,))

dense = tf.keras.layers.Dense(hp.Int("input_units", min_value=128, max_value=256, step=32),
                              activation=hp.Choice("activation", ['relu', 'tanh'])
                             )(inputs)
dense = tf.keras.layers.Dropout(0.2)(dense)

# num_layer as hyperparameter
for i in range(hp.Int("dense_layer", 1, n_layers)):
    dense = tf.keras.layers.Dense(hp.Int(f"hidden_unit_{i}", 128, 256, 32),
                                  activation=hp.Choice("activation", ['relu', 'tanh'])
                                 )(dense)
    
output = tf.keras.layers.Dense(1, activation='sigmoid')(dense)
model = tf.keras.Model(inputs=inputs, outputs=output)

lr = hp.Float("lr", min_value=1e-4, max_value=1e-1, sampling="log")
model.compile(optimizer=tf.keras.optimizers.Adam(lr),
          loss=tf.keras.losses.BinaryCrossentropy(),
          metrics=metrics)
return model

hyperparameter search space would be

 {neurons:[128, 160, 192, 224, 256], 
  num_hidden_layers:[1,2,3], 
  activation_function = ['relu', 'tanh'],
  learning_rate = [0.0001, 0.001, 0.01]}

Now begin search

tuner = RandomSearch(
    build_model,
    objective = kt.Objective("val_binary_accuracy", direction="max"),
    max_trials = 3,
    executions_per_trial = 1,
    directory=LOG_DIR
)
tensorboard_cb = tf.keras.callbacks.TensorBoard('logs/hyp_tune/')

tuner.search(X_train, y_train, epochs=10, batch_size=512, 
            validation_data=(X_test, y_test),
            callbacks=[tensorboard_cb]
            )

From keras-tuner guide https://keras.io/guides/keras_tuner/visualize_tuning/ This should work fine, showing Hparams when opening tensorboard.

However when I select HPARAMS tab, it outputs message below:

No hparams data was found.
Probable causes:

You haven’t written any hparams data to your event files.
Event files are still being loaded (try reloading this page).
TensorBoard can’t find your event files.
If you’re new to using TensorBoard, and want to find out how to add data and set up your event files, check out the README and perhaps the TensorBoard tutorial.

If you think TensorBoard is configured properly, please see the section of the README devoted to missing data problems and consider filing an issue on GitHub.

I've tried re-searching, restarting notebook, however cannot still no luck.

[EDIT] when I load tensorboard tensorboard --logdir='logs/t1' it should show logs/t1 at left side of screen below Runs however it shows logs/t0 which is previous run(simple model run w/o hyperparameter tuning) I think since it is showing previous run w/o hyperparameter tuning it has no data showing in HPARAMS tab. How can I delete previous log and load new one? (overwriting hyperparameter tuning model with 'logs/t0' works fine)

I write this code and run it correctly:

At the end, use these two commands and get your output:

%load_ext tensorboard
%tensorboard --logdir /logs/hyp_tune/

Full code:

# !pip install keras-tuner -q

import numpy as np
import keras_tuner
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

(x_train, y_train), (x_test, y_test) = (np.random.rand(1000,4), np.random.rand(1000)) , (np.random.rand(100,4), np.random.rand(100))



def build_model(hp):
    n_layers = 4
    n_features = x_train.shape[1]
    inputs = tf.keras.Input(shape=(n_features,))
    dense = tf.keras.layers.Dense(hp.Int("input_units", min_value=128, max_value=256, step=32),
                                activation=hp.Choice("activation", ['relu', 'tanh'])
                                )(inputs)
    dense = tf.keras.layers.Dropout(0.2)(dense)

    # num_layer as hyperparameter
    for i in range(hp.Int("dense_layer", 1, n_layers)):
        dense = tf.keras.layers.Dense(hp.Int(f"hidden_unit_{i}", 128, 256, 32),
                                    activation=hp.Choice("activation", ['relu', 'tanh'])
                                    )(dense)
        
    output = tf.keras.layers.Dense(1, activation='sigmoid')(dense)
    model = tf.keras.Model(inputs=inputs, outputs=output)

    lr = hp.Float("lr", min_value=1e-4, max_value=1e-1, sampling="log")
    model.compile(optimizer=tf.keras.optimizers.Adam(lr),
            loss=tf.keras.losses.BinaryCrossentropy(),
            metrics=["accuracy"])
    return model



hp = keras_tuner.HyperParameters()
model = build_model(hp)
model.summary()
tuner = keras_tuner.RandomSearch(
    build_model,
    max_trials=10,
    overwrite=True,
    objective="val_accuracy",
    # Set a directory to store the intermediate results.
    directory="/logs/hyp_tune/",
)

tensorboard_cb = tf.keras.callbacks.TensorBoard('/logs/hyp_tune/')
tuner.search(
    x_train,
    y_train,
    validation_data=(x_test, y_test),
    batch_size=512, 
    epochs=10,
    callbacks=[tensorboard_cb],
)

output:

%load_ext tensorboard
%tensorboard --logdir /logs/hyp_tune/

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM