tf.config.experimental.set_memory_growth
Stay organized with collections
Save and categorize content based on your preferences.
Set if memory growth should be enabled for a PhysicalDevice
.
tf.config.experimental.set_memory_growth(
device, enable
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
If memory growth is enabled for a PhysicalDevice
, the runtime initialization
will not allocate all memory on the device. Memory growth cannot be configured
on a PhysicalDevice
with virtual devices configured.
For example:
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
# Invalid device or cannot modify virtual devices once initialized.
pass
Args |
device
|
PhysicalDevice to configure
|
enable
|
(Boolean) Whether to enable or disable memory growth
|
Raises |
ValueError
|
Invalid PhysicalDevice specified.
|
RuntimeError
|
Runtime is already initialized.
|
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-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.config.experimental.set_memory_growth\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/config.py#L729-L754) |\n\nSet if memory growth should be enabled for a `PhysicalDevice`.\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.config.experimental.set_memory_growth`](https://www.tensorflow.org/api_docs/python/tf/config/experimental/set_memory_growth)\n\n\u003cbr /\u003e\n\n tf.config.experimental.set_memory_growth(\n device, enable\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Use a GPU](https://www.tensorflow.org/guide/gpu) - [Import a JAX model using JAX2TF](https://www.tensorflow.org/guide/jax2tf) - [Using the SavedModel format](https://www.tensorflow.org/guide/saved_model) | - [Universal Sentence Encoder SentEval demo](https://www.tensorflow.org/hub/tutorials/senteval_for_universal_sentence_encoder_cmlm) - [Bayesian Gaussian Mixture Model and Hamiltonian MCMC](https://www.tensorflow.org/probability/examples/Bayesian_Gaussian_Mixture_Model) - [Modeling COVID-19 spread in Europe and the effect of interventions](https://www.tensorflow.org/probability/examples/Estimating_COVID_19_in_11_European_countries) |\n\nIf memory growth is enabled for a `PhysicalDevice`, the runtime initialization\nwill not allocate all memory on the device. Memory growth cannot be configured\non a `PhysicalDevice` with virtual devices configured.\n\n#### For example:\n\n physical_devices = tf.config.list_physical_devices('GPU')\n try:\n tf.config.experimental.set_memory_growth(physical_devices[0], True)\n except:\n # Invalid device or cannot modify virtual devices once initialized.\n pass\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|------------------------------------------------------|\n| `device` | `PhysicalDevice` to configure |\n| `enable` | (Boolean) Whether to enable or disable memory growth |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|-------------------------------------|\n| `ValueError` | Invalid `PhysicalDevice` specified. |\n| `RuntimeError` | Runtime is already initialized. |\n\n\u003cbr /\u003e"]]