Tensorflow 1)检测可用设备 2)判断哪个在工作 3)获得版版本号 4) 指定使用显卡设备

用如下代码可检测tensorflow的能使用设备情况:

import os
from tensorflow.python.client import device_lib
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"

if __name__ == "__main__":
    print(device_lib.list_local_devices())

输出:

[name: "/cpu:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5641726301910989523
]

可知目前系统可用设备 为CPU

 

在Python环境中输入:

import numpy
import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

之后就会出现详细的信息:
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/device:GPU:0
a: /job:localhost/replica:0/task:0/device:GPU:0
MatMul: /job:localhost/replica:0/task:0/device:GPU:0
[[ 22.  28.]
 [ 49.  64.]]
就能看到是GPU在工作还是CPU再工作了。

 

 

检查tensorflow的版本情况:

pip3 list

各应用版本为:

1

2

tensorflow      1.10.1

tensorflow-gpu      1.9.0

升级了tensorflow版本,忘记了升级tensorflow-gpu版本,现在两个版本有代差,而tensorflow默认选择版本高的CPU版本来计算了。

升级tensorflow-gpu:

pip3 install --index-url http://pypi.douban.com/simple --trusted-host pypi.douban.com --upgrade tensorflow-gpu

再次检测可用设备情况,结果如下:

2018-09-04 10:51:22.996654: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties: name: GeForce GTX 1060 5GB major: 6 minor: 1 memoryClockRate(GHz): 1.7085 pciBusID: 0000:01:00.0 totalMemory: 4.94GiB freeMemory: 4.23GiB 2018-09-04 10:51:22.996666: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0 2018-09-04 10:51:23.189923: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-09-04 10:51:23.189953: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0 2018-09-04 10:51:23.189959: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0: N 2018-09-04 10:51:23.190105: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/device:GPU:0 with 3969 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060 5GB, pci bus id: 0000:01:00.0, compute capability: 6.1) [name: "/device:CPU:0" device_type: "CPU" memory_limit: 268435456 locality { } incarnation: 15761951721866580392 , name: "/device:GPU:0" device_type: "GPU" memory_limit: 4161994752 locality { bus_id: 1 links { } } incarnation: 16066826866269340415 physical_device_desc: "device: 0, name: GeForce GTX 1060 5GB, pci bus id: 0000:01:00.0, compute capability: 6.1" ]

 

设置使用GPU:

使用 tf.device('/gpu:1') 指定Session在第二块GPU上运行:

import tensorflow as tf 
with tf.device('/gpu:1'):
    v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1')
    v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2')
    sumV12 = v1 + v2
    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:

设置使用cpu:

tensorflow中不同的GPU使用/gpu:0和/gpu:1区分,而CPU不区分设备号,统一使用 /cpu:0

import tensorflow as tf
with tf.device('/cpu:0'):
    v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1')
    v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2')
    sumV12 = v1 + v2
    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
        print sess.run(sumV12)
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值