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的代碼中的單引號的內容,就可以指定對應的設備進行訓練了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM