Skip to content

Instantly share code, notes, and snippets.

@Wapiti08
Last active May 6, 2020 08:38
Show Gist options
  • Save Wapiti08/b486162693b39de3d42af1183d14ffa2 to your computer and use it in GitHub Desktop.
Save Wapiti08/b486162693b39de3d42af1183d14ffa2 to your computer and use it in GitHub Desktop.
callback_usage on early stop
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# this is the class can stop the epochs when the condition is acceptable
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch,logs={}):
if (logs.get('acc')>=0.99):
print("the result has reached the 99% so cancelling the epoch")
self.model.stop_learning = True
x_train,x_test = x_train/255.0, x_test/255.0
callbacks = myCallback()
model = tf.keras.models.Sequential([
# flatten means to change the shape of picture
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation = tf.nn.relu),
tf.keras.layers.Dense(10, activation = tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_test,y_test,epochs = 10,callbacks=[callbacks])
================================
import keras
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch,logs={}):
if (logs.get('acc')>=0.99):
print("the result has reached the 99% so cancelling the epoch")
self.model.stop_training = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment