前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tensorflow API(一)

Tensorflow API(一)

原创
作者头像
XianxinMao
修改2021-08-09 11:02:49
3730
修改2021-08-09 11:02:49
举报
文章被收录于专栏:深度学习框架深度学习框架

tf.config.list_physical_devices

Return a list of physical devices visible to the host runtime.

Physical devices are hardware devices present on the host machine. By default all discovered CPU and GPU devices are considered visible.

This API allows querying the physical hardware resources prior to runtime initialization. Thus, giving an opportunity to call any additional configuration APIs. This is in contrast to tf.config.list_logical_devices, which triggers runtime initialization in order to list the configured devices.

The following example lists the number of visible GPUs on the host.

代码语言:javascript
复制
physical_devices = tf.config.list_physical_devices('GPU')
print("Num GPUs:", len(physical_devices))

However, the number of GPUs available to the runtime may change during runtime initialization due to marking certain devices as not visible or configuring multiple logical devices.

tf.config.set_visible_devices

Set the list of visible devices.

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

代码语言:javascript
复制
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

tf.config.list_logical_devices

Return a list of logical devices created by runtime

Logical devices may correspond to physical devices or remote devices in the cluster. Operations and tensors may be placed on these devices by using the name of the tf.config.LogicalDevice.

Calling tf.config.list_logical_devices triggers the runtime to configure any tf.config.PhysicalDevice visible to the runtime, thereby preventing further configuration. To avoid runtime initialization, call tf.config.list_physical_devices instead.

代码语言:javascript
复制
logical_devices = tf.config.list_logical_devices('GPU')
if len(logical_devices) > 0:
  # Allocate on GPU:0
  with tf.device(logical_devices[0].name):
    one = tf.constant(1)
  # Allocate on GPU:1
  with tf.device(logical_devices[1].name):
    two = tf.constant(2)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • tf.config.list_physical_devices
  • tf.config.set_visible_devices
  • tf.config.list_logical_devices
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档