Keras early stopping callback error, val_loss metric not available
I up-voted the previous answer as it gave me the insight to verify the data and inputs to the fit_generator
function and find out what the root cause of the issue actually was. In summary, in cases where my dataset was small, I calculated validation_steps
and steps_per_epoch
which turned out to be zero (0) which caused the error.
I suppose the better longer-term answer, perhaps for the Keras team, is to cause an error/exception in fit_generator
when these values are zero, which would probably lead to a better understanding about how to address this issue.
The error occurs to us because we forgot to set validation_data in fit() method, while used 'callbacks': [keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
Code causing error is:
self.model.fit(
x=x_train,
y=y_train,
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
verbose=True)
Adding validation_data=(self.x_validate, self.y_validate),
in fit() fixed:
self.model.fit(
x=x_train,
y=y_train,
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
validation_data=(x_validate, y_validate),
verbose=True)
If the error only occurs when you use smaller datasets, you're very likely using datasets small enough to not have a single sample in the validation set.
Thus it cannot calculate a validation loss.