Running code on a GPU

In this subsection, we will demonstrate the usage of a GPU with Theano and Tensorflow. As an example, we will benchmark the execution of a very simple matrix multiplication on the GPU and compare it to its running time on a CPU.

The code in this subsection requires the possession of a GPU. For learning purposes, it is possible to use the Amazon EC2 service ( to request a GPU-enabled instance.

The following code performs a simple matrix multiplication using Theano. We use the T.matrix function to initialize a two-dimensional array, and then we use the method to perform the matrix multiplication:

    from theano import function, config    import theano.tensor as T    import numpy as np import time ...

Get Advanced Python Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.