Does calling tf.keras.backend.clear_session() fully release memory when using subclassed Models with custom training loops?
0 reputation · 04 Sept 2021, 13:54 UTC
Goal: Verify whether invoking tf.keras.backend.clear_session() after each training iteration completely frees memory when the model is a subclassed Model that implements a custom train_step.
Constraint: The Keras FAQ recommends clear_session() to counter the gradual RAM growth seen with repeated fit() calls on Sequential models, but anecdotal reports suggest the method may not reclaim all resources for subclassed models, especially when callbacks or optimizer variables retain hidden references, and the effect may vary between TensorFlow versions and execution modes (eager vs. tf.function graph).
Questions: Does clear_session() release all references to model weights, optimizer variables, and custom training‑loop state in subclassed models? Are additional steps required, such as removing callbacks or resetting optimizer states, to achieve full memory reclamation? Does running the loop under tf.function graph mode change the outcome compared with eager execution?