Home > Enterprise >  Missing val_loss and val_accuracy after overriding the fit function
Missing val_loss and val_accuracy after overriding the fit function

Time:05-29

Following Keras Tuner's documentation for implementing a custom objective function, the fit function of my model's class is as below:

class HyperAE(kt.HyperModel):
  def build(self, hp):
      ...
  
  def fit(self, hp, model, x, y, validation_data, **kwargs):
      model.fit(x, y, **kwargs)
      x_val, y_val = validation_data
      y_pred = model.predict(x_val)
      return {
        "metric_1": -np.mean(np.abs(y_pred - y_val)),
        "metric_2": np.mean(np.square(y_pred - y_val)),
      }

When running the tuner with this model, I can't see val_loss and other validation metrics printed as before. How can I make them to get printed again?

CodePudding user response:

This happened because validation_data is not passed to the actual model.fit function call. The problem can be fixed as below:

class HyperAE(kt.HyperModel):
  def build(self, hp):
      ...
  
  def fit(self, hp, model, x, y, validation_data, **kwargs):
      model.fit(x=x, y=y, validation_data=validation_data, **kwargs)
      x_val, y_val = validation_data
      y_pred = model.predict(x_val)
      return {
        "metric_1": -np.mean(np.abs(y_pred - y_val)),
        "metric_2": np.mean(np.square(y_pred - y_val)),
      }
  • Related