tf.keras.backend.clear_session
Stay organized with collections
Save and categorize content based on your preferences.
Resets all state generated by Keras.
tf.keras.backend.clear_session()
Keras manages a global state, which it uses to implement the Functional
model-building API and to uniquify autogenerated layer names.
If you are creating many models in a loop, this global state will consume
an increasing amount of memory over time, and you may want to clear it.
Calling clear_session()
releases the global state: this helps avoid
clutter from old models and layers, especially when memory is limited.
Example 1: calling clear_session()
when creating models in a loop
for _ in range(100):
# Without `clear_session()`, each iteration of this loop will
# slightly increase the size of the global state managed by Keras
model = tf.keras.Sequential([
tf.keras.layers.Dense(10) for _ in range(10)])
for _ in range(100):
# With `clear_session()` called at the beginning,
# Keras starts with a blank state at each iteration
# and memory consumption is constant over time.
tf.keras.backend.clear_session()
model = tf.keras.Sequential([
tf.keras.layers.Dense(10) for _ in range(10)])
Example 2: resetting the layer name generation counter
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
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. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.keras.backend.clear_session\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.14.0/keras/backend.py#L226-L297) |\n\nResets all state generated by Keras. \n\n tf.keras.backend.clear_session()\n\nKeras manages a global state, which it uses to implement the Functional\nmodel-building API and to uniquify autogenerated layer names.\n\nIf you are creating many models in a loop, this global state will consume\nan increasing amount of memory over time, and you may want to clear it.\nCalling `clear_session()` releases the global state: this helps avoid\nclutter from old models and layers, especially when memory is limited.\n\nExample 1: calling `clear_session()` when creating models in a loop \n\n for _ in range(100):\n # Without `clear_session()`, each iteration of this loop will\n # slightly increase the size of the global state managed by Keras\n model = tf.keras.Sequential([\n tf.keras.layers.Dense(10) for _ in range(10)])\n\n for _ in range(100):\n # With `clear_session()` called at the beginning,\n # Keras starts with a blank state at each iteration\n # and memory consumption is constant over time.\n tf.keras.backend.clear_session()\n model = tf.keras.Sequential([\n tf.keras.layers.Dense(10) for _ in range(10)])\n\nExample 2: resetting the layer name generation counter \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"]]