Python 如何使用 TensorFlow GPU?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51306862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:49:07  来源:igfitidea点击:

How to use TensorFlow GPU?

pythontensorflowgpu

提问by Guruku

How to use TensorFlow GPUversion instead of CPUversion in Python 3.6 x64?

如何在 Python 3.6 x64 中使用TensorFlow GPU版本而不是CPU版本?

import tensorflow as tf

Python is using my CPUfor calculations.
I can notice it because I have an error:

Python 正在使用我的CPU进行计算。
我可以注意到它,因为我有一个错误:

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 to switch to GPU version?

如何切换到GPU版本?

回答by Ashwel

Follow this tutorial Tensorflow GPUI did it and it works perfect.

按照本教程Tensorflow GPU我做到了,而且效果很好。

Attention!- install version 9.0!newer version is not supported by Tensorflow-gpu

注意力!- 安装9.0 版!Tensorflow-gpu 不支持较新的版本

Steps:

脚步:

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

from tensorflow.python.client import device_lib print(device_lib.list_local_devices())

from tensorflow.python.client import device_lib print(device_lib.list_local_devices())

回答by Hazarapet Tunanyan

First you need to install tensorflow-gpu, because this package is responsible for gpu computations. Also remember to run your code with environment variable CUDA_VISIBLE_DEVICES = 0(or if you have multiple gpus, put their indices with comma). There might be some issues related to using gpu. if your tensorflow does not use gpu anyway, try this

首先你需要安装tensorflow-gpu,因为这个包负责 gpu 计算。还要记住使用环境变量CUDA_VISIBLE_DEVICES = 0运行您的代码(或者如果您有多个 gpu,请将它们的索引用逗号放置)。可能存在与使用 GPU 相关的一些问题。如果你的 tensorflow 无论如何都不使用 GPU,试试这个

回答by praneeth

Strangely, even though the tensorflow website 1mentions that CUDA 10.1 is compatible with tensorflow-gpu-1.13.1, it doesn't work so far. tensorflow-gpu gets installed properly though but it throws out weird errors when running.

奇怪的是,尽管 tensorflow 网站1提到 CUDA 10.1 与 tensorflow-gpu-1.13.1 兼容,但到目前为止它不起作用。虽然 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(上面的答案)中提到的步骤进行此配置,对我有用:)

回答by Kristjan Kica

The 'new' way to install tensorflow GPU if you have Nvidia, is with Anaconda. Works on Windows too. With 1 line.

如果您有 Nvidia,安装 tensorflow GPU 的“新”方法是使用Anaconda。也适用于 Windows。带 1 行。

conda create --name tf_gpu tensorflow-gpu 

This is a shortcut for 3 commands, which you can execute separately if you want.

这是 3 个命令的快捷方式,您可以根据需要单独执行。

  1. Create an anaconda environment conda create --name tf_gpu

  2. Activate the environment activate tf_gpu

  3. Install tensorflow-GPU conda install tensorflow-gpu

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

  2. 激活环境 activate tf_gpu

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

You can use the conda environment.

您可以使用 conda 环境。

回答by mrk

I tried following the above tutorial. Thing is tensorflow changes a lot and so do the NVIDIA versions needed for running on a GPU. 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:

我尝试按照上述教程进行操作。事情是 tensorflow 变化很大,在 GPU 上运行所需的 NVIDIA 版本也是如此。下一个问题是您的驱动程序版本决定了您的工具包版本等。截至今天,有关软件要求的这些信息应该阐明它们如何相互作用:

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 hereyou'll find the up-to-date requirements stated by tensorflow (which will hopefully be updated by them on a regular basis).

在这里,你会发现通过tensorflow规定的最高最新的要求(这将有望由他们定期更新)。

回答by Shalini Maiti

Uninstall tensorflow and install only tensorflow-gpu; this should be sufficient. By default, this should run on the GPU and not the CPU. However, further you can do the following to specify which GPU you want it to run on.

卸载tensorflow,只安装tensorflow-gpu;这应该足够了。默认情况下,这应该在 GPU 而不是 CPU 上运行。但是,您可以进一步执行以下操作来指定您希望它在哪个 GPU 上运行。

If you have an nvidia GPU, find out your GPU id using the command nvidia-smion the terminal. After that, add these lines in your script:

如果您有 nvidia GPU,请使用nvidia-smi终端上的命令找出您的 GPU id 。之后,在脚本中添加这些行:

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