tf.keras.backend.clear_session
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
Resets all state generated by Keras.
View aliases
Compat aliases for migration
See
Migration guide for
more details.
tf.compat.v1.keras.backend.clear_session
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 2021-02-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2021-02-18 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.4.0/tensorflow/python/keras/backend.py#L259-L318) |\n\nResets all state generated by Keras.\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`](https://www.tensorflow.org/api_docs/python/tf/keras/backend/clear_session)\n\n\u003cbr /\u003e\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 clutter\nfrom 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([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([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"]]