Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run theano on GPU

If I run the following code with python 3.5

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(), ))

I get the output

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

it says if the times are close, it means that I am running on my CPU.

How can I run this code on my GPU?

NOTE:

  • I have a workstation with Nvidia Quadro k4200.
  • I have installed Cuda toolkit
  • I have successfully worked an cuda vectorAdd sample project on VS2012.
like image 773
babeyh Avatar asked Nov 04 '15 10:11

babeyh


People also ask

Can you run code on GPU?

Using the CUDA Toolkit you can accelerate your C or C++ applications by updating the computationally intensive portions of your code to run on GPUs. To accelerate your applications, you can call functions from drop-in libraries as well as develop custom applications using languages including C, C++, Fortran and Python.

What is the use of Theano?

Theano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays so efficiently. It is mostly used in building Deep Learning Projects. It works a way more faster on Graphics Processing Unit (GPU) rather than on CPU.


2 Answers

You configure Theano to use a GPU by specifying the device=gpu in Theano's config. There are two principle methods for setting the config: (1) in the THEANO_FLAGS environment variable, or (2) via the .theanorc file. Both methods, and all of Theano's configuration flags, are documented.

You will know that Theano is using the GPU if, after calling import theano you see a message that looks something like this

Using gpu device 0: GeForce GT 640 (CNMeM is disabled)

The details may vary for you but if no message appears at all then Theano is using the CPU only.

Note also that even if you see the GPU message, your particular computation graph may not run on the GPU. To see which parts of your computation are running on the GPU print its compiled and optimized graph

f = theano.function(...)
theano.printing.debugprint(f)

Operations that start with the prefix 'Gpu' will run on the GPU. Operations that do not have that prefix to their name will run on the CPU.

like image 98
Daniel Renshaw Avatar answered Sep 24 '22 14:09

Daniel Renshaw


If you are on Linux, create a .theanorc file in your home folder and add the following to set up theano to run on GPU.

[global]
device = gpu
floatx = float32
like image 32
Dherath Avatar answered Sep 20 '22 14:09

Dherath