### Theano

时间:2024-08-31 09:05:50

Theano.

#@author:       gr
#@date: 2014-07-02
#@email: forgerui@gmail.com

一、安装Theano

ubuntu下安装相对简单。

安装依赖:

sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ git libatlas3gf-base libatlas-dev

安装theano:

sudo pip install Theano

测试安装是否成功:

$ python
>>> import theano
>>> theano.test()

二、用GPU加速

神经网络需要大量的计算,利用cuda可以进行有效的加速。

可以使用如下脚本进行测试gpu, 保存为check1.py:

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 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print 'Used the cpu'
else:
print 'Used the gpu'

运行时分别使用cpu、gpu测试:

$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py
[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]
Looping 1000 times took 3.06635117531 seconds
Result is [ 1.23178029 1.61879337 1.52278066 ..., 2.20771813 2.29967761
1.62323284]
Used the cpu $ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python check1.py
Using gpu device 0: GeForce GTX 580
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.638810873032 seconds
Result is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
1.62323296]
Used the gpu

我在本机上测试,平均速度要快5倍左右。

三、实例分析LeNet

LeNet是Y. LeCun设计的一种卷积神经网络。我们可以使用这个深度学习的教程,代码在GitHub上

Reference

  1. http://deeplearning.net/software/theano/install_ubuntu.html
  2. http://deeplearning.net/software/theano/tutorial/using_gpu.html
  3. http://deeplearning.net/tutorial/contents.html
  4. http://deeplearning.net/tutorial/lenet.html