I am using Keras tuner. For the simple following code:
import keras_tuner as kt
from tensorflow.keras.regularizers import l1, l2
from tensorflow.keras.models import Sequential
# x: 100 x 20
# y: 1 x 100
tuner = kt.Hyperband(
self.build_auto_encoder_model,
objective='val_loss',
max_epochs=30,
hyperband_iterations=20)
tuner.search(x[0:80], y[0:80], epochs=30, validation_data=(x[80:], y[80:]))
best_model = tuner.get_best_models(1)[0]
def build_auto_encoder_model(hp):
model = Sequential()
regulizer_rate = hp.Choice("regulizer_rate", [1e-1, 1e-2, 1e-3, 1e-4, 1e-5])
model.add(Dense(18, input_dim=20, activation='relu', kernel_regularizer=l1(regulizer_rate)))
model.add(Dense(12, activation='relu', kernel_regularizer=l1(regulizer_rate)))
model.add(Dense(10, activation='relu', kernel_regularizer=l1(regulizer_rate)))
model.compile(optimizer=Adam(1e-2), loss='mse')
I have already tried for the different number of Dense
layers, I am getting the following error:
Tensor's shape (20, 18) is not compatible with supplied shape (20, 15)
However, when totally, created a new project, it works. What was the reason?
The reason is because of some previous errors in the function code, an object has been created and loaded for any future trials, due to overwrite
variable of the turner is False
by default. Also, in the last version of the created object, the first layer was 15
that has been changed to 18
in your example.
A simple solution to resolve the problem (instead of creating a new project) is to make the overwrite
variable to True
to prevent reloading previously incompatible object with new changes, like the following:
# ...
tuner = kt.Hyperband(
self.build_auto_encoder_model,
objective='val_loss',
max_epochs=30,
hyperband_iterations=20,
overwrite = True) # here
# ...