win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)

win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)

第一步:安装anaconda3,选择window版64位下载
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第1张图片
安装界面,选择路径:
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第2张图片
选择第一个框勾选,python待会我们另外安装。
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第3张图片
第二步:搭建tensorflow的python3.5的虚拟环境,打开命令行cmd,直接在用户目录下搭建:

  • 搭建环境(中间步骤yes/no选yes)
conda create -n tensorflow pip python=3.5
  • 激活环境
activate tensorflow
  • 安装gpu版的tensorflow(在环境激活之后,前面有个tensorflow)
pip install --ignore-installed --upgrade tensorflow-gpu

第三步:在虚拟的环境tensorflow里安装你需要的软件,比如jupyter notebook(注意要在tensorflow的环境下)

conda install jupyter notebook

第四步:安装cuda+配置cudnn

一.安装cuda

  1. 首先确定你的显卡所需要的cuda和cudnn的版本:
    在你的tensorflow的安装目录下的".conda"里面,具体路径是(.conda\envs\tensorflow\Lib\site-packages\tensorflow\python\platform\build_info.py),打开build_info.py,如图所示你可以得到相应的cuda和cudnn的版本。
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第4张图片

  2. 下载cuda和cudnn。
    a. 下载cuda,我对应的版本是10.0,下载链接
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第5张图片
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第6张图片
    b.下载cudnn,我对应的版本是第7版,下载链接
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第7张图片

    图中有好多个cuda10.0对应的cudnn,选最前面那个(其他对应的应该也行),这里需要用邮箱去注册一个账号才可以下载。

  3. 安装cuda
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第8张图片
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第9张图片
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第10张图片
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第11张图片
    win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第12张图片

    安装位置记录一下(一般默认就好)

二.配置cudnn

cudnn是不要安装的,只需要将解压后的三个文件放到cuda的安装目录(默认是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0)就好,注意,我这里是v10.0,换成自己安装的cuda的版本号。

在这里插入图片描述

第五步:环境变量的设置(计算机右键—>属性—>高级系统设置—>环境变量—>系统变量—>path—>编辑—>新建)

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp

如果你默认安装的话只需要改版本号,不然把上面代码路径换成你自己的cuda的安装路径

第六步:测试

1.打开jupyter notebook
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第13张图片

2.新建一个python3的文件:

from __future__ import print_function
'''
Basic Multi GPU computation example using TensorFlow library.
Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

'''
This tutorial requires your machine to have 1 GPU
"/cpu:0": The CPU of your machine.
"/gpu:0": The first GPU of your machine
'''

import numpy as np
import tensorflow as tf
import datetime

# Processing Units logs
log_device_placement = True

# Num of multiplications to perform
n = 10

'''
Example: compute A^n + B^n on 2 GPUs
Results on 8 cores with 2 GTX-980:
 * Single GPU computation time: 0:00:11.277449
 * Multi GPU computation time: 0:00:07.131701
'''
# Create random large matrix
A = np.random.rand(10000, 10000).astype('float32')
B = np.random.rand(10000, 10000).astype('float32')

# Create a graph to store results
c1 = []
c2 = []

def matpow(M, n):
    if n < 1: #Abstract cases where n < 1
        return M
    else:
        return tf.matmul(M, matpow(M, n-1))

'''
Single GPU computing
'''
with tf.device('/gpu:0'):
    a = tf.placeholder(tf.float32, [10000, 10000])
    b = tf.placeholder(tf.float32, [10000, 10000])
    # Compute A^n and B^n and store results in c1
    c1.append(matpow(a, n))
    c1.append(matpow(b, n))

with tf.device('/cpu:0'):
  sum = tf.add_n(c1) #Addition of all elements in c1, i.e. A^n + B^n

t1_1 = datetime.datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
    # Run the op.
    sess.run(sum, {a:A, b:B})
t2_1 = datetime.datetime.now()

print("Single GPU computation time: " + str(t2_1-t1_1))

打印出运行时间表示配置成功:

Single GPU computation time: 0:00:17.804514

提醒:有可能出现运行错误,显示cuda的运行版本和驱动版本不匹配

CUDA driver version is insufficient for CUDA runtime version

可以使用在命令行里运行nvidia-smi命令去查看驱动版本(Driver Version):
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第14张图片

这一步可能报错:
在这里插入图片描述

需要将C:\Program Files\NVIDIA Corporation\NVSMI添加到path路径中

下面是cuda驱动版本和运行版本之间的关系:
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第15张图片

比如我的驱动版本是436.15,运行版本是cuda10.0,满足>=411.31,如果发现不满足,则要去NVIDIA官网去下载更新的驱动(一般可以找你的显卡当前版本最新的下载安装),下载地址

步骤如下:
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第16张图片
win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建)_第17张图片

安装就不再演示了,安装默认选项安装就好。

你可能感兴趣的:(win10 基于GPU的anaconda3+tensorflow深度学习环境搭建(简易搭建))