win10上使用gpu版的tensorflow

前置条件

  1. 电脑上装有nvidia显卡
  2. 已安装显卡驱动
  3. 安装过驱动支持的cuda和cudnn版本
  4. 安装gpu版本的tensorflow

以上条件如果不满足,请先参考此篇,安装好显卡驱动,cuda和cudnn。然后pip安装tensorflow_gpu==2.x.x,当导入tensorflow时

>>> import tensorflow as tf
D:\python36_win_tf\Python36\lib\site-packages\numpy\_distributor_init.py:32: UserWarning: loaded more than 1 DLL from .libs:
D:\python36_win_tf\Python36\lib\site-packages\numpy\.libs\libopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll
D:\python36_win_tf\Python36\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
  stacklevel=1)
Traceback (most recent call last):
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow_core\python\platform\self_check.py", line 47, in preload_check
    ctypes.WinDLL(build_info.msvcp_dll_name)
  File "D:\python36_win_tf\Python36\lib\ctypes\__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in <module>
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
    from tensorflow_core import *
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "D:\python36_win_tf\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 30, in <module>
    self_check.preload_check()
  File "D:\python36_win_tf\Python36\lib\site-packages\tensorflow_core\python\platform\self_check.py", line 55, in preload_check
    % build_info.msvcp_dll_name)
ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. You may install this DLL by downloading Visual C++ 2015 Redistributable Update 3 from this URL: https://www.microsoft.com/en-us/download/details.aspx?id=53587

根据报错信息,缺少c++编译工具的动态库,需安装Visual c++ 2015。因此找官网下载一份,这里提供一个链接
win10上使用gpu版的tensorflow_第1张图片

下载下来的镜像里面包含有VisualCppBuildTools_Full.exe
win10上使用gpu版的tensorflow_第2张图片

双击安装,只需安装8.1即可。
win10上使用gpu版的tensorflow_第3张图片

安装完成后,重新导入,出现新的报错

>>> import tensorflow as tf
2022-10-11 11:12:12.428210: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
>>>

提示找不到cudart64_100.dll这个cuda运行时的库文件,推测是cuda11相对tensorflow2.0版本来说高了。一个讨巧的版本是找到cudart64_110.dll文件,将其复制,并改名,可以去掉错误,但这种方法后续使用时存在隐患。

win10上使用gpu版的tensorflow_第4张图片

改名后,再次导入,没有错误,可以正常打印出版本信息

win10上使用gpu版的tensorflow_第5张图片
至此,初步完成安装。

你可能感兴趣的:(cuda,tensorflow,python,深度学习)