码迷,mamicode.com
首页 > 其他好文 > 详细

Theano 学习笔记(1.搭环境 Anaconda + Theano + VS2010 + CUDA)

时间:2015-11-15 10:45:48      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:

最近几天开始接触深度学习,鉴于深度学习对速度和GPU计算的要求以及在网络层数不断加深后求导计算的复杂度不断增加,打算搭建一个Theano平台(抛弃Matlab),仅供自己娱乐下(花式灌水)。。。

主要步骤:

  1. Theano之CPU计算

  2. 搭建CUDA & VS2010

  3. Theano之GPU计算

1.Theano之CPU ONLY

安装Theano的条件:

  1. 需要安装Numpy,Scipy,Noise等等python的包。下载Anaconda(去官网下载这个http://www.continuum.io/downloads,很简单就能配置成功了,Anaconda集成了pip,numpy,scipy等等很多python科学计算的常用包)添加环境变量”path = H:\Anaconda2; H:\Anaconda2\Scripts; H:\Anaconda2\Library\bin;”改成自己的就可以了
  2. 需要gcc编译器。如果你的电脑中已经有了,就可以跳过这一步。如果你之前并没安装gcc编译器,可以自己去http://www.mingw.org/下载最新版,按照上面的步骤走就行了。但是最简单的办法是通过pip(可以用来管理安装python的包)来安装两个包,在cmd下输入conda install mingw libpython即可。添加环境变量”path = H:\Anaconda2\MinGW\bin;H:\Anaconda2\MinGW\x86_64-w64-mingw32\lib;”
  3. 连接gcc和theno。在home文件夹(C:/user/{your name})下新建一个文本文档,取名为.theanorc.txt,并在里面输入以下内容,blas是加速CPU计算,详情请见http://icl.cs.utk.edu/lapack-for-windows/lapack/,但是对于追求GPU多并行单位的人来说,可以跳过

[blas]
ldflags =

我是通过pip直接安装gcc编译器的,其他的方法需要多加几句:

[blas]
ldflags =

[gcc]
cxxflags = -I{your gcc address}

也就是告诉theano你的gcc编译器在哪里,如果通过pip安装mingw就不需要了。

之后,我们可以开心的安装Theano啦啦啦,只需要在cmd下输入pip install theano就行了。。。。

OK,最后在spyder下测试下,

import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices(‘XY‘)
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print "NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
                                           np_end-np_start, t_end-t_start)
print "Result difference: %f" % (np.abs(AB-tAB).max(), )

结果为

NP time: 0.675000[s], theano time: 0.535000[s] (times should be close when run on CPU!)
Result difference: 0.000000

2.搭建CUDA和VS2010

VS的安装:http://blog.sina.com.cn/s/blog_4f8cdc9e0100kklr.html (会不会被MS拉黑。。。)

CUDA的配置:http://blog.csdn.net/yeyang911/article/details/17450963

照着上面的步骤做就行了,在此强调一点,在安装CUDA之前必须要去NVIDIA官网上下载相应的官方驱动,别用驱动XX的驱动,容易出问题,我第一次安装就把显卡驱动弄没了。。。

还有就是怎么知道自己的显卡到底能不能用cuda并行计算框架呢,https://developer.nvidia.com/cuda-gpus告诉你。

3.Theano之GPU计算

在完成了以上的步骤后,剩下的就很简单了,把home文件夹(C:/user/{your name}).theanorc.txt的内容修改为:

[blas]
ldflags =

[nvcc]
fastmath = True
flags = -LH:\Anaconda2\libs
compiler_bindir = D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin

[global]
floatX = float32
device = gpu

flags和complier_binder改成你自己的地址就行了,然后test:

在Spyder下运行

from theano import function, config, shared, sandbox
import theano.tensor as 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 i in xrange(iters):
    r = f()
t1 = time.time()
print("It took %f seconds" % (t1 - t0))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print(‘Used the cpu‘)
else:
    print(‘Used the gpu‘)

以上所做的就是做很多次(10*30*768*1000)次exp计算,用了张量(Tensor)表示。

比较下我的电脑下CPU和GPU计算的差别

CPU:i7-4720HQ

[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
It took 55.188000 seconds
Used the cpu

GPU(GTX 960M)

[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
It took 1.817000 seconds
Used the gpu
Using gpu device 0: GeForce GTX 960M

速度竟然提高了30倍技术分享。。。。

呵呵,接下来该爽爽了

Theano 学习笔记(1.搭环境 Anaconda + Theano + VS2010 + CUDA)

标签:

原文地址:http://www.cnblogs.com/thkinglee/p/4966176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!