TensorFlow
Callbacks
Monitor and control training with callbacks.
By EZ4Code Team
callbackstraining
Code
import tensorflow as tf
callbacks = [
tf.keras.callbacks.EarlyStopping(
monitor="val_loss", patience=3, restore_best_weights=True),
tf.keras.callbacks.ModelCheckpoint(
"best.keras", monitor="val_loss", save_best_only=True),
tf.keras.callbacks.ReduceLROnPlateau(
monitor="val_loss", factor=0.5, patience=2, min_lr=1e-6),
tf.keras.callbacks.TensorBoard(log_dir="./logs", histogram_freq=1),
tf.keras.callbacks.CSVLogger("training.csv"),
]
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation="relu", input_shape=(10,)),
tf.keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
# Custom callback
class PrintEpoch(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
print(f"epoch {epoch}: {logs}")
# model.fit(x_train, y_train, validation_split=0.2,
# epochs=50, callbacks=callbacks + [PrintEpoch()])Explanation
Callbacks hook into the training lifecycle to stop early, checkpoint the best weights, reduce the learning rate, or log to TensorBoard and CSV. EarlyStopping with restore_best_weights reverts to the best epoch instead of the last one. Subclassing Callback lets you run custom logic at any phase of training.
More TensorFlow Snippets
Tensor Basics
Create and operate on TensorFlow tensors.
Keras Model
Build models with Sequential and the functional API.
Layers
Use core layers and build a custom one.
Compile and Train
Compile, fit, and evaluate a Keras model.
Custom Training Loop
Step through batches with GradientTape.
Save and Load
Persist models in SavedModel and Keras formats.