tf.keras.backend.clear_session
Stay organized with collections
Save and categorize content based on your preferences.
Destroys the current TF graph and session, and creates a new one.
tf.keras.backend.clear_session()
Calling clear_session() releases the global graph state that Keras is
holding on to; resets the counters used for naming layers and
variables in Keras; and resets the learning phase. This helps avoid clutter
from old models and layers, especially when memory is limited, and a
common use-case for clear_session is releasing memory when building models
and layers in a loop.
import tensorflow as tf
layers = [tf.keras.layers.Dense(10) for _ in range(10)]
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)
dense_10
tf.keras.backend.set_learning_phase(1)
print(tf.keras.backend.learning_phase())
1
tf.keras.backend.clear_session()
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)
dense
print(tf.keras.backend.learning_phase())
0
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.backend.clear_session\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/backend/clear_session) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/keras/backend.py#L241-L284) |\n\nDestroys the current TF graph and session, and creates a new one.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.backend.clear_session`](/api_docs/python/tf/keras/backend/clear_session)\n\n\u003cbr /\u003e\n\n tf.keras.backend.clear_session()\n\nCalling clear_session() releases the global graph state that Keras is\nholding on to; resets the counters used for naming layers and\nvariables in Keras; and resets the learning phase. This helps avoid clutter\nfrom old models and layers, especially when memory is limited, and a\ncommon use-case for clear_session is releasing memory when building models\nand layers in a loop. \n\n import tensorflow as tf\n layers = [tf.keras.layers.Dense(10) for _ in range(10)]\n new_layer = tf.keras.layers.Dense(10)\n print(new_layer.name)\n dense_10\n tf.keras.backend.set_learning_phase(1)\n print(tf.keras.backend.learning_phase())\n 1\n tf.keras.backend.clear_session()\n new_layer = tf.keras.layers.Dense(10)\n print(new_layer.name)\n dense\n print(tf.keras.backend.learning_phase())\n 0"]]