This is the model I am attempting to use:
import tensorflow as tf
from keras.utils.np_utils import to_categorical
import keras_tuner as kt
#Convert y to categorical
y_train3=pd.Series(y_train3)
y_train3= to_categorical(y_train3)
y_test3=pd.Series(y_test3)
y_test3=to_categorical(y_test3)
#Define Neural Network Model
def model_builder(hp):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,)))
hp_activation = hp.Choice('activation', values=['relu', 'tanh'])
hp_layer_1 = hp.Int('layer_1', min_value=1, max_value=1000, step=100)
hp_layer_2 = hp.Int('layer_2', min_value=1, max_value=1000, step=100)
hp_layer_3 = hp.Int('layer_3', min_value=1, max_value=1000, step=100)#<<<<<<<<<< I ADDED THIS PART
hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
model.add(tf.keras.layers.Dense(units=hp_layer_1, activation=hp_activation))
model.add(tf.keras.layers.Dense(units=hp_layer_2, activation=hp_activation))
model.add(tf.keras.layers.Dense(units=hp_layer_3, activation=hp_activation))#<<<<<<<<<< I ADDED THIS PART
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss="categorical_crossentropy",
metrics=['accuracy'])
return model
tuner = kt.Hyperband(model_builder,
objective='val_accuracy',
max_epochs=100,
factor=3,
directory='dir',
project_name='x')
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
tuner.search(X_train3, y_train3, epochs=10, validation_split=0.2, callbacks=[stop_early])
From my understanding, the code without the sections that I have added is a Neural Network with only 2 hidden layers, but I wish to increase it to 3.
I added the commented sections (see right of code) in an attempt to increase the number of layers to 3, but I get a ValueError saying "Received incompatible tensor with shape (10,) when attempting to restore variable with shape (701,)". This happens when running tuner.search
.
The code runs fine without those added comments. Clearly adding those lines is breaking something, but I do not understand what, could someone enlighten me please? Thanks!