tensorflow查看使用的是cpu还是gpu

https://stackoverflow.com/questions/38009682/how-to-tell-if-tensorflow-is-using-gpu-acceleration-from-inside-python-shell

  • 方法1
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

  • 方法2

  • 方法3

import tensorflow as tf
with tf.device('/gpu:0'):
    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)

with tf.Session() as sess:
    print (sess.run(c))

安装了tensorflow-gpu,但是train的时候用的还是cpu.用方法1能检测到gpu,但实际上计算的时候还是用了cpu.用方法3可以检测出来.

import tensorflow as tf
tf.test.is_gpu_available()

找到libcudart.so所在位置,添加路径到.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64

吐血,我机器上的是cuda10.1.  
https://github.com/tensorflow/tensorflow/issues/26289

pip装的不支持cuda10.1....支持到cuda10.0

重新安装cuda10.0 https://developer.nvidia.com/cuda-10.0-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=runfilelocal下载,安装.done!

安装cudnn
https://developer.nvidia.com/rdp/cudnn-download

感动,终于在gpu上跑起来了!


posted @   core!  阅读(31626)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示