TensorFlow指定GPU/CPU进行训练和输出devices信息

Posted youpeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TensorFlow指定GPU/CPU进行训练和输出devices信息相关的知识,希望对你有一定的参考价值。

TensorFlow指定GPU/CPU进行训练和输出devices信息

1.在tensorflow代码中指定GPU/CPU进行训练

with tf.device('/gpu:0'):
    ....
with tf.device('/gpu:1'):
    ...
with tf.device('/cpu:0'):
    ...

2.输出devices的信息

在指定devices的时候往往不知道具体的设备信息,这时可用下面的代码查看对应的信息

进入Python环境

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

输出以下信息:

2019-05-23 20:12:47.415412: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-05-23 20:12:47.509275: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-05-23 20:12:47.509632: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x14b6e60 executing computations on platform CUDA. Devices:
2019-05-23 20:12:47.509660: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce MX150, Compute Capability 6.1
2019-05-23 20:12:47.529891: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1992000000 Hz
2019-05-23 20:12:47.530293: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x1b7b140 executing computations on platform Host. Devices:
2019-05-23 20:12:47.530318: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
2019-05-23 20:12:47.530451: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce MX150 major: 6 minor: 1 memoryClockRate(GHz): 1.341
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.92GiB
2019-05-23 20:12:47.530468: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-05-23 20:12:47.531469: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-05-23 20:12:47.531487: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
2019-05-23 20:12:47.531494: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 
2019-05-23 20:12:47.531563: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 1738 MB memory) -> physical GPU (device: 0, name: GeForce MX150, pci bus id: 0000:01:00.0, compute capability: 6.1)
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 1736381910647465363
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 10300285037066135290
physical_device_desc: "device: XLA_GPU device"
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 6680013036417599682
physical_device_desc: "device: XLA_CPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1823080448
locality {
  bus_id: 1
  links {
  }
}
incarnation: 7894169161128462449
physical_device_desc: "device: 0, name: GeForce MX150, pci bus id: 0000:01:00.0, compute capability: 6.1"
]

找到对应devices的name,复制双引号下的名字,替换第1的代码中的单引号的内容,就可以指定对应的设备进行训练了。

以上是关于TensorFlow指定GPU/CPU进行训练和输出devices信息的主要内容,如果未能解决你的问题,请参考以下文章

fakeapp训练结束后用保存吗

Keras/Tensorflow选择GPU/CPU运行

Tensorflow训练识别自定义图片

深度学习基础——硬件知识总结

[深度学习] Pytorch—— 多/单GPUCPU,训练保存加载模型参数问题

tensorflow saver 保存和恢复指定 tensor