Bird
Raised Fist0
TensorFlowml~20 mins

Custom callbacks in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Custom callbacks
Problem:You have trained a neural network on the MNIST dataset. The model trains well but you want to monitor the training process more closely by printing the loss and accuracy at the end of each epoch.
Current Metrics:Training accuracy: 98%, Validation accuracy: 97%, Loss: 0.05
Issue:The training process runs silently without detailed feedback after each epoch. You want to add custom messages during training to better understand model progress.
Your Task
Create a custom callback in TensorFlow that prints the loss and accuracy at the end of each epoch during training.
Use TensorFlow 2.x and Keras API.
Do not change the model architecture or dataset.
The callback must print training loss and accuracy after each epoch.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

# Load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# Build model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Define custom callback
class PrintMetricsCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        loss = logs.get('loss')
        accuracy = logs.get('accuracy')
        print(f'Epoch {epoch + 1}: loss = {loss:.4f}, accuracy = {accuracy:.4f}')

# Train model with callback
model.fit(x_train, y_train, epochs=5, batch_size=32, callbacks=[PrintMetricsCallback()])
Created a subclass of tf.keras.callbacks.Callback named PrintMetricsCallback.
Overrode the on_epoch_end method to print loss and accuracy after each epoch.
Added the custom callback to the model.fit call.
Results Interpretation

Before: Training ran silently without detailed feedback.

After: After each epoch, the console prints loss and accuracy, e.g., 'Epoch 1: loss = 0.2000, accuracy = 0.9400'.

Custom callbacks let you add your own code during training to monitor or modify the process. This helps you understand model behavior better.
Bonus Experiment
Modify the custom callback to stop training early if accuracy exceeds 98%.
💡 Hint
In on_epoch_end, check accuracy and call self.model.stop_training = True if condition met.

Practice

(1/5)
1. What is the main purpose of creating a custom callback in TensorFlow?
easy
A. To run your own code at specific points during model training
B. To change the model architecture automatically
C. To increase the dataset size during training
D. To convert the model to a different format

Solution

  1. Step 1: Understand what callbacks do

    Callbacks let you add custom actions during training, like logging or stopping early.
  2. Step 2: Identify the role of custom callbacks

    Custom callbacks let you run your own code at points like start/end of epochs or batches.
  3. Final Answer:

    To run your own code at specific points during model training -> Option A
  4. Quick Check:

    Custom callbacks = run code during training [OK]
Hint: Callbacks run code during training steps [OK]
Common Mistakes:
  • Thinking callbacks change model structure
  • Confusing callbacks with data augmentation
  • Assuming callbacks convert model formats
2. Which method should you override in a custom callback to execute code at the end of each training epoch?
easy
A. on_train_begin
B. on_epoch_end
C. on_batch_begin
D. on_train_end

Solution

  1. Step 1: Recall callback method names

    Common callback methods include on_train_begin, on_epoch_end, on_batch_begin, on_train_end.
  2. Step 2: Match method to event

    on_epoch_end runs code after each epoch finishes, which is what the question asks.
  3. Final Answer:

    on_epoch_end -> Option B
  4. Quick Check:

    End of epoch method = on_epoch_end [OK]
Hint: End of epoch callback method is on_epoch_end [OK]
Common Mistakes:
  • Using on_train_begin for epoch end
  • Confusing batch and epoch methods
  • Overriding on_train_end instead
3. What will be printed after training if this custom callback is used?
import tensorflow as tf
class MyCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        print(f"Epoch {epoch+1} ended with loss {logs['loss']:.2f}")

model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(optimizer='sgd', loss='mse')
model.fit([[1.0]], [[2.0]], epochs=2, callbacks=[MyCallback()])
medium
A. Error because logs is None
B. Epoch 0 ended with loss X\nEpoch 1 ended with loss Y
C. No output printed during training
D. Epoch 1 ended with loss X\nEpoch 2 ended with loss Y (X and Y are numbers)

Solution

  1. Step 1: Understand on_epoch_end behavior

    The method prints epoch number +1 and loss from logs after each epoch.
  2. Step 2: Check training epochs and output

    Training runs 2 epochs, so prints for epoch 1 and 2 with loss values (numbers).
  3. Final Answer:

    Epoch 1 ended with loss X\nEpoch 2 ended with loss Y (X and Y are numbers) -> Option D
  4. Quick Check:

    Prints after each epoch with loss [OK]
Hint: Epoch count starts at 0, print adds 1 [OK]
Common Mistakes:
  • Assuming epoch numbering starts at 1 internally
  • Expecting no print output
  • Thinking logs is None causing error
4. Identify the error in this custom callback code that tries to stop training when loss is below 0.1:
class StopCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        if logs['loss'] < 0.1:
            self.model.stop_training = True

model.fit(x_train, y_train, epochs=10, callbacks=[StopCallback()])
medium
A. on_epoch_end method must return a value
B. stop_training should be called as a method
C. logs dictionary might be None causing KeyError
D. Callback class must inherit from tf.keras.Model

Solution

  1. Step 1: Check logs usage

    logs can be None if no metrics are passed, so logs['loss'] may cause KeyError.
  2. Step 2: Confirm correct way to stop training

    Setting self.model.stop_training = True is correct; no method call needed.
  3. Final Answer:

    logs dictionary might be None causing KeyError -> Option C
  4. Quick Check:

    Check logs is not None before accessing [OK]
Hint: Always check logs is not None before using keys [OK]
Common Mistakes:
  • Calling stop_training as a method
  • Expecting on_epoch_end to return a value
  • Inheriting from wrong base class
5. You want to save the model weights only when validation accuracy improves during training. Which custom callback method and logic should you implement?
hard
A. Override on_epoch_end, check logs['val_accuracy'], save weights if improved
B. Override on_train_begin, save weights before training starts
C. Override on_batch_end, save weights after every batch
D. Override on_train_end, save weights after training finishes

Solution

  1. Step 1: Identify when to check validation accuracy

    Validation accuracy is updated at epoch end, so on_epoch_end is the right method.
  2. Step 2: Implement logic to save weights only if val_accuracy improves

    Inside on_epoch_end, compare current val_accuracy with previous best and save if better.
  3. Final Answer:

    Override on_epoch_end, check logs['val_accuracy'], save weights if improved -> Option A
  4. Quick Check:

    Save on epoch end if val_accuracy improves [OK]
Hint: Use on_epoch_end to monitor validation metrics [OK]
Common Mistakes:
  • Saving weights too often (every batch)
  • Saving only before or after all training
  • Not checking if accuracy improved before saving
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard