簡體   English   中英

Tensorflow好像沒看到我的gpu

[英]Tensorflow doesn't seem to see my gpu

我在 cuda 7.5 和 8.0 上都試過 tensorflow,沒有 cudnn(我的 GPU 很舊,cudnn 不支持它)。

當我執行device_lib.list_local_devices()時,output 中沒有 gpu。Theano 看到我的 gpu,並且可以正常使用它,/usr/share/cuda/samples 中的示例也可以正常工作。

我通過 pip 安裝安裝了 tensorflow。 是不是我的gpu太老了,tf不支持了? GTX 460

我在 jupyter 筆記本中遇到了同樣的問題。 這可能是一個簡單的修復。

$ pip uninstall tensorflow
$ pip install tensorflow-gpu

您可以檢查它是否適用於:

tf.test.gpu_device_name()

2020 年更新

似乎 tensorflow 2.0+ 帶有 gpu 功能,因此pip install tensorflow應該就足夠了

如果您使用的是 conda,您可能已經安裝了 tensorflow 的 cpu 版本。 檢查環境的包列表( conda list )以查看是否是這種情況。 如果是這樣,請使用conda remove tensorflow並安裝 keras-gpu( conda install -c anaconda keras-gpu 。這將安裝在 GPU 中運行機器學習代碼所需的一切。干杯!

PS 您應該首先檢查是否使用nvidia-smi正確安裝了驅動程序。 默認情況下,這不在您的 PATH 中,因此您可能還需要將該文件夾添加到您的路徑中。 .exe 文件位於C:\\Program Files\\NVIDIA Corporation\\NVSMI

概括:

  1. 檢查 tensorflow 是否看到您的 GPU(可選)
  2. 檢查您的視頻卡是否可以與 tensorflow 一起使用(可選)
  3. 查找與您的 tf 版本兼容的 CUDA Toolkit 和 cuDNN SDK 版本
  4. 安裝 CUDA 工具包
  5. 安裝cuDNN SDK
  6. pip卸載張量流; pip 安裝 tensorflow-gpu
  7. 檢查 tensorflow 是否看到您的 GPU

*來源 - https://www.tensorflow.org/install/gpu

詳細說明:

  1. 檢查 tensorflow 是否看到您的 GPU(可選)

     from tensorflow.python.client import device_lib def get_available_devices(): local_device_protos = device_lib.list_local_devices() return [x.name for x in local_device_protos] print(get_available_devices()) # my output was => ['/device:CPU:0'] # good output must be => ['/device:CPU:0', '/device:GPU:0']
  2. 檢查您的卡是否可以使用 tensorflow(可選)

  3. 找到您需要的 CUDA Toolkit 和 cuDNN SDK 版本

    a) 找到你的 tf 版本

    import sys print (sys.version) # 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] import tensorflow as tf print(tf.__version__) # my output was => 1.13.1

    b) 為您的 tf 版本找到正確版本的 CUDA Toolkit 和 cuDNN SDK

     https://www.tensorflow.org/install/source#linux * it is written for linux, but worked in my case see, that tensorflow_gpu-1.13.1 needs: CUDA Toolkit v10.0, cuDNN SDK v7.4
  4. 安裝 CUDA 工具包

    a) 安裝 CUDA Toolkit 10.0

     https://developer.nvidia.com/cuda-toolkit-archive select: CUDA Toolkit 10.0 and download base installer (2 GB) installation settings: select only CUDA (my installation path was: D:\\Programs\\x64\\Nvidia\\Cuda_v_10_0\\Development)

    b) 添加環境變量:

     system variables / path must have: D:\\Programs\\x64\\Nvidia\\Cuda_v_10_0\\Development\\bin D:\\Programs\\x64\\Nvidia\\Cuda_v_10_0\\Development\\libnvvp D:\\Programs\\x64\\Nvidia\\Cuda_v_10_0\\Development\\extras\\CUPTI\\libx64 D:\\Programs\\x64\\Nvidia\\Cuda_v_10_0\\Development\\include
  5. 安裝cuDNN SDK

    a) 下載cuDNN SDK v7.4

     https://developer.nvidia.com/rdp/cudnn-archive (needs registration, but it is simple) select "Download cuDNN v7.4.2 (Dec 14, 2018), for CUDA 10.0"

    b) 將“bin”文件夾的路徑添加到“環境變量/系統變量/路徑”中:

     D:\\Programs\\x64\\Nvidia\\cudnn_for_cuda_10_0\\bin
  6. pip 卸載 tensorflow pip 安裝 tensorflow-gpu

  7. 檢查 tensorflow 是否看到您的 GPU

     - restart your PC - print(get_available_devices()) - # now this code should return => ['/device:CPU:0', '/device:GPU:0']

