Win10环境下安装GPU版本Tensorflow

配置成功的环境

Win10 x64 + GTX 960M + CUDA v10.0 + cuDNN v7.6.5 + Anancoda 3 (env python3.7) + tensorflow-gpu==1.14


Win10 x64 + GTX 960M

系统和硬件信息如图

CUDA v10.0

注意操作系统和CUDA的适配性,这是官方给的数据
在这里插入图片描述

这里安装的是CUDA10.0版本(下载链接),未安装最新10.2版本,将下载的cuda_10.0.130_411.31_win10.exe安装文件以默认方式安装(一路点击下一步),安装完成后重启下机器(不重启应该也行)。

CUDA安装成功后,在cmd窗口执行命令nvcc --version会打印GPU相关信息:


cuDNN v7.6.5

NVIDIA cuDNN是用于深度神经网络的GPU加速库,集成到Tensorflow等主流深度学习框架,用于加速计算。

官网下载与CUDA对应的cuDNN,下载需要账号登陆,可注册一个(下载链接
在这里插入图片描述
cuDNN包含三个文件cudnn.lib、cudnn.h和cudnn64_7.dll:

cuda
|__lib
|____x64
|______cudnn.lib
|__include
|____cudnn.h
|——bin
|____cudnn64_7.dll

将这三个文件复制到CUDA v10.0对应的目录下即可,默认目录为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0.


Anacoda 3 + python3.7虚拟环境

安装Anacoda 3(已安装忽略),安装成功后,打开新的cmd窗口,若无法识别conda指令,则需要将以下两个目录添加至path环境变量

  • C:\ProgramData\Anaconda3(安装根目录)
  • C:\ProgramData\Anaconda3\Scripts(脚本子目录)

使用conda指令创建python3.7版本虚拟环境并激活:

>  conda create -n tfgpu14 python=3.7
>  activate tfgpu14

tensorflow-gpu==1.14

tfgpu14虚拟环境内执行pip执行安装tensorflow-gpu版本

> pip install tensorflow-gpu==1.14

安装完成后,cmd窗口中输入python,进入python控制台,执行以下指令:

>>> import tensorflow as tf
>>> sess = tf.Session()

若tensorflow包可正常导入(numpy1.18会报警告信息,降为1.16警告信息就没了),且可建立Session,则环境搭建完成~

(tfgpu14) C:\Users\merlin>pip install numpy==1.16
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting numpy==1.16
  Downloading http://mirrors.aliyun.com/pypi/packages/dd/3e/0d7a914ee6cceef588dd83b18e257dc474ac67028a8d340dfec644878128/numpy-1.16.0-cp37-cp37m-win_amd64.whl (11.9 MB)
     |████████████████████████████████| 11.9 MB 6.8 MB/s
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 1.17.0
    Uninstalling numpy-1.17.0:
      Successfully uninstalled numpy-1.17.0
Successfully installed numpy-1.16.0

(tfgpu14) C:\Users\merlin>python
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> sess = tf.Session()
2020-05-01 02:45:27.273116: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2020-05-01 02:45:28.572762: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:02:00.0
2020-05-01 02:45:28.573391: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2020-05-01 02:45:28.576089: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2020-05-01 02:45:28.580038: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-05-01 02:45:28.586819: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:02:00.0
2020-05-01 02:45:28.587523: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2020-05-01 02:45:28.590432: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2020-05-01 02:45:30.804386: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-01 02:45:30.804939: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0
2020-05-01 02:45:30.805494: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N
2020-05-01 02:45:30.811922: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3045 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:02:00.0, compute capability: 5.0)
>>>

Tips

  • 第1次调用tensorflow session有点慢,加载Adding visible gpu devices: 0大概需要8分钟,之后会秒加载;
  • 安装tensorflow-gpu==2.0版本也可以,但2.1版本加载cuda_dll出错,可能需要高版本的CUDA;

猜你喜欢

转载自blog.csdn.net/sinat_34072381/article/details/105873218