Python中使用TensorFlow库中的list_local_devices()方法获取可用设备列表的步骤
发布时间:2024-01-12 06:28:41
要获取TensorFlow库中可用设备列表,可以使用tf.config.list_local_devices()方法。
步骤如下:
1. 导入TensorFlow库:
import tensorflow as tf
2. 使用tf.config.list_local_devices()方法获取设备列表:
devices = tf.config.list_local_devices()
3. 遍历设备列表,打印每个设备的名称和设备类型:
for device in devices:
print("Device name:", device.name)
print("Device type:", device.device_type)
4. 完整的示例代码如下:
import tensorflow as tf
devices = tf.config.list_local_devices()
for device in devices:
print("Device name:", device.name)
print("Device type:", device.device_type)
运行以上代码,输出将类似于以下内容:
Device name: /physical_device:CPU:0 Device type: CPU Device name: /physical_device:GPU:0 Device type: GPU
这表示有一个CPU设备和一个GPU设备可用。
注意:list_local_devices()方法返回的设备列表包括CPU、GPU以及其他可能的设备,如TPU。
