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上跑起來了!



免責聲明!

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



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