简体   繁体   English

如何使用 TensorFlow GPU?

[英]How do I use TensorFlow GPU?

How do I use TensorFlow GPU version instead of CPU version in Python 3.6 x64?如何在 Python 3.6 x64 中使用TensorFlow GPU版本而不是CPU版本?

import tensorflow as tf

Python is using my CPU for calculations. Python 正在使用我的CPU进行计算。
I can notice it because I have an error:我可以注意到它,因为我有一个错误:

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2您的 CPU 支持未编译此 TensorFlow 二进制文件以使用的指令:AVX2

I have installed tensorflow and tensorflow-gpu.我已经安装了 tensorflow 和 tensorflow-gpu。

How do I switch to GPU version?如何切换到 GPU 版本?

Follow this tutorial Tensorflow GPU I did it and it works perfect.按照本教程Tensorflow GPU我做到了,它运行良好。

Attention!注意力! - install version 9.0! - 安装9.0 版! newer version is not supported by Tensorflow-gpu Tensorflow-gpu 不支持较新的版本

Steps:脚步:

  1. Uninstall your old tensorflow卸载旧的 tensorflow
  2. Install tensorflow-gpu pip install tensorflow-gpu安装 tensorflow-gpu pip install tensorflow-gpu
  3. Install Nvidia Graphics Card & Drivers (you probably already have)安装 Nvidia 显卡和驱动程序(您可能已经拥有)
  4. Download & Install CUDA下载并安装 CUDA
  5. Download & Install cuDNN下载并安装 cuDNN
  6. Verify by simple program通过简单程序验证
from tensorflow.python.client import device_lib 
print(device_lib.list_local_devices())

The 'new' way to install tensorflow GPU if you have Nvidia, is with Anaconda .如果你有 Nvidia,安装 tensorflow GPU 的“新”方法是使用Anaconda Works on Windows too.也适用于 Windows。 With 1 line.带 1 条线。

conda create --name tf_gpu tensorflow-gpu 

This is a shortcut for 3 commands, which you can execute separately if you want or if you already have a conda environment and do not need to create one.这是 3 个命令的快捷方式,如果您需要或者如果您已经有一个 conda 环境并且不需要创建一个,您可以单独执行这些命令。

  1. Create an anaconda environment conda create --name tf_gpu创建 anaconda 环境conda create --name tf_gpu

  2. Activate the environment conda activate tf_gpu激活环境conda activate tf_gpu

  3. Install tensorflow-GPU conda install tensorflow-gpu安装 tensorflow-GPU conda install tensorflow-gpu

You can use the conda environment.您可以使用 conda 环境。

Follow the steps in the latest version of the documentation.按照最新版本文档中的步骤操作。 Note: GPU and CPU functionality is now combined in a single tensorflow package注意:GPU 和 CPU 功能现在组合在一个 tensorflow 包中

pip install tensorflow

# OLDER VERSIONS pip install tensorflow-gpu

https://www.tensorflow.org/install/gpu https://www.tensorflow.org/install/gpu

This is a great guide for installing drivers and CUDA if needed: https://www.quantstart.com/articles/installing-tensorflow-22-on-ubuntu-1804-with-an-nvidia-gpu/如果需要,这是安装驱动程序和 CUDA 的绝佳指南: https ://www.quantstart.com/articles/installing-tensorflow-22-on-ubuntu-1804-with-an-nvidia-gpu/

First you need to install tensorflow-gpu , because this package is responsible for gpu computations.首先你需要安装tensorflow-gpu ,因为这个包负责 gpu 计算。 Also remember to run your code with environment variable CUDA_VISIBLE_DEVICES = 0 (or if you have multiple gpus, put their indices with comma).还要记住使用环境变量CUDA_VISIBLE_DEVICES = 0运行您的代码(或者如果您有多个 GPU,请将它们的索引用逗号放置)。 There might be some issues related to using gpu.可能存在与使用 gpu 相关的一些问题。 if your tensorflow does not use gpu anyway, try this如果你的 tensorflow 无论如何都不使用 gpu,试试这个

I tried following the above tutorial.我尝试按照上面的教程进行操作。 Thing is tensorflow changes a lot and so do the NVIDIA versions needed for running on a GPU.事情是 tensorflow 发生了很大变化,在 GPU 上运行所需的 NVIDIA 版本也是如此。 The next issue is that your driver version determines your toolkit version etc. As of today this information about the software requirements should shed some light on how they interplay:下一个问题是您的驱动程序版本决定了您的工具包版本等。截至今天,有关软件要求的这些信息应该可以阐明它们如何相互作用:

NVIDIA® GPU drivers —CUDA 9.0 requires 384.x or higher.
CUDA® Toolkit —TensorFlow supports CUDA 9.0.
CUPTI ships with the CUDA Toolkit.
cuDNN SDK (>= 7.2) Note: Make sure your GPU has compute compatibility >3.0
(Optional) NCCL 2.2 for multiple GPU support.
(Optional) TensorRT 4.0 to improve latency and throughput for inference on some models.

And here you'll find the up-to-date requirements stated by tensorflow (which will hopefully be updated by them on a regular basis).在这里,您会找到 tensorflow 提出的最新要求(希望他们会定期更新)。

For conda environment.对于conda环境。

  • conda search tensorflow to search the available versions of tensorflow. conda search tensorflow搜索可用版本的 tensorflow。 The ones that have mkl are optimized for CPU.具有mkl的那些针对 CPU 进行了优化。 You can choose the ones with gpu .您可以选择带有gpu的那些。
  • Then check the version of your cuda using nvcc --version and find the proper version of tensorflow in this page, according to your version of cuda.然后使用nvcc --version检查您的 cuda 版本,并根据您的 cuda 版本在页面中找到正确的 tensorflow 版本。
  • For example, for cuda/10.1,and python3.8, you can use例如,对于 cuda/10.1 和 python3.8,您可以使用
    • conda install tensorflow=2.2.0=gpu_py38hb782248_0

Strangely, even though the tensorflow website 1 mentions that CUDA 10.1 is compatible with tensorflow-gpu-1.13.1, it doesn't work so far.奇怪的是,即使 tensorflow 网站1提到 CUDA 10.1 与 tensorflow-gpu-1.13.1 兼容,但到目前为止它还不起作用。 tensorflow-gpu gets installed properly though but it throws out weird errors when running. tensorflow-gpu 虽然安装正确,但在运行时会抛出奇怪的错误。

So far, the best configuration to run tensorflow with GPU is CUDA 9.0 with tensorflow_gpu-1.12.0 under python3.6.到目前为止,使用 GPU 运行 tensorflow 的最佳配置是 CUDA 9.0 和 python3.6 下的 tensorflow_gpu-1.12.0。

Following this configuration with the steps mentioned in https://stackoverflow.com/a/51307381/2562870 (the answer above), worked for me :)按照https://stackoverflow.com/a/51307381/2562870 (上面的答案)中提到的步骤进行此配置,对我有用:)

Uninstall tensorflow and install only tensorflow-gpu;卸载tensorflow,只安装tensorflow-gpu; this should be sufficient.这应该足够了。 By default, this should run on the GPU and not the CPU.默认情况下,这应该在 GPU 而不是 CPU 上运行。 However, further you can do the following to specify which GPU you want it to run on.但是,您可以进一步执行以下操作来指定您希望它在哪个 GPU 上运行。

If you have an nvidia GPU, find out your GPU id using the command nvidia-smi on the terminal.如果您有 nvidia GPU,请在终端上使用命令nvidia-smi找出您的 GPU id。 After that, add these lines in your script:之后,在脚本中添加这些行:

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = #GPU_ID from earlier

config = tf.ConfigProto()
sess = tf.Session(config=config)

For the functions where you wish to use GPUs, write something like the following:对于您希望使用 GPU 的功能,请编写如下内容:

with tf.device(tf.DeviceSpec(device_type="GPU", device_index=gpu_id)):

How do I use TensorFlow GPU version instead of CPU version in Python 3.6 x64?如何在Python 3.6 x64中使用TensorFlow GPU版本而不是CPU版本?

import tensorflow as tf

Python is using my CPU for calculations. Python正在使用我的CPU进行计算。
I can notice it because I have an error:我可以注意到它,因为我有一个错误:

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2

I have installed tensorflow and tensorflow-gpu.我已经安装了tensorflow和tensorflow-gpu。

How do I switch to GPU version?如何切换到GPU版本?

Use Docker使用 Docker
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html

This is a better alternative to juggling to sync the most recent versions that are working together correctly这是一个更好的替代方法来同步正确工作的最新版本

There are a lot of threads related to this questions which are version dependent and therefore becoming non-relevant in a short time, here are links to a few threads:有很多与此问题相关的线程依赖于版本,因此在短时间内变得不相关,这里有几个线程的链接:
Why is Tensorflow not recognizing my GPU after conda install? 为什么安装 conda 后 Tensorflow 无法识别我的 GPU?
Which tensorflow-gpu version is compatible with Python 3.7.3 哪个tensorflow-gpu版本兼容Python 3.7.3
Python environment doesn't use GPU while using conda to install tensorflow-gpu Python环境在使用conda安装tensorflow-gpu时不使用GPU

This answer is basically the solution to the more general closed thread: https://stackoverflow.com/questions/69808644/how-to-properly-install-tensorflow-with-gpu这个答案基本上是更通用的封闭线程的解决方案: https : //stackoverflow.com/questions/69808644/how-to-properly-install-tensorflow-with-gpu

There are 2 steps:有2个步骤:

  1. Update your graphics driver to latest and proprietary.(not open source)将您的图形驱动程序更新到最新和专有的。(非开源​​)
  2. Run the simple script I have created to Create conda virtual environment and Download all necessary requirements Script here运行我创建的简单脚本以创建 conda 虚拟环境并在此处下载所有必要的需求脚本

or或者

    echo 'Name of the TENSORFLOW ENVIRONMENT:'
    read ENVNAME

    #CREATING THE ENV
    conda create --name $ENVNAME -y

    #ACTIVATE THE eNV
    conda activate $ENVNAME6

    # INSTALLING CUDA DRIVERS
    conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0 -y

    # INSTALLING TENSORFLOW
    conda install tensorflow-gpu -y
    conda install -c anaconda ipykernel -y
    conda install ipykernel -y

    # ADDING ENV TO JUPYTER LIST
    python3 -m ipykernel install --user --name=$ENVNAME

    # 'VERIFY GPU SUPPORT'
    python3 -c "import tensorflow as tf; 
    print(tf.config.list_physical_devices('GPU'))"

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

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