當我查看你的 GPU 時,我發現它只支持 CUDA Compute Capability 2.1。 (可以通過https://developer.nvidia.com/cuda-gpus查看)不幸的是,TensorFlow 需要具有最低 CUDA 計算能力 3.0 的 GPU。 https://www.tensorflow.org/get_started/os_setup#optional_install_cuda_gpus_on_linux

您可能會看到來自 TensorFlow 的一些日志正在檢查您的 GPU,但最終該庫將避免使用不受支持的 GPU。

以下對我有用,hp 筆記本電腦。 我有一個 Cuda Compute 功能(版本)3.0 兼容的 Nvidia 卡。 Windows 7的。

pip3.6.exe uninstall tensorflow-gpu
pip3.6.exe uninstall tensorflow-gpu
pip3.6.exe install tensorflow-gpu

因此,截至 2022 年 4 月, tensorflow package 包含 CPU 和 GPU 版本。 要安裝 GPU 版本,請搜索以查看可用的內容:

λ conda search tensorflow
Loading channels: done
# Name                       Version           Build  Channel
tensorflow                    0.12.1          py35_1  conda-forge
tensorflow                    0.12.1          py35_2  conda-forge
tensorflow                     1.0.0          py35_0  conda-forge
…
tensorflow                     2.5.0 mkl_py39h1fa1df6_0  pkgs/main
tensorflow                     2.6.0 eigen_py37h37bbdb1_0  pkgs/main
tensorflow                     2.6.0 eigen_py38h63d3545_0  pkgs/main
tensorflow                     2.6.0 eigen_py39h855417c_0  pkgs/main
tensorflow                     2.6.0 gpu_py37h3e8f0e3_0  pkgs/main
tensorflow                     2.6.0 gpu_py38hc0e8100_0  pkgs/main
tensorflow                     2.6.0 gpu_py39he88c5ba_0  pkgs/main
tensorflow                     2.6.0 mkl_py37h9623b36_0  pkgs/main
tensorflow                     2.6.0 mkl_py38hdc16138_0  pkgs/main
tensorflow                     2.6.0 mkl_py39h31650da_0  pkgs/main

您可以看到有支持 Python 3.7、3.8 和 3.9 的 TF 2.6.0 構建,並且是為MKL (Intel CPU)、 Eigen或 GPU 構建的。

要縮小范圍,您可以在搜索中使用通配符。 這將找到為 GPU 構建的任何 Tensorflow 2.x 版本,例如:

λ conda search tensorflow=2*=gpu*
Loading channels: done
# Name                       Version           Build  Channel
tensorflow                     2.0.0 gpu_py36hfdd5754_0  pkgs/main
tensorflow                     2.0.0 gpu_py37h57d29ca_0  pkgs/main
tensorflow                     2.1.0 gpu_py36h3346743_0  pkgs/main
tensorflow                     2.1.0 gpu_py37h7db9008_0  pkgs/main
tensorflow                     2.5.0 gpu_py37h23de114_0  pkgs/main
tensorflow                     2.5.0 gpu_py38h8e8c102_0  pkgs/main
tensorflow                     2.5.0 gpu_py39h7dc34a2_0  pkgs/main
tensorflow                     2.6.0 gpu_py37h3e8f0e3_0  pkgs/main
tensorflow                     2.6.0 gpu_py38hc0e8100_0  pkgs/main
tensorflow                     2.6.0 gpu_py39he88c5ba_0  pkgs/main

要在其他空環境中安裝特定版本,您可以使用如下命令:

λ conda activate tf

(tf) λ conda install tensorflow=2.6.0=gpu_py39he88c5ba_0

…

The following NEW packages will be INSTALLED:

  _tflow_select      pkgs/main/win-64::_tflow_select-2.1.0-gpu
  …
  cudatoolkit        pkgs/main/win-64::cudatoolkit-11.3.1-h59b6b97_2
  cudnn              pkgs/main/win-64::cudnn-8.2.1-cuda11.3_0
  …
  tensorflow         pkgs/main/win-64::tensorflow-2.6.0-gpu_py39he88c5ba_0
  tensorflow-base    pkgs/main/win-64::tensorflow-base-2.6.0-gpu_py39hb3da07e_0
  …

如您所見,如果您安裝 GPU 版本,它也會自動安裝兼容cudatoolkitcudnn包。 您不需要手動檢查版本的兼容性,或手動從 Nvidia 的網站下載幾千兆字節,或注冊為開發人員,如其他答案或官方網站上所述

安裝后,通過運行確認它可以正常工作並看到 GPU:

λ python
Python 3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.6.0'
>>> tf.config.list_physical_devices()
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

但是,讓 conda 安裝 GPU 版本您要使用的其他軟件包是另一回事,因為 package 對我來說有很多不兼容之處。 我認為您能做的最好的事情就是使用通配符指定安裝條件並祈禱。

這會嘗試安裝為 GPU 構建的任何TF 2.x 版本,並且具有與 Spyder 和 matplotlib 的依賴項兼容的依賴項,例如:

λ conda install tensorflow=2*=gpu* spyder matplotlib

對我來說,這最終安裝了一個兩年前的 GPU 版本的 tensorflow:

  matplotlib         pkgs/main/win-64::matplotlib-3.5.1-py37haa95532_1
  spyder             pkgs/main/win-64::spyder-5.1.5-py37haa95532_1
  tensorflow         pkgs/main/win-64::tensorflow-2.1.0-gpu_py37h7db9008_0

我以前一直在使用tensorflow-gpu package,但它不再起作用了。 conda 通常會永遠努力尋找要安裝的兼容包,即使安裝了它,它實際上也不會安裝 tensorflow 的 gpu 版本CUDA 依賴項:

λ conda list
…
cookiecutter              1.7.2              pyhd3eb1b0_0
cryptography              3.4.8            py38h71e12ea_0
cycler                    0.11.0             pyhd3eb1b0_0
dataclasses               0.8                pyh6d0b6a4_7
…
tensorflow                2.3.0           mkl_py38h8557ec7_0
tensorflow-base           2.3.0           eigen_py38h75a453f_0
tensorflow-estimator      2.6.0              pyh7b7c402_0
tensorflow-gpu            2.3.0                he13fc11_0

我遇到了一個問題,我需要最新的 TensorFlow(撰寫本文時為 2.8.0),支持在 conda 環境中運行 GPU。 問題是它無法通過 conda 獲得。 我所做的是

conda install cudatoolkit==11.2
pip install tensorflow-gpu==2.8.0

雖然我檢查了 cuda 工具包版本與 tensorflow 版本兼容,但它仍然返回錯誤, libcudart.so.11.0 因此,GPU 不可見。 補救措施是使用此命令將環境變量LD_LIBRARY_PATH設置為指向您的anaconda3/envs/<your_tensorflow_environment>/lib

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/<user>/anaconda3/envs/<your_tensorflow_environment>/lib

除非您將其永久化,否則每次在 session(jupyter notebook)之前啟動終端時都需要創建此變量。 按照 conda 官方網站上的這個過程可以很方便地實現自動化。

我有一個問題,因為我沒有指定Tensorflow的版本,所以我的版本是2.11 幾個小時后,我發現安裝指南中描述了我的問題:

注意: TensorFlow 2.10是最后一個支持 GPU on native-Windows的 TensorFlow 版本。 TensorFlow 2.11開始,您將需要在 WSL2 中安裝 TensorFlow,或安裝 tensorflow-cpu,並可選擇嘗試 TensorFlow-DirectML-Plugin

在此之前,我閱讀了這個問題和類似問題的大部分答案。 我遵循了@AndrewPt 的回答 我已經安裝了CUDA但為了以防萬一更新了版本,安裝了cudNN並重新啟動了計算機。

對我來說最簡單的解決方案是降級到 2.10(您可以嘗試安裝指南中提到的不同選項)。 我首先卸載了所有這些包(可能沒有必要,但我不想在凌晨 2 點看到 pip 是如何搞砸版本的):

pip uninstall keras
pip uninstall tensorflow-io-gcs-filesystem
pip uninstall tensorflow-estimator
pip uninstall tensorflow
pip uninstall Keras-Preprocessing
pip uninstall tensorflow-intel

因為我只想要舊版本所需的包,而我沒有為 2.11 版本的所有必需包都這樣做。 之后我安裝了tensorflow 2.10

pip install tensorflow<2.11

它奏效了。

我使用此代碼檢查 GPU 是否可見:

import tensorflow as tf 
print(tf.config.list_physical_devices('GPU'))

就我而言,我有一個可用的 tensorflow-gpu 版本 1.14,但突然停止工作。 我使用以下方法解決了問題:

 pip  uninstall tensorflow-gpu==1.14
 pip  install   tensorflow-gpu==1.14

我在 Windows 操作系統上遇到了同樣的問題。 我按照 tensorflow 關於安裝 CUDA、cudnn 等的說明進行操作,並嘗試了上述答案中的建議——但沒有成功。 解決我的問題的是更新我的 GPU 驅動程序。 您可以通過以下方式更新它們:

  1. 按下 windows 按鈕 + r
  2. 輸入devmgmt.msc
  3. 右鍵單擊“顯示適配器”並單擊“屬性”選項
  4. 轉到“驅動程序”選項卡並選擇“更新驅動程序”。
  5. 最后,點擊“自動搜索更新的驅動程序軟件”
  6. 重新啟動機器並再次運行以下檢查:
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
[x.name for x in local_device_protos]
Sample output:
2022-01-17 13:41:10.557751: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189
pciBusID: 0000:01:00.0
2022-01-17 13:41:10.558125: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2022-01-17 13:41:10.562095: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2022-01-17 13:45:11.392814: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-01-17 13:45:11.393617: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0
2022-01-17 13:45:11.393739: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N
2022-01-17 13:45:11.401271: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:0 with 1391 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> [x.name for x in local_device_protos]
['/device:CPU:0', '/device:GPU:0']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM