简体   繁体   English

在python3.7中使用tensorflow-gpu 2.0.0-alpha0时出错

[英]Error when using tensorflow-gpu 2.0.0-alpha0 in python3.7

I have followed the instructions shown on tensorflow's website to the absolute letter, yet I still get the following error when trying to import tensorflow in python:我已经按照tensorflow 网站显示的说明进行了绝对字母操作,但是在尝试在 python 中导入 tensorflow 时仍然出现以下错误:

Traceback (most recent call last):
File "C:\Users\redacted\source\Repos\TFTest1\TFTest1\tf2-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module> from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\redacted\source\Repos\TFTest1\TFTest1\tf2-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>     _pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\redacted\source\Repos\TFTest1\TFTest1\tf2-gpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper     _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\imp.py", line 242, in load_module     return load_dynamic(name, filename, file)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\imp.py", line 342, in load_dynamic     return _load(spec) ImportError: DLL load failed: The specified module could not be found.
Failed to load the native TensorFlow runtime.  See https://www.tensorflow.org/install/errors  for some common reasons and solutions.  Include the entire stack trace above this error message when asking for help.

which is thrown when I run the following code当我运行以下代码时抛出

import tensorflow as tf

My setup is as follows:我的设置如下:

  • Windows 10视窗 10
  • AMD 2700X CPU AMD 2700X 中央处理器
  • Nvidia RTX 2080 GPU英伟达 RTX 2080 GPU
  • Python 3.7 (64bit) Python 3.7(64 位)
  • CUDA Toolkit 10.0 CUDA 工具包 10.0
  • CUDAnn 7.5.1 (for CUDA 10.0) CUDAnn 7.5.1(适用于 CUDA 10.0)

I have the following in my User-Scoped PATH environment variable:我的用户范围 PATH 环境变量中有以下内容:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include
C:\tools\cuda\bin

I have then been able to confirm that CUDA is properly installed by running the following in the command line:然后,我可以通过在命令行中运行以下命令来确认 CUDA 已正确安装:

nvcc -V

which shows me这让我看到

nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2018 NVIDIA
Corporation Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018
Cuda compilation tools, release 10.0, V10.0.130

I'm using Visual Studio 2019 to run the python scripts in a fresh virtual environment (With python version 3.7).我正在使用 Visual Studio 2019 在全新的虚拟环境中运行 python 脚本(使用 python 3.7 版)。 In that environment I have run the following command:在那个环境中,我运行了以下命令:

pip install tensorflow-gpu==2.0.0-alpha0

which installed various packages, and in that environment I get the aforementioned error.它安装了各种软件包,在那个环境中我得到了上述错误。

As a side note my script runs just fine when using CPU only tensorflow.作为旁注,我的脚本在仅使用 CPU tensorflow 时运行得很好。 In a python 3.7 environment with the added package pip install tensorflow==2.0.0-alpha0 it all works, but it's running on my CPU, and slowly!在 python 3.7 环境中添加了包pip install tensorflow==2.0.0-alpha0一切正常,但它在我的 CPU 上运行,而且很慢!

The Two-fold Question:双重问题:

  • How can I get tensorflow 2.0 to do its work on the GPU rather than the CPU?如何让 tensorflow 2.0 在 GPU 而不是 CPU 上工作?
  • Is my setup correct for tensorflow 2.0 GPU?我的设置是否适用于 tensorflow 2.0 GPU?

I was able to solve my problem by having the exact same setup as I described in my original post, but instead used Python 3.6 instead of 3.7.通过使用与我在原始帖子中描述的完全相同的设置,我能够解决我的问题,但使用 Python 3.6而不是 3.7。 All other variables remained the same.所有其他变量保持不变。 (Thank to Peter for helping find the answer!) (感谢彼得帮助找到答案!)

I started with a fresh environment in Visual Studio 2019 and ran the following imports:我从 Visual Studio 2019 中的新环境开始,并运行了以下导入:

pip install tensorflow-gpu==2.0.0-alpha0

I was then able to run my code on the CPU and GPU.然后我就可以在 CPU 和 GPU 上运行我的代码了。 I confirmed the GPU was available to tensorflow by running:我通过运行来确认 GPU 可用于 tensorflow:

from tensorflow.python.client import device_lib

print(device_lib.list_local_devices())

which displayed其中显示

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 18233115335171708614
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 6588081767
locality {
  bus_id: 1
  links {
  }
}
incarnation: 12253248400045769946
physical_device_desc: "device: 0, name: GeForce RTX 2080, pci bus id: 0000:1d:00.0, compute capability: 7.5"
]

I could also see that CPU usage was at 25% and the GPU usage was at 10% using Task Manager.我还可以看到使用任务管理器时 CPU 使用率为 25%,GPU 使用率为 10%。 I suppose my neural network is somewhat linear and cannot really make full use of the GPU quite yet.我想我的神经网络有点线性,还不能真正充分利用 GPU。

Problem solved!问题解决了!

New GPU?新 GPU? Had the same problem couple of weeks ago.几周前遇到了同样的问题。 Took me hours to find the problem.我花了几个小时才找到问题。 Here are some ideas:这里有一些想法:

As of April 2019, I solved the "DLL load failed" problem under Windows 10 / Python 3.6.x / GPU RTX 20xx by installing CUDA 10.0 ( not 10.1 or 9.x!!! ) with cuDNN 7.5.0.截至 2019 年 4 月,我通过使用 cuDNN 7.5.0 安装 CUDA 10.0(不是 10.1 或 9.x!!! )解决了 Windows 10 / Python 3.6.x / GPU RTX 20xx 下的“DLL 加载失败”问题。 I also have Visual Studio 2015 installed.我还安装了 Visual Studio 2015。

Make sure its CUDA 10.0!!确保它的 CUDA 10.0 !! Place the files from cuDNN in the respective dirs of your CUDA installation.将 cuDNN 中的文件放在 CUDA 安装的相应目录中。 Don't forget to add the location of your cuDNN *.dll files (the /bin/ folder of your CUDA installation) to your PATH (win environment variables).不要忘记将 cuDNN *.dll 文件的位置(CUDA 安装的 /bin/ 文件夹)添加到 PATH(win 环境变量)。

Tensorflow can be installed using pip install tensorflow-gpu (as of April version 1.13.1). Tensorflow 可以使用 pip install tensorflow-gpu 安装(截至 4 月版本 1.13.1)。 TF 2.0 should work the same way. TF 2.0 应该以同样的方式工作。 Your problem seems to be related to CUDA.您的问题似乎与 CUDA 有关。 However, if there is no specific reason why you need TF 2.0, I would go with the standard installation.但是,如果没有特定原因需要 TF 2.0,我会选择标准安装。

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

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