为什么 Tensorflow 在安装 conda 后无法识别我的 GPU?

Why is Tensorflow not recognizing my GPU after conda install?

我是深度学习的新手,过去两天我一直在尝试在我的电脑上安装 tensorflow-gpu 版本,但没有成功。我避免安装 CUDA 和 cuDNN 驱动程序,因为由于许多兼容性问题,一些在线论坛不推荐它。因为我之前已经在使用 python 的 conda 发行版,所以我选择了他们官方网站上写的 conda install -c anaconda tensorflow-gpuhttps://anaconda.org/anaconda/tensorflow-gpu .

然而,即使在新的虚拟环境中安装了 gpu 版本(为了避免与基本环境中 pip 安装的库发生潜在冲突),由于某些神秘原因,tensorflow 似乎甚至无法识别我的 GPU。

一些代码片段我 运行(在 anaconda 提示符下)理解它没有识别我的 GPU:-

1.

>>>from tensorflow.python.client import device_lib
        >>>print(device_lib.list_local_devices())
                    [name: "/device:CPU:0"
                device_type: "CPU"
                memory_limit: 268435456
                locality {
                }
                incarnation: 7692219132769779763
                ]

如您所见,它完全忽略了 GPU。

2.

>>>tf.debugging.set_log_device_placement(True)
    >>>a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
2020-12-13 10:11:30.902956: I tensorflow/core/platform/cpu_feature_guard.cc:142] This 
TensorFlow 
binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU 
instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
>>>b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
>>>c = tf.matmul(a, b)
>>>print(c)
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32)

在这里,它应该通过显示 Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0(如此处所写:https://www.tensorflow.org/guide/gpu)来表明它 运行 使用 GPU,但不存在类似的东西。我也不确定第二行之后的消息是什么意思。

我也在网上搜索了几个解决方案,包括这里,但几乎所有问题都与第一种手动安装方法有关,我还没有尝试过,因为每个人都推荐这种方法。

我不再使用 cmd,因为在从基本环境中卸载 tensorflow-cpu 并重新安装后,环境变量不知何故变得混乱,它与 anaconda 提示符完美配合,但与 cmd 不兼容。这是一个单独的问题(也很普遍),但我提到它是为了防止它在这里发挥作用。我在全新的虚拟环境中安装了 gpu 版本以确保安装干净,据我所知,仅需要为手动安装 CUDA 和 cuDNN 库设置路径变量。

我使用的卡:-(支持 CUDA)

C:\WINDOWS\system32>wmic path win32_VideoController get name
Name
NVIDIA GeForce 940MX
Intel(R) HD Graphics 620

Tensorflow 和我目前使用的 python 版本:-

>>> import tensorflow as tf
>>> tf.__version__
'2.3.0'

Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.

系统信息:Windows10 Home,64 位操作系统,基于 x64 的处理器。

任何帮助将不胜感激。提前致谢。

您需要安装 cuDNN 和 CUDA 工具包才能使用您的 GPU。

首先检查兼容版本here

可以找到 cuDNN here(需要免费帐户)。

可以找到 CUDA 工具包 here

同样,安装前请检查兼容版本。较新的版本不向后兼容。

我看到你的 GPU 有 compute capability 5.0 没关系,TensorFlow 应该会喜欢它。因此,我假设在环境设置过程中出现了问题。请尝试使用以下方法创建新环境:

conda create --name tf_gpu tensorflow-gpu 

然后在 tf_gpu 中安装您想要的所有其他软件包,然后重试。

P.S:在环境中只有一个 TensorFlow 包(gpu 包)非常重要。如果您拥有多个,则无法保证

import tensorflow as tf

将导入您想要的...

我也无法(还)让 TF 2.3.0 识别我的 Nvidia Quadro Pro 620 GPU。

注意:我在这台电脑上还有另外 2 个 'environments' (windows Pro) 都是通过 Anaconda 安装的:

  1. Python 3.7.8 TF 2.0.0...识别(并使用)Nvidia GPU
  2. Python 3.6.9 TF 2.1.0...识别(并使用)Nvidia GPU
  3. Python 3.8.6 TF 2.3.0... 看不到 GPU

我的机器有 Cuda 11.1; cuDNN 8.0.5

我的下一个想法是考虑在 TF = 2.3.0

的第三个配置中将 Python 从 3.8.6 降级到 3.7.8

史蒂夫

2021 年 8 月 Conda 安装现在可能正在运行,正如@ComputerScientist 在下面的评论中所说,conda install tensorflow-gpu==2.4.1 将提供 cudatoolkit-10.1.243cudnn-7.6.5

以下内容写于 2021 年 1 月,已过时

目前 conda install tensorflow-gpu 安装 tensorflow v2.3.0,但不安装 conda cudnn 或 cudatoolkit 软件包。手动安装它们(例如使用 conda install cudatoolkit=10.1)似乎也无法解决问题。

一个解决方案是安装较早版本的tensorflow,它会安装cudnn和cudatoolkit,然后用pip升级

conda install tensorflow-gpu=2.1
pip install tensorflow-gpu==2.3.1

(2.4.0 使用 cuda 11.0 和 cudnn 8.0,但是截至 2020 年 12 月 16 日 cudnn 8.0 不在 anaconda 中)

编辑:另请参阅@GZ0 的回答,该回答链接到 github 带有单行解决方案的讨论

@geometrikal 解决方案几乎对我有用。但是在使用 conda 安装 tensorflow-gpu 和使用 pip 安装 tensorflow 2.3 之间,我需要卸载包 tensorflow-gpu 的 tensorflow 部分以避免 pip 的一致性警告。 Conda 会卸载整个包。我知道 Conda does not recommend mixing pip with conda 但这是有效的解决方案,我厌倦了再花一天时间解决这个问题。

conda create -n tfgpu python=3.7
conda activate tfgpu
conda install tensorflow-gpu=2.1

pip uninstall tensorflow
pip uninstall tensorflow-estimator
pip uninstall tensorboard 
pip uninstall tensorboard-plugin-wit
pip install tensorflow==2.3
pip check

安装 tensorflow-gpu 2.3 期间 Anaconda 在 Windows 10 上自动选择的 tensorflow 构建似乎有问题。请找到解决方法 here(如果您有 GitHub 帐户,请考虑对 GitHub 答案投赞成票)。

Windows: Python 3.7: conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py37h936c3e2_0

Python 3.8: conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py38h1fcfbd6_0

使用 conda 安装 TensorFlow 始终是管理 TensorFlow 本身以及 CUDA 和 CUDNN 的多版本的更好方法。我最近创建了一个新的 conda 环境并准备安装最新的 TensorFlow。你说的问题我也遇到了。我检查了 conda install tensorflow-gpu 的依赖列表,发现缺少 cudatoolkitcudnn 包。由于 Anaconda 的最新版本的 tensorflow-gpu 是 2.3,我认为这个问题已经被@GZ0 在 GitHub issue 的回答中指出了。

这里我列出了下面的输出:

使用conda install tensorflow=2.3

PS > conda install tensorflow-gpu=2.3
## Package Plan ##

  environment location: C:\Anaconda3\envs\test_cuda_38

  added / updated specs:
    - tensorflow-gpu=2.3


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    absl-py-0.12.0             |   py38haa95532_0         176 KB
    aiohttp-3.7.4              |   py38h2bbff1b_1         513 KB
    astunparse-1.6.3           |             py_0          17 KB
    async-timeout-3.0.1        |   py38haa95532_0          14 KB
    blas-1.0                   |              mkl           6 KB
    blinker-1.4                |   py38haa95532_0          23 KB
    brotlipy-0.7.0             |py38h2bbff1b_1003         412 KB
    cachetools-4.2.1           |     pyhd3eb1b0_0          13 KB
    cffi-1.14.5                |   py38hcd4344a_0         224 KB
    chardet-3.0.4              |py38haa95532_1003         194 KB
    click-7.1.2                |     pyhd3eb1b0_0          64 KB
    coverage-5.5               |   py38h2bbff1b_2         272 KB
    cryptography-3.4.7         |   py38h71e12ea_0         643 KB
    cython-0.29.23             |   py38hd77b12b_0         1.7 MB
    gast-0.4.0                 |             py_0          15 KB
    google-auth-1.29.0         |     pyhd3eb1b0_0          76 KB
    google-auth-oauthlib-0.4.4 |     pyhd3eb1b0_0          18 KB
    google-pasta-0.2.0         |             py_0          46 KB
    grpcio-1.36.1              |   py38hc60d5dd_1         1.7 MB
    h5py-2.10.0                |   py38h5e291fa_0         841 KB
    hdf5-1.10.4                |       h7ebc959_0         7.9 MB
    icc_rt-2019.0.0            |       h0cc432a_1         6.0 MB
    idna-2.10                  |     pyhd3eb1b0_0          52 KB
    importlib-metadata-3.10.0  |   py38haa95532_0          34 KB
    intel-openmp-2021.2.0      |     haa95532_616         1.8 MB
    keras-applications-1.0.8   |             py_1          29 KB
    keras-preprocessing-1.1.2  |     pyhd3eb1b0_0          35 KB
    libprotobuf-3.14.0         |       h23ce68f_0         1.9 MB
    markdown-3.3.4             |   py38haa95532_0         144 KB
    mkl-2021.2.0               |     haa95532_296       115.5 MB
    mkl-service-2.3.0          |   py38h2bbff1b_1          49 KB
    mkl_fft-1.3.0              |   py38h277e83a_2         137 KB
    mkl_random-1.2.1           |   py38hf11a4ad_2         223 KB
    multidict-5.1.0            |   py38h2bbff1b_2          61 KB
    numpy-1.20.1               |   py38h34a8a5c_0          23 KB
    numpy-base-1.20.1          |   py38haf7ebc8_0         4.2 MB
    oauthlib-3.1.0             |             py_0          91 KB
    opt_einsum-3.1.0           |             py_0          54 KB
    protobuf-3.14.0            |   py38hd77b12b_1         242 KB
    pyasn1-0.4.8               |             py_0          57 KB
    pyasn1-modules-0.2.8       |             py_0          72 KB
    pycparser-2.20             |             py_2          94 KB
    pyjwt-1.7.1                |           py38_0          48 KB
    pyopenssl-20.0.1           |     pyhd3eb1b0_1          49 KB
    pyreadline-2.1             |           py38_1         145 KB
    pysocks-1.7.1              |   py38haa95532_0          31 KB
    requests-2.25.1            |     pyhd3eb1b0_0          52 KB
    requests-oauthlib-1.3.0    |             py_0          23 KB
    rsa-4.7.2                  |     pyhd3eb1b0_1          28 KB
    scipy-1.6.2                |   py38h66253e8_1        13.0 MB
    tensorboard-plugin-wit-1.6.0|             py_0         630 KB
    tensorflow-2.3.0           |mkl_py38h8557ec7_0           6 KB
    tensorflow-base-2.3.0      |eigen_py38h75a453f_0        49.5 MB
    tensorflow-estimator-2.3.0 |     pyheb71bc4_0         271 KB
    termcolor-1.1.0            |   py38haa95532_1           9 KB
    typing-extensions-3.7.4.3  |       hd3eb1b0_0          12 KB
    typing_extensions-3.7.4.3  |     pyh06a4308_0          28 KB
    urllib3-1.26.4             |     pyhd3eb1b0_0         105 KB
    werkzeug-1.0.1             |     pyhd3eb1b0_0         239 KB
    win_inet_pton-1.1.0        |   py38haa95532_0          35 KB
    wrapt-1.12.1               |   py38he774522_1          49 KB
    yarl-1.6.3                 |   py38h2bbff1b_0         153 KB
    ------------------------------------------------------------
                                           Total:       210.0 MB

使用conda install tensorflow=2.1

PS > conda install tensorflow-gpu=2.1
## Package Plan ##

  environment location: C:\Anaconda3\envs\test_cuda

  added / updated specs:
    - tensorflow-gpu=2.1


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _tflow_select-2.1.0        |              gpu           3 KB
    absl-py-0.12.0             |   py37haa95532_0         175 KB
    aiohttp-3.7.4              |   py37h2bbff1b_1         507 KB
    astor-0.8.1                |   py37haa95532_0          47 KB
    async-timeout-3.0.1        |   py37haa95532_0          14 KB
    blas-1.0                   |              mkl           6 KB
    blinker-1.4                |   py37haa95532_0          23 KB
    brotlipy-0.7.0             |py37h2bbff1b_1003         337 KB
    cachetools-4.2.1           |     pyhd3eb1b0_0          13 KB
    cffi-1.14.5                |   py37hcd4344a_0         220 KB
    chardet-3.0.4              |py37haa95532_1003         192 KB
    click-7.1.2                |     pyhd3eb1b0_0          64 KB
    coverage-5.5               |   py37h2bbff1b_2         273 KB
    cryptography-3.4.7         |   py37h71e12ea_0         641 KB
    cudatoolkit-10.1.243       |       h74a9793_0       300.3 MB
    cudnn-7.6.5                |       cuda10.1_0       179.1 MB
    cython-0.29.23             |   py37hd77b12b_0         1.7 MB
    gast-0.2.2                 |           py37_0         155 KB
    google-auth-1.29.0         |     pyhd3eb1b0_0          76 KB
    google-auth-oauthlib-0.4.4 |     pyhd3eb1b0_0          18 KB
    google-pasta-0.2.0         |             py_0          46 KB
    grpcio-1.36.1              |   py37hc60d5dd_1         1.7 MB
    h5py-2.10.0                |   py37h5e291fa_0         808 KB
    hdf5-1.10.4                |       h7ebc959_0         7.9 MB
    icc_rt-2019.0.0            |       h0cc432a_1         6.0 MB
    idna-2.10                  |     pyhd3eb1b0_0          52 KB
    importlib-metadata-3.10.0  |   py37haa95532_0          34 KB
    intel-openmp-2021.2.0      |     haa95532_616         1.8 MB
    keras-applications-1.0.8   |             py_1          29 KB
    keras-preprocessing-1.1.2  |     pyhd3eb1b0_0          35 KB
    libprotobuf-3.14.0         |       h23ce68f_0         1.9 MB
    markdown-3.3.4             |   py37haa95532_0         144 KB
    mkl-2021.2.0               |     haa95532_296       115.5 MB
    mkl-service-2.3.0          |   py37h2bbff1b_1          48 KB
    mkl_fft-1.3.0              |   py37h277e83a_2         133 KB
    mkl_random-1.2.1           |   py37hf11a4ad_2         214 KB
    multidict-5.1.0            |   py37h2bbff1b_2          85 KB
    numpy-1.20.1               |   py37h34a8a5c_0          23 KB
    numpy-base-1.20.1          |   py37haf7ebc8_0         4.1 MB
    oauthlib-3.1.0             |             py_0          91 KB
    opt_einsum-3.1.0           |             py_0          54 KB
    protobuf-3.14.0            |   py37hd77b12b_1         240 KB
    pyasn1-0.4.8               |             py_0          57 KB
    pyasn1-modules-0.2.8       |             py_0          72 KB
    pycparser-2.20             |             py_2          94 KB
    pyjwt-1.7.1                |           py37_0          49 KB
    pyopenssl-20.0.1           |     pyhd3eb1b0_1          49 KB
    pyreadline-2.1             |           py37_1         143 KB
    pysocks-1.7.1              |           py37_1          28 KB
    requests-2.25.1            |     pyhd3eb1b0_0          52 KB
    requests-oauthlib-1.3.0    |             py_0          23 KB
    rsa-4.7.2                  |     pyhd3eb1b0_1          28 KB
    scipy-1.6.2                |   py37h66253e8_1        12.8 MB
    six-1.15.0                 |   py37haa95532_0          51 KB
    tensorboard-plugin-wit-1.6.0|             py_0         630 KB
    tensorflow-2.1.0           |gpu_py37h7db9008_0           4 KB
    tensorflow-base-2.1.0      |gpu_py37h55f5790_0       105.3 MB
    tensorflow-estimator-2.1.0 |     pyhd54b08b_0         251 KB
    tensorflow-gpu-2.1.0       |       h0d30ee6_0           3 KB
    termcolor-1.1.0            |   py37haa95532_1           9 KB
    typing-extensions-3.7.4.3  |       hd3eb1b0_0          12 KB
    typing_extensions-3.7.4.3  |     pyh06a4308_0          28 KB
    urllib3-1.26.4             |     pyhd3eb1b0_0         105 KB
    werkzeug-0.16.1            |             py_0         258 KB
    win_inet_pton-1.1.0        |   py37haa95532_0          35 KB
    wrapt-1.12.1               |   py37he774522_1          49 KB
    yarl-1.6.3                 |   py37h2bbff1b_0         151 KB
    ------------------------------------------------------------
                                           Total:       745.0 MB

因此,您可以使用@GZ0 和@geometrikal 的建议在Windows 平台上从Anaconda 安装最新版本(v2.3) 的tensorflow-gpu,或者只使用conda install tensorflow-gpu=2.1获得最新和正确的环境。

注意tensorflow-gpu v2.1只支持3.5-3.7之间的Python。

截至 2021 年 8 月,使用 TensorFlow 2.4.1,我相信它似乎在 conda 环境中安装了 CUDA 和 CuDNN。这是我在 Ubuntu 18.04 机器上创建一个新的 conda env 所做的:

conda create --name tftest python=3.7 -y  &&  conda activate tftest
conda install ipython tensorflow-gpu==2.4.1 -y

上面的命令将列出以下要安装的包。为了我们的目的,请注意 cudatoolkitcudnn 是如何列出的

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _tflow_select-2.1.0        |              gpu           2 KB
    absl-py-0.13.0             |   py37h06a4308_0         173 KB
    aiohttp-3.7.4              |   py37h27cfd23_1         536 KB
    astor-0.8.1                |   py37h06a4308_0          47 KB
    astunparse-1.6.3           |             py_0          17 KB
    async-timeout-3.0.1        |   py37h06a4308_0          13 KB
    attrs-21.2.0               |     pyhd3eb1b0_0          46 KB
    backcall-0.2.0             |     pyhd3eb1b0_0          13 KB
    blas-1.0                   |              mkl           6 KB
    blinker-1.4                |   py37h06a4308_0          23 KB
    brotlipy-0.7.0             |py37h27cfd23_1003         320 KB
    c-ares-1.17.1              |       h27cfd23_0         108 KB
    cachetools-4.2.2           |     pyhd3eb1b0_0          13 KB
    cffi-1.14.6                |   py37h400218f_0         223 KB
    chardet-3.0.4              |py37h06a4308_1003         175 KB
    charset-normalizer-2.0.4   |     pyhd3eb1b0_0          35 KB
    click-8.0.1                |     pyhd3eb1b0_0          79 KB
    coverage-5.5               |   py37h27cfd23_2         254 KB
    cryptography-3.4.7         |   py37hd23ed53_0         904 KB
    cudatoolkit-10.1.243       |       h6bb024c_0       347.4 MB
    cudnn-7.6.5                |       cuda10.1_0       179.9 MB
    cupti-10.1.168             |                0         1.4 MB
    cython-0.29.24             |   py37h295c915_0         1.9 MB
    decorator-5.0.9            |     pyhd3eb1b0_0          12 KB
    gast-0.4.0                 |     pyhd3eb1b0_0          13 KB
    google-auth-1.33.0         |     pyhd3eb1b0_0          80 KB
    google-auth-oauthlib-0.4.4 |     pyhd3eb1b0_0          18 KB
    google-pasta-0.2.0         |             py_0          46 KB
    grpcio-1.36.1              |   py37h2157cd5_1         1.9 MB
    h5py-2.10.0                |   py37hd6299e0_1         902 KB
    hdf5-1.10.6                |       hb1b8bf9_0         3.7 MB
    idna-3.2                   |     pyhd3eb1b0_0          48 KB
    importlib-metadata-3.10.0  |   py37h06a4308_0          33 KB
    intel-openmp-2021.3.0      |    h06a4308_3350         1.4 MB
    ipython-7.26.0             |   py37hb070fc8_0        1005 KB
    ipython_genutils-0.2.0     |     pyhd3eb1b0_1          27 KB
    jedi-0.18.0                |   py37h06a4308_1         911 KB
    keras-preprocessing-1.1.2  |     pyhd3eb1b0_0          35 KB
    libgfortran-ng-7.5.0       |      ha8ba4b0_17          22 KB
    libgfortran4-7.5.0         |      ha8ba4b0_17         995 KB
    libprotobuf-3.17.2         |       h4ff587b_1         2.0 MB
    markdown-3.3.4             |   py37h06a4308_0         127 KB
    matplotlib-inline-0.1.2    |     pyhd3eb1b0_2          12 KB
    mkl-2021.3.0               |     h06a4308_520       141.2 MB
    mkl-service-2.4.0          |   py37h7f8727e_0          56 KB
    mkl_fft-1.3.0              |   py37h42c9631_2         170 KB
    mkl_random-1.2.2           |   py37h51133e4_0         287 KB
    multidict-5.1.0            |   py37h27cfd23_2          66 KB
    numpy-1.20.3               |   py37hf144106_0          23 KB
    numpy-base-1.20.3          |   py37h74d4b33_0         4.5 MB
    oauthlib-3.1.1             |     pyhd3eb1b0_0          90 KB
    opt_einsum-3.3.0           |     pyhd3eb1b0_1          57 KB
    parso-0.8.2                |     pyhd3eb1b0_0          69 KB
    pexpect-4.8.0              |     pyhd3eb1b0_3          53 KB
    pickleshare-0.7.5          |  pyhd3eb1b0_1003          13 KB
    prompt-toolkit-3.0.17      |     pyh06a4308_0         256 KB
    protobuf-3.17.2            |   py37h295c915_0         319 KB
    ptyprocess-0.7.0           |     pyhd3eb1b0_2          17 KB
    pyasn1-0.4.8               |             py_0          57 KB
    pyasn1-modules-0.2.8       |             py_0          72 KB
    pygments-2.10.0            |     pyhd3eb1b0_0         725 KB
    pyjwt-2.1.0                |   py37h06a4308_0          32 KB
    pyopenssl-20.0.1           |     pyhd3eb1b0_1          49 KB
    pysocks-1.7.1              |           py37_1          27 KB
    python-flatbuffers-1.12    |     pyhd3eb1b0_0          24 KB
    requests-2.26.0            |     pyhd3eb1b0_0          59 KB
    requests-oauthlib-1.3.0    |             py_0          23 KB
    rsa-4.7.2                  |     pyhd3eb1b0_1          28 KB
    scipy-1.6.2                |   py37had2a1c9_1        15.5 MB
    six-1.16.0                 |     pyhd3eb1b0_0          18 KB
    tensorboard-2.4.0          |     pyhc547734_0         8.8 MB
    tensorboard-plugin-wit-1.6.0|             py_0         630 KB
    tensorflow-2.4.1           |gpu_py37ha2e99fa_0           4 KB
    tensorflow-base-2.4.1      |gpu_py37h29c2da4_0       195.2 MB
    tensorflow-estimator-2.5.0 |     pyh7b7c402_0         267 KB
    tensorflow-gpu-2.4.1       |       h30adc30_0           3 KB
    termcolor-1.1.0            |   py37h06a4308_1           9 KB
    traitlets-5.0.5            |     pyhd3eb1b0_0          81 KB
    typing-extensions-3.10.0.0 |       hd3eb1b0_0           8 KB
    typing_extensions-3.10.0.0 |     pyh06a4308_0          27 KB
    urllib3-1.26.6             |     pyhd3eb1b0_1         112 KB
    wcwidth-0.2.5              |             py_0          29 KB
    werkzeug-1.0.1             |     pyhd3eb1b0_0         239 KB
    wrapt-1.12.1               |   py37h7b6447c_1          49 KB
    yarl-1.6.3                 |   py37h27cfd23_0         133 KB
    zipp-3.5.0                 |     pyhd3eb1b0_0          13 KB
    ------------------------------------------------------------
                                           Total:       915.9 MB

接下来,运行 ipython 并尝试:

In [1]: import tensorflow as tf
2021-08-29 12:26:36.582384: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1

In [2]: tf.config.list_physical_devices('GPU')
2021-08-29 12:26:48.676151: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-08-29 12:26:48.679894: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2021-08-29 12:26:48.975002: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties: 
pciBusID: 0000:04:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
2021-08-29 12:26:48.979341: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 1 with properties: 
pciBusID: 0000:08:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
2021-08-29 12:26:48.981747: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 2 with properties: 
pciBusID: 0000:09:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
2021-08-29 12:26:48.990002: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 3 with properties: 
pciBusID: 0000:85:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
2021-08-29 12:26:48.992488: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 4 with properties: 
pciBusID: 0000:89:00.0 name: Tesla V100-PCIE-32GB computeCapability: 7.0
coreClock: 1.38GHz coreCount: 80 deviceMemorySize: 31.75GiB deviceMemoryBandwidth: 836.37GiB/s
2021-08-29 12:26:48.992523: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1
2021-08-29 12:26:49.312793: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.10
2021-08-29 12:26:49.312907: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.10
2021-08-29 12:26:49.388961: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.10
2021-08-29 12:26:49.413946: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.10
2021-08-29 12:26:49.535055: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusolver.so.10
2021-08-29 12:26:49.563142: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.10
2021-08-29 12:26:50.009291: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.7
2021-08-29 12:26:50.051301: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0, 1, 2, 3, 4
Out[2]: 
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'),
 PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'),
 PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU'),
 PhysicalDevice(name='/physical_device:GPU:3', device_type='GPU'),
 PhysicalDevice(name='/physical_device:GPU:4', device_type='GPU')]

In [3]: tf.test.is_built_with_cuda()
Out[3]: True

这台机器有5个GPU,所以上面的输出是正确的。

我不确定的是为什么要安装 CUDA 10.1 和 CuDNN 7.6.5,因为 from Google's TF compatibility chart 2.4.0(大概是 2.4.1?)似乎可以与 CUDA 11.0 一起使用和 CuDNN 8。如果有人对此有见解,请随时加入...

以下步骤对我有用:

按照视频中的操作进行。 https://www.youtube.com/watch?v=r31jnE7pR-g

同时安装视频中缺少的 tensorflow estimator。在图片中,您可以看到适合我的环境。

my environment

也许您必须将版本更改为相同的版本。 现在进入 Visual Code 和 运行 您的代码以及您之前创建的 anaconda 环境。见下图。

select your environment

在我的例子中是 tf_env,我创建并命名的。

尝试 运行 您的代码。如果 Visual Code 提示缺少某些内容,请尝试使用 anaconda 终端安装它。单击“播放”按钮启动终端。

play Button terminal

进行更改时也关闭并打开 Visual Code,有时也关闭 anaconda。现在试试下面的代码。

> print("Num GPU: ", len(tf.config.list_physical_devices("GPU")))
> 
> print(tf.test.is_gpu_available()) print(tf.test.is_built_with_cuda())



> OUTPUT 
> Num GPU:  1 
> WARNING:tensorflow:From <ipython-input-2-8748de971110>:3:
> is_gpu_available (from tensorflow.python.framework.test_util) is
> deprecated and will be removed in a future version. Instructions for
> updating: Use `tf.config.list_physical_devices('GPU')` instead. 
> True
> True

如果您的输出相同,则一切正常。现在,当您训练模型时,您应该会在任务管理器中看到您的 gpu 运行ning。

希望对大家有所帮助:)

以我为例(2022 年 4 月):

 conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py38h1fcfbd6_0 cudatoolkit cudnn keras matplotlib

完美运行!!它安装了 tensorflow-gpu=2.3 - cudatoolkit 10.1.243 和 cudnn 7.6.5

2022 年 4 月 26 日更新,tensorflow 2.6.0 完成这项工作, Python 版本 3.8.13

回顾 - 在 Anaconda Jupyter 中遇到同样的“GPU 不显示”问题

'2.3.0'

按照此 link 中的步骤,再次出现“GPU 未显示”。

反复试验:下面的解决方法可以解决问题。

  1. 从 Navigator 启动 CMD window(相同的环境);
  2. conda install tensorflow=2.6.0 --channel conda-forge -y;
  3. 需要一些时间(收集包、解析环境等);
  4. 在这种情况下,自动将 CUDA 和 cuDNN 版本分别更新到 11.3 和 8.2。
  5. 完成后,退出Anaconda并重启电脑。
  6. Return 到 Jupyter,运行 单元格:

cell

并检查结果:

2.6.0

[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]