Keras early stopping callback error, val_loss metric not available
This error could be happen when you training a model. First you have to make sure that val_loss or val_accuracy is filled on the callback params.
model.fit( callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)], x=x_train, y=y_train, validation_data=(x_validate, y_validate), verbose=True)
When this error happen, you can solve it by quit and stop the jupyter notebook. Don’t forget to save the ipynb file first. And then deleting the .ipynb_checkpoints folder. After that start the jupyter notebook again. But if it still doesn’t work, you can do the alternative solution below to solve the problem. According to the stackoverflow, the answer from Daniel said that 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. To resolve this problem, you can increase the data or you can also increase the train times and split the train set in 80:20. The error occurs can possibly happen because you forgot to set validation_data in fit() method, while used
callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)],
Adding validation_data=(x_validate, y_validate) or validation_data=(self.x_validate, self.y_validate) in fit() will fixed it. The code below is the example where to put the code. x_validate and y_validate are a variable name you can adjust it using your variable name.
model.fit( callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)], x=x_train, y=y_train, validation_data=(self.x_validate, self.y_validate), verbose=True)
Or
model.fit( callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)], x=x_train, y=y_train, validation_data=(self.x_validate, self.y_validate), verbose=True)
validation_data is receiving a data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. Thus, note the fact that the validation loss of data provided using validation_split or validation_data is not affected by regularization layers like noise and dropout. validation_data will override validation_split. validation_data could be:
- A tuple (x_val, y_val) of Numpy arrays or tensors
- A tuple (x_val, y_val, val_sample_weights) of NumPy arrays
- A tf.data.Dataset
- A Python generator or keras.utils.Sequence returning (inputs, targets) or (inputs, targets, sample_weights)