I'm using keras tuner for hyperparameter tuning my sequential neural network in keras. My aim is to let keras tuner do a lot of trials and then save all statistics - loss, hyperparameters, trial numbers and epochs - to a file, so I can plot them myself and get a better overview over how keras tuner conducted the tests. Note that this is a regression, so I'm using mean squared error (mse) as a loss function, not accuracy. Here's an example of my network and the setup of keras tuner (based on the "Getting started" tutorial of keras tuner):
import numpy as np
import keras
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import layers
import keras_tuner
from sklearn.model_selection import train_test_split
from tensorboard.backend.event_processing import event_accumulator
# generate random data
random_data = np.random.rand(100,4)
x_data = random_data[:, :-1]
y_data = random_data[:, -1:]
input_dimensions = len(x_data[0, :])
# split data into train and test
x_train, x_eval, y_train, y_eval = train_test_split(x_data, y_data, test_size=0.3, random_state=101)
# create keras tuner and model
def build_model(hp):
model = keras.Sequential()
# test number of layers, number of neurons in each layer and activation function
for i in range(hp.Int("num_layers", 2, 4)):
model.add(layers.Dense(
units=hp.Int(f"units_{i}", min_value=32, max_value=1024, step=32),
activation=hp.Choice("activation", ["relu", "sigmoid"])))
model.add(layers.Dense(1, activation="linear"))
model.compile(optimizer=Adam(learning_rate=0.0005),
loss='mse')
return model
build_model(keras_tuner.HyperParameters())
tuner = keras_tuner.RandomSearch(
hypermodel=build_model,
objective=keras_tuner.Objective('loss', direction="min"),
max_trials=5,
executions_per_trial=3,
overwrite=True,
project_name="keras_tuner_test")
For extracting the statistics I'm using the Tensorboard callback method (just to be clear: I don't want to actually use Tensorboard. I want only the data and then decide for myself how to display it) with the following code based on this link or this link:
sample_log_directory = <path to directory>
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=sample_log_directory)
tuner.search(x_train, y_train, epochs=3, validation_data=(x_eval, y_eval), callbacks=[tensorboard_callback])
def extract_history(best_trial):
acc = []
val_acc = []
loss = []
val_loss = []
for set_data in ['train', 'validation']:
if set_data == 'train':
print(sample_log_directory + best_trial + '/execution0/' + set_data)
ea = event_accumulator.EventAccumulator(sample_log_directory + best_trial + '/execution0/' + set_data)
ea.Reload()
for i in range(len(ea.Scalars('epoch_loss'))):
acc.append(ea.Scalars('epoch_acc')[i][2])
loss.append(ea.Scalars('epoch_loss')[i][2])
#lr.append(ea.Scalars('epoch_lr')[i][2])
if set_data == 'validation':
ea = event_accumulator.EventAccumulator(sample_log_directory + best_trial + '/execution0/' + set_data)
ea.Reload()
for i in range(len(ea.Scalars('epoch_loss'))):
val_acc.append(ea.Scalars('epoch_acc')[i][2])
val_loss.append(ea.Scalars('epoch_loss')[i][2])
return acc, val_acc, loss, val_loss
best_trial = tuner.oracle.get_best_trials()[0].trial_id
acc, val_acc, loss, val_loss = extract_history(best_trial)
Unfortunately, when doing that I get the error message KeyError: 'Key epoch_loss was not found in Reservoir'
. It seems like with e.g. ea.Scalars('epoch_acc') I merely have the wrong key (as I said at the beginning, this is a regression and therefore not using accuracy). How can I find out which keys are correct? I tried inspecting ea.scalars.Keys()
which results in an empty list, although it seems that ea.scalars
is not the same as ea.Scalars
.