tf.config.set_visible_devices
Stay organized with collections
Save and categorize content based on your preferences.
Set the list of visible devices.
tf.config.set_visible_devices(
devices, device_type=None
)
Used in the notebooks
Specifies which PhysicalDevice
objects are visible to the runtime.
TensorFlow will only allocate memory and place operations on visible
physical devices, as otherwise no LogicalDevice
will be created on them.
By default all discovered devices are marked as visible.
The following example demonstrates disabling the first GPU on the machine.
physical_devices = tf.config.list_physical_devices('GPU')
try:
# Disable first GPU
tf.config.set_visible_devices(physical_devices[1:], 'GPU')
logical_devices = tf.config.list_logical_devices('GPU')
# Logical device was not created for first GPU
assert len(logical_devices) == len(physical_devices) - 1
except:
# Invalid device or cannot modify virtual devices once initialized.
pass
Args |
devices
|
List of PhysicalDevice s to make visible
|
device_type
|
(optional) Only configure devices matching this device type.
For example "CPU" or "GPU". Other devices will be left unaltered.
|
Raises |
ValueError
|
If argument validation fails.
|
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.set_visible_devices\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#L533-L566) |\n\nSet the list of visible devices.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.config.experimental.set_visible_devices`](https://www.tensorflow.org/api_docs/python/tf/config/set_visible_devices)\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_visible_devices`](https://www.tensorflow.org/api_docs/python/tf/config/set_visible_devices), [`tf.compat.v1.config.set_visible_devices`](https://www.tensorflow.org/api_docs/python/tf/config/set_visible_devices)\n\n\u003cbr /\u003e\n\n tf.config.set_visible_devices(\n devices, device_type=None\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|-----------------------------------------------------|\n| - [Use a GPU](https://www.tensorflow.org/guide/gpu) |\n\nSpecifies which `PhysicalDevice` objects are visible to the runtime.\nTensorFlow will only allocate memory and place operations on visible\nphysical devices, as otherwise no `LogicalDevice` will be created on them.\nBy default all discovered devices are marked as visible.\n\nThe following example demonstrates disabling the first GPU on the machine. \n\n physical_devices = tf.config.list_physical_devices('GPU')\n try:\n # Disable first GPU\n tf.config.set_visible_devices(physical_devices[1:], 'GPU')\n logical_devices = tf.config.list_logical_devices('GPU')\n # Logical device was not created for first GPU\n assert len(logical_devices) == len(physical_devices) - 1\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| `devices` | List of `PhysicalDevice`s to make visible |\n| `device_type` | (optional) Only configure devices matching this device type. For example \"CPU\" or \"GPU\". Other devices will be left unaltered. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|---------------------------------|\n| `ValueError` | If argument validation fails. |\n| `RuntimeError` | Runtime is already initialized. |\n\n\u003cbr /\u003e"]]