Bird
Raised Fist0
TensorFlowml~12 mins

Custom callbacks in TensorFlow - Model Pipeline Trace

Choose your learning style10 modes available
Model Pipeline - Custom callbacks

This pipeline shows how a neural network trains on data while using a custom callback to monitor and print training progress in a friendly way.

Data Flow - 4 Stages
1Data loading
1000 rows x 20 columns→Load dataset with 20 features and 1 label column→1000 rows x 20 columns (features), 1000 rows x 1 column (labels)
Feature row: [0.5, 1.2, ..., 0.3], Label: 1
↓
2Data preprocessing
1000 rows x 20 columns→Normalize features to range 0-1→1000 rows x 20 columns
Normalized feature row: [0.02, 0.48, ..., 0.15]
↓
3Train/test split
1000 rows x 20 columns→Split data into 800 training and 200 testing rows→800 rows x 20 columns (train), 200 rows x 20 columns (test)
Train feature row: [0.1, 0.3, ..., 0.4], Test feature row: [0.2, 0.5, ..., 0.1]
↓
4Model training with custom callback
800 rows x 20 columns→Train neural network with callback printing loss every epoch→Trained model with updated weights
Epoch 1: loss=0.65, accuracy=0.60
Training Trace - Epoch by Epoch
Loss
0.7 |*       
0.6 | *      
0.5 |  *     
0.4 |   *    
0.3 |    *   
    +---------
     1 2 3 4 5 Epochs
EpochLoss ↓Accuracy ↑Observation
10.650.60Model starts learning, loss is high, accuracy moderate
20.500.72Loss decreases, accuracy improves
30.400.80Model continues to improve
40.350.85Loss lowers further, accuracy rises
50.300.88Training converging well
Prediction Trace - 3 Layers
Layer 1: Input layer
Layer 2: Hidden layer with ReLU
Layer 3: Output layer with sigmoid
Model Quiz - 3 Questions
Test your understanding
What does the custom callback do during training?
AChanges model weights manually
BAdds more data to training set
CPrints loss and accuracy after each epoch
DStops training after first epoch
Key Insight
Custom callbacks let us watch training closely and add helpful actions like printing progress, which makes learning easier to understand and control.

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