【待完善另一种情况】tensorflow 使用CPU而不使用GPU的问题解决

1. 问题

今天运行代码时发现一个怪现象,在使用TensorFlow时,通过os.environ["CUDA_VISIBLE_DEVICES"] = "0"指定使用GPU 0,但是使用nvidia-smi命令查看GPU的使用情况时,发现还是没有使用GPU,而是使用的CPU进行计算,导致计算速度非常慢,跑了一个晚上,才跑出来一点点数据,见下:

在这里插入图片描述
使用下面的代码检测TensorFlow能使用的设备的情况:

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

发现可用设备只有两块CPU:在这里插入图片描述

2. Solution

查找资料,发现可能是TensorFlow CPU版本(1.14)比GPU版本(1.13.1)高的原因,导致两个版本有代差,而tensorflow默认选择版本高的CPU版本来计算了。

升级GPU版本到1.14版本,注意:这里一定要指定升级的版本,因为cuda版本和TensorFlow版本有一定对应关系,如果升级到最新GPU版本,可能会导致不能使用

使用如下命令升级GPU版本到1.14:

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

升级后查看TensorFlow版本:

pip3 list| grep tensorflow

结果为:
在这里插入图片描述
再次使用下面的命令进行检测:

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

结果如下所示,GPU已经赫然在列。在这里插入图片描述

3. other

  • 解决了上述问题,在运行的过程中出现了下面的Could not create cudnn handle错误:

    I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcublas.so.10.0
    I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7
    E tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    E tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    

    在我的环境上,通过进行如下的修改,即可修复该问题。

      from tensorflow.compat.v1 import ConfigProto
      from tensorflow.compat.v1 import InteractiveSession
    
      config = ConfigProto()
      config.gpu_options.allow_growth = True
      session = InteractiveSession(config=config)
    

    用GPU跑程序就是快:
    在这里插入图片描述

  • Linux 文件按时间排序

    • ls -alt 按照修改时间排序,等价于 ls --sort=time -la
    • ls -alc 按照创建时间排序
    • ls -alu 按照访问时间排序
    • 以上命令均可使用 -r 实现逆序排序
  • 另一种 测试tf是否能使用GPU的代码

    import tensorflow as tf
    tf.test.is_gpu_available()
    tf.test.gpu_device_name() #输出gpu的名称
    

4. 更新

在pyenv、conda环境下,TensorFlow CPU、GPU版本一样,同时和cuda的版本也匹配,但运行代码还是不能使用GPU。

TensorFlow issue - 423893319中有类似的问题,不清楚是不是该原因导致的:

We (tensorflow team) do not maintain the packages available through anaconda. 
Please reach out to anaconda with issues with those packages.

看issue问题提出者的回答应该是解决该问题了:不过还是通过卸载老的版本这个操作。

在这里插入图片描述

自己还未解决情况下GPU不能使用的问题。待续… 2019年12月14日21:09:07

5. 参考

  1. tensorflow 使用CPU而不使用GPU的问题解决
  2. LINUX的文件按时间排序
  3. Crash: Could not create cuDNN handle when convnets are used #6698
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值