Enable GPU for Theano

1. Install Theano

http://deeplearning.net/software/theano/install.html

 

2. Use the following script to test Theano can work at least in CPU mode:

'''
test whether theano is using cpu or gpu
'''
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 = 10000

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'

 

3. Install Cuda: 

Follow instructions here: http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/#axzz3l18NxDB5

You need to first install Cuda ToolKit, then `sudo apt-get update` and then `sudo apt-get install cuda`.

Also need to follow the post-installation steps: http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/index.html#post-installation-actions

 

4.  Add the following .theanorc file in your home directory:

[global]
floatX = float32
device = gpu0             # This enables GPU usage

[nvcc]
fastmath = True

http://deeplearning.net/software/theano/library/config.html

 

5.  Retry the script in 2.

Leave a comment

Your email address will not be published. Required fields are marked *