Tensorflow 2.0 list_physical_devices 未检测到我的 GPU

Kol*_*pox 13 python gpu tensorflow2.0

我最近在计算机上安装了tensorflow 2.0,但是当我尝试在 GPU 上运行它时,Jupyter 或 Vitual Studio Code 上的函数tf.config.experimental.list_physical_devices('GPU')会返回一个空数组。你知道为什么吗 ?

我的设置:

电脑:微星

处理器:Intel(R) Core(TM) i7-8750H CPU @ 2.220GHz

GPU 0:英特尔(R) 超高清显卡 630

显卡:NVIDIA GeForce GTX 1060

Python:Ananconda 3 与 Python 3.7

Tensenflow 2.0 安装有pip install tensorflow

我的测试代码:

physical_devices = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices)
if physical_devices:
  tf.config.experimental.set_memory_growth(physical_devices[0], True)
Run Code Online (Sandbox Code Playgroud)

提前致谢 !:)

ade*_*ago 8

大多数答案都已经过时了。现代版本tensorflow包括tensorflow-gpu. 无法pip再使用最后一个来安装。

运行以下脚本来检查您的可用设备:

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())

# Your output is probably something like ['/device:CPU:0']
# It should be ['/device:CPU:0', '/device:GPU:0']
Run Code Online (Sandbox Code Playgroud)

如果您没有看到 GPU,请按照以下步骤操作:

  1. 从此处更新您的 NVIDIA 显卡:https://www.nvidia.com/Download/index.aspx ?lang=en-us

  2. 了解您需要哪个 CUDA 版本。运行您希望在 GPU 中运行的脚本时,错误消息会告诉您这一点。例如,对于我的 NVIDIA RTX 2060 Super,我得到2021-01-06 12:14:56.670596: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found因此我需要 CUDA 版本 11。这一步非常重要,必须下载兼容的版本。

  3. 从https://developer.nvidia.com/cuda-toolkit-archive下载并安装 CUDA 工具包,选择您需要的版本(如上一步中所示),或下载最新版本(当前版本为 12)写作)。

  4. 按照此处的说明安装 Zlib 和 cuDNN:https ://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-zlib-windows

  5. 运行顶部的初始脚本,您现在应该看到类似以下内容:

在此输入图像描述

提示:请确保在前面步骤中的每次驱动程序更新或软件安装后重新启动,以防万一。

之后,您所做的所有计算都应该默认使用 GPU。您可以通过查看任务栏的 GPU 使用情况来检查这一点。

有用的资源:


小智 6

在这里提供解决方案(答案部分),即使它出现在评论部分中也是为了社区的利益。

pip install tensorflow您可以尝试pip3 install --upgrade tensorflow-gpu或直接删除,而不是tensorflow,然后installing "tensorflow-gpu就会解决您的问题。

安装完Tensorflow GPU后,可以如下检查GPU

physical_devices = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices)
if physical_devices:
  tf.config.experimental.set_memory_growth(physical_devices[0], True)
Run Code Online (Sandbox Code Playgroud)

输出:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
Run Code Online (Sandbox Code Playgroud)

  • 尝试在 2023 年执行此操作,在“pip install tensorflow-gpu”上,收到错误““tensorflow-gpu”包已被删除!请安装“tensorflow”。` (3认同)