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(
free_memory=True
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
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.
Args |
free_memory
|
Whether to call Python garbage collection.
It's usually a good practice to call it to make sure
memory used by deleted objects is immediately freed.
However, it may take a few seconds to execute, so
when using clear_session() in a short loop,
you may want to skip it.
|
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 = keras.Sequential([
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.
keras.backend.clear_session()
model = keras.Sequential([
keras.layers.Dense(10) for _ in range(10)])
Example 2: resetting the layer name generation counter
layers = [keras.layers.Dense(10) for _ in range(10)]
new_layer = keras.layers.Dense(10)
print(new_layer.name)
dense_10
keras.backend.clear_session()
new_layer = 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 2024-06-07 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 2024-06-07 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/v3.3.3/keras/src/backend/common/global_state.py#L24-L98) |\n\nResets all state generated by Keras.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.utils.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 free_memory=True\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Estimators](https://www.tensorflow.org/guide/estimator) | - [Transfer learning for video classification with MoViNet](https://www.tensorflow.org/tutorials/video/transfer_learning_with_movinet) - [Classifying CIFAR-10 with XLA](https://www.tensorflow.org/xla/tf2xla/tutorials/autoclustering_xla) - [Graph regularization for sentiment classification using synthesized graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb) - [Graph regularization for document classification using natural graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_mlp_cora) |\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\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `free_memory` | Whether to call Python garbage collection. It's usually a good practice to call it to make sure memory used by deleted objects is immediately freed. However, it may take a few seconds to execute, so when using `clear_session()` in a short loop, you may want to skip it. |\n\n\u003cbr /\u003e\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 = keras.Sequential([\n 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 keras.backend.clear_session()\n model = keras.Sequential([\n keras.layers.Dense(10) for _ in range(10)])\n\nExample 2: resetting the layer name generation counter \n\n layers = [keras.layers.Dense(10) for _ in range(10)]\n new_layer = keras.layers.Dense(10)\n print(new_layer.name)\n dense_10\n keras.backend.clear_session()\n new_layer = keras.layers.Dense(10)\n print(new_layer.name)\n dense"]]