I am training my LSTM model using keras tuner. I am getting an error
Expected the return value of HyperModel.fit() to be a single float when objective
is left unspecified. Recevied return value: <tensorflow.python.keras.callbacks.History object at 0x7fa3e80be710> of type <class 'tensorflow.python.keras.callbacks.History'>.
I am not familiar with this error and also searched a bit. I am also not that much familiar with keras tuner.
My code is
x_test = x_test[FeaturesSelected]
totalColumns = len(FeaturesSelected)
callback = EarlyStopping(monitor='val_loss', patience=3)
def build_model(hp):
model=keras.Sequential()
model.add(layers.Flatten(input_shape=(totalColumns,1)))
for i in range(hp.Int('layers',2,8)):
model.add(layers.Dense(units=hp.Int('units_'+str(i),50,100,step = 10),
activation=hp.Choice('act_'+str(i),['relu','sigmoid'])))
model.add(layers.Dense(10,activation='softmax'))
model.compile(optimizer =keras.optimizers.Adam(hp.Choice('learning_rate',values=[1e-2,1e-4])),
loss='mean_absolute_error')
return model
tuner = RandomSearch(build_model,max_trials = 20,executions_per_trial=5)
tuner.search(x_train,y_train,epochs = 10,validation_data=(x_test,y_test),callbacks=[callback])
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
best_model = tuner.hypermodel.build(best_hps)
history = best_model.fit(img_train, label_train, epochs=50, validation_split=0.2)
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
hypermodel = tuner.hypermodel.build(best_hps)
hypermodel.fit(x_train,y_train, epochs=best_epoch, validation_split=0.2)
test_pred = hypermodel.predict(x_test)