Useful techniques and methods that you can use while you are developing machine learning models.
DESIRED_ACCURACY = 0.98
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs = {}):
if(logs.get('acc') > DESIRED_ACCURACY):
print(f"\nReached {DESIRED_ACCURACY * 100}% accuracy so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
We can make use of tf.keras.callbacks.Callback to stop training if it reaches a desired accuracy (or loss). Simply create a myCallback
class and call it. After that, don't forget to add [callbacks]
.
model.fit(training_data, training_labels, epochs=100, callbacks=[callbacks])
If you would like to use loss
instead of accuracy
, use logs.get('loss')
.