Category Archives: Deep Learning

Configure Theano & CUDA for Deep Learning on a Mac

STEP 1 – Install Theano
After installing Anaconda run the following command in Terminal

$ conda install theano pygpu

STEP 2 – Install the correct CUDA driver based on your model of Mac
Browse to this link to download the correct version of the CUDA driver for your Mac. Make sure to click on the driver link and choose the Supported Products tab to determine the Mac hardware that the particular driver supports.

For some older Macs, the version 6.5.45 driver is the best choice.

STEP 3 – Install the CUDA toolkit, but don’t upgrade the driver
Download the right version of the CUDA toolkit for your Mac from the archive here.

For some older Macs, version 6.5 toolkit is the best choice

STEP 4 – Install XCode
Download the XCode app from the Apple site and install it on your Mac

STEP 5 – Install XCode Command Line tools
Open a Terminal and run the following command

$ xcode-select --install

Choose to install the command line tools

STEP 6 – Check the cc compiler
Open a Terminal and run the following command

$ /usr/bin/cc --version


STEP 7 – Update your .bash_profile file
Add the following to .bash_profile

export LD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-6.5/lib/
export CUDA_ROOT=/Developer/NVIDIA/CUDA-6.5/
export THEANO_FLAGS='mode=FAST_RUN,device=gpu,floatX=float32'

export PATH=/Developer/NVIDIA/CUDA-6.5/bin${PATH:+:${PATH}}
export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-6.5/lib\


STEP 8 – Test the NVCC compiler
Run the following in Terminal

$ /Developer/NVIDIA/CUDA-6.0/bin/nvcc -V


STEP 9 – Switch to the Samples Directory 
Switch to Samples directory that were installed as part of the toolkit

cd /Developer/NVIDIA/CUDA-6.0/samples/


STEP 10 – Make the Samples
Run the below, one line at a time and make sure you don’t get any errors

make -C 0_Simple/vectorAdd
make -C 0_Simple/vectorAddDrv
make -C 1_Utilities/deviceQuery
make -C 1_Utilities/bandwidthTest


STEP 11 – Run the Samples
Switch to the relevant directory to run the compiled files

cd /Developer/NVIDIA/CUDA-6.5/samples/bin/x86_64/darwin/release

Make sure you get the relevant output when running the below line by line



STEP 12 – Configure Theano to use the GPU
Create a file called .theanorc in your HOME directory and add the following to it

ldflags =

floatX = float32
device = gpu

fastmath = True

cxxflags = -ID:\MinGW\include

# Set to where the cuda drivers are installed.


STEP 13 – Run the following to confirm that Theano now uses the GPU

from theano import function, config, shared, tensor
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([], tensor.exp(x))
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
    print('Used the gpu')