Tensorflow 2.0 Accessing a batch's tensors from a callback
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Keras callbacks are great for logging, checkpointing, and early stopping, but they do not automatically hand you the full input batch during training. If you need access to batch tensors in TensorFlow 2, the clean solution is usually to expose the specific values you need from train_step and read them inside the callback.
Why callbacks do not receive x and y directly
Methods such as on_train_batch_begin and on_train_batch_end receive the batch index and a logs dictionary. That dictionary contains metrics and loss values, not the raw tensors that were fed into the model.
This design keeps callbacks generic, but it means code like "print the mean of the current input batch" cannot be done from a callback alone. You need a bridge between the training step and the callback.
A practical pattern: custom train_step
The standard pattern is:
- subclass
tf.keras.Model - override
train_step - compute the batch-level values you care about
- return them in
logs - read them in the callback
Here is a minimal runnable example:
The callback still does not receive raw batch tensors directly, but it receives exactly the batch-derived information you chose to publish.
If you really need the full batch tensors
Sometimes summaries are not enough. You may want to inspect misclassified samples or save the actual batch for debugging. In that case, you can temporarily store the tensors on the model during train_step:
Then a callback can read self.model.last_batch_x. This works, but it should be used carefully. Keeping full tensors around can increase memory use and may create confusion when running distributed training or graph-compiled code.
For long-running jobs, it is usually better to log compact summaries, indices, or a few sampled examples instead of storing whole batches.
Alternative approaches
There are other options, but they are usually heavier:
- write a custom training loop with
tf.GradientTape - wrap the dataset so each batch carries extra metadata
- emit debugging information with
tf.printinside the model
A custom training loop gives total control and is the best fit when callbacks start fighting the design of your experiment.
Common Pitfalls
The most common mistake is expecting logs to contain x and y automatically. It will not.
Another problem is returning objects that are too large or not easily serializable in logs. Keep logs small and metric-like.
People also forget that tensors in callbacks may need conversion for display. Printing float(logs["loss"]) is clearer than dumping a raw TensorFlow tensor object.
Finally, if you override train_step, make sure you still update metrics and apply gradients correctly. A custom hook is not useful if it silently changes training behavior.
Summary
- Keras callbacks do not automatically expose raw batch tensors.
- The usual fix is to override
train_stepand return batch-derived values inlogs. - Full-batch access is possible by storing tensors on the model, but it increases complexity and memory usage.
- Use compact summaries unless you truly need the complete batch.
- If callback-based inspection feels awkward, switch to a custom training loop for full control.
Related reading
- tensorflow 2.0 An op outside of the function building code is being passed
- Tensorflow 2.0 Custom loss function with multiple inputs
- TensorFlow 2.0 dataset.__iter__ is only supported when eager execution is enabled
- Tensorflow 2.0 dataset and dataloader
- TensorFlow 2.0 do you need a tf.function decorator on top of each function?
- Tensorflow 2.0 doesn't compute the gradient
- Tensorflow 2.0 How to change the output signature while using tf.saved_model
- TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.