theano环境配置

一、基本环境

1、64位win10系统

2、已安装visual studio 2017

3、可支持NVIDIA显卡

二、安装Anaconda

安装地址:https://www.continuum.io/downloads

theano环境配置

选择python3.6version下面的DOWNLOAD即可,我的是64位的。然后只需要按照步骤安装。

 theano环境配置

三、使用Anaconda清华镜像

由于之后要多次使用Anaconda中的conda命令下载文件,最好使用Anaconda镜像文件实现文件的快速下载,只需要在cmd(命令提示符)下直接输入以下两条即可:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

使用了镜像之后,我们就可以继续快速安装和配置了。

 

四、安装mingw

在你的Anaconda安装目录下应该没有MinGW,我的安装目录是D:\ProgramData\Anaconda3,我们直接用Anaconda的安装命令来安装:打开cmd(命令提示符),直接输入安装命令:

conda install mingw libpython

结果如下:

theano环境配置theano环境配置


五、安装theano

在cmd下输入以下pip指令,直接安装theano0.8.2。

pip install theano==0.8.2

目前我的版本是theano-0.8.2(0.9的版本容易报错,并且不容易调整过来)

 

六、检查环境变量

这个时候Anaconda的环境变量应该已经自动添加,以防万一,可以在检查一下:

theano环境配置theano环境配置

在“此电脑”(也就是我们常说的“我的电脑”)上面点击计算机,然后点击旁边的“属性”(就是有一个红色的勾的那个),左边点击“高级系统设置”,在弹出的“系统属性”下点击“高级”:

theano环境配置theano环境配置

找到下面的“环境变量”,点进去后再点击下面系统变量中的Path,检查有没有这两个路径:D:\ProgramData\Anaconda3和D:\ProgramData\Anaconda3\Scripts

theano环境配置theano环境配置

如果没有,点击右面的新建,加上这两条路径。

 

七、创建.theanorc.txt文件

在cmd的home目录中(即以下的路径,C:\Users\lenovo,你们的路径可能不一样)新建txt文件(文本文档文件),重命名为.theanorc.txt,在里面设置以下内容:

theano环境配置theano环境配置

[global]

openmp=False

[blas]

ldflags =

[gcc]

cxxflags = -ID:\ProgramData\Anaconda3\MinGW

以上那个路径是你安装好的MinGW路径,通常在Anaconda下面。

 

八、测试theano

好啦,我们来休息下,看看theano有没有安装成功:

先进入python命令行模式,直接在cmd中输入python,等到出现了“<<<”后,输入以下指令:

import theano

print(theano.config.blas.ldflags)

若没有返回值,说明安装成功,即:

theano环境配置theano环境配置

若有其他错误,可能是版本并不是我安装的以上版本,也可能是配置和我的不一样,具体问题可以在百度上找到答案,如果仍然不能解决可以在下面跟我留言。

 

九、安装CUDA

使用gpu给theano加速最终可以比只用cpu快上10倍以上,并且很多大的工程需要更多的数据,这时候gpu的应用就能决定theano深度学习的效率了,所以我们需要安装CUDA,进而实现gpu的使用。

首先,显卡一定是NVIDIA的,并且已经安装了visualstudio,在这个基础上,我们去他的官网上下载对应的软件支持:

https://developer.nvidia.com/cuda-toolkit 选择CUDA 8版本的安装(安装9RC后一直有些问题解决不了,又换了回来并最终通过了),选择适合的版本,文件有点大,应该是1.3GB左右:(我的是win10版本,x86_64位,本地下载)

 

在漫长的等待后,选择自定义安装,把所有都勾上,最终在cmd中输入以下指令验证安装成功:nvcc -V,会出现以下界面:

theano环境配置theano环境配置

 

之后,为了使cudnn能发挥作用,登陆这个网页:http://download.****.net/download/u012223520/9683325在****上下载cudnn8.0。大概有43MB大小左右,下载后将里面的三个文件夹复制粘贴覆盖C:\ProgramFiles\NVIDIA GPU Computing Toolkit\CUDA\v8.0中。

我们继续配置.theanorc文件,将文件最终配置为:

[global]

device = cuda

floatX = float32

openmp=False

 

allow_input_downcast=True

[lib]

cnmem = 0.3

[gcc]  
  
cxxflags=-ID:\ProgramData\Anaconda3\MinGW  (填MinGW对应路径)
  

[cuda] 

 

root=C:\Program Files\NVIDIA GPU ComputingToolkit\CUDA\v9.0\bin


[nvcc]  

 

fastmath = True

flags=-LD:\ProgramData\Anaconda3\libs  (填Anaconda下对应的libs路径)
  
-compiler_bindir=D:\Program Files\visual studio\VC\bin (填VS安装时对应的bin路径,前面有一个小短行)

这个时候开始在cmd的python命令行中输入以下指令:import theano.sandbox.cuda,若没有出现错误,说明一切安装正确。

容易出现以下问题:The cuda backend is deprecated and will beremoved in the next release (v0.10). Please switch to the gpuarray backend. Youcan get more information about how to switch at this URL:https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29解决方法是:直接使用conda 命令安装:conda installtheano pygpu,同时把theano的配置文件.theanorc.txt中device=gpu改成device=cuda or cuda0(此处来源http://www.jianshu.com/p/5d5b521c7382

十、测试能否使用gpu加速

 

from theanoimport function,config, shared, sandbox

import theano.tensoras T

import numpy

import time

 

vlen = 10*30*768 # 10 x cores x threads per core

iters = 1000

 

rng =numpy.random.RandomState(22)

x =shared(numpy.asarray(rng.rand(vlen), config.floatX))

f = function([],T.exp(x))

print(f.maker.fgraph.toposort())

t0 = time.time()

for iin range(iters):

    r = f()

t1 = time.time()

print('Looping %dtimes took' %iters, t1 - t0,'seconds')

print('Result is', r)

 

ifnumpy.any([isinstance(x.op, T.Elemwise)for xin f.maker.fgraph.toposort()]):

    print('Used the cpu')

else:

    print('Usedthe gpu')

最终会输出:Used the gpu,并有开销时间显示,我的电脑用了1.57s,将.theanorc.txt文件改成一下形式,即可以使用cpu,最终程序输出Used the cpu,用了17s左右。这可以很明显看出使用gpu的速度更快。

[blas]

ldflags =

[gcc]

cxxflags=-ID:\ProgramData\Anaconda3\MinGW 

 

附:最终的成功安装由于解决各种错误,有一点小小的区别:

1、       与theano有关的网上更新的依赖库下载了一些。

2、       增加了一个清华镜像。