简体   繁体   English

如何让 tensorflow keras 使用我的 GPU?

[英]How to get tensorflow keras to use my GPU?

I am trying to use keras in tensorflow to train a CNN network for some image classification.我正在尝试在 tensorflow 中使用 keras 来训练 CNN 网络进行某些图像分类。 Obviously, the training running on my CPU is incredibly slow and so I need to use my GPU to do the training.显然,在我的 CPU 上运行的训练非常慢,所以我需要使用我的 GPU 来进行训练。 I've found many similar questions on StackOverflow, none of which have helped me get the GPU to work, hence I am asking this question separately.我在 StackOverflow 上发现了许多类似的问题,但都没有帮助我让 GPU 工作,因此我单独提出这个问题。

I've got an NVIDIA GeForce GTX 1060 3GB and the 466.47 NVIDIA driver installed.我已经安装了 NVIDIA GeForce GTX 1060 3GB 和 466.47 NVIDIA 驱动程序。 I've installed the CUDA toolkit from the NVIDIA website (installation is confirmed with nvcc -V command outputting my version 11.3), and downloaded the CUDNN library.我已经从 NVIDIA 网站安装了 CUDA 工具包(通过输出我的版本 11.3 的 nvcc -V 命令确认安装),并下载了 CUDNN 库。 I unzipped the CUDNN file and copied the files to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3, as stated on the NVIDIA website.我解压缩了 CUDNN 文件并将文件复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3,如 NVIDIA 网站上所述。 Finally, I've checked that it's on PATH (C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\bin and C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\libnvvp are both in the environment variable 'Path'). Finally, I've checked that it's on PATH (C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\bin and C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\libnvvp are both在环境变量“路径”中)。

I then set up an environment using conda, downloading some packages that I need, like scikit-learn, as well as tensorflow-gpu=2.3 After booting my environment into Jupyter Notebook, I run this code to check to see if it's picking up the GPU:然后我使用 conda 设置了一个环境,下载了一些我需要的包,比如 scikit-learn 以及 tensorflow-gpu=2.3 在将我的环境引导到 Jupyter Notebook 之后,我运行这个代码来检查它是否在拾取 GPU:

import tensorflow as tf
print(tf.__version__)
print(tf.config.list_physical_devices())

And get this:得到这个:

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

I have tried literally everything I have come into contact with on this topic, but am not getting any success in getting it to work.我已经尝试了我在这个主题上接触过的所有东西,但在让它发挥作用方面没有取得任何成功。 Any help would be appreciated.任何帮助,将不胜感激。

You, first, have to install all CUDA requirements.首先,您必须安装所有 CUDA 要求。 If you have Ubuntu 20.04, here is how you can install the requirements.如果您有 Ubuntu 20.04,请按照以下说明安装要求。 Then it's the right time to install tensorflow.那么是时候安装 tensorflow。 Asa you intended to utilize your GPU, you have install tensorflow-gpu library, not tensorflow alone.作为你打算使用你的 GPU,你已经安装了 tensorflow-gpu 库,而不是单独的 tensorflow。

I'm guessing you have installed TensorFlow correctly using pip install tensorflow .我猜你已经使用pip install tensorflow NVIDIA GPU cards with CUDA architectures 3.5, 5.0, 6.0, 7.0, 7.5, 8.0 and higher than 8.0 are currently supported by TensorFlow. NVIDIA GPU 卡与 CUDA 架构 3.5、5.0、6.0、7.0、7.5、8.0 和高于 8.0 目前受 Z074DD699710DA0EC1E3EB45.F1 支持If you have the supported cards but TensorFlow cannot detect your GPU, you have to install the following software:如果您有支持的卡,但 TensorFlow 无法检测到您的 GPU,则必须安装以下软件:

You can optionally install TensorRT 6.0 to improve latency and throughput for inference on some models.您可以选择安装TensorRT 6.0以改善某些模型的推理延迟和吞吐量。 For more info, please refer to the TensorFlow documentation: https://www.tensorflow.org/install/gpu有关详细信息,请参阅 TensorFlow 文档: https://www.tensorflow.org/install/gpu

I recommend to use conda to install the CUDA Toolkit packages as well as CUDNN, which will avoid wasting time downloading the right packages (or making changes in the system folders)我建议使用 conda 安装 CUDA Toolkit 包以及 CUDNN,这样可以避免浪费时间下载正确的包(或在系统文件夹中进行更改)

conda install -c conda-forge cudatoolkit=11.0 cudnn=8.1

Then you can install keras and tensorflow-gpu by typing然后您可以通过键入安装 keras 和 tensorflow-gpu

conda install keras==2.7
pip install tensorflow-gpu==2.7

and it will work directly.它会直接工作。

Based on this issue基于这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM