## Printing a tensor’s shape in Theano

While developing code with Theano I got exceptions that the shapes of the quantities given to an operator do not match.

Searching around, one finds examples how to print the values of the elements of tensors (e.g. here) but not necessarily their shape:

from theano import tensor as T, function, printing x = T.dvector() printing_op = printing.Print('vector') printed_x = printing_op(x) f = function([x], printed_x) result = f([1, 2, 3])

which yields:

vector __str__ = [ 1. 2. 3.]

While this works nicely for small and few dimensions, inferring the shape from a printout of a large tensor is tedious…

Just printing x.shape where x is a Theano tensor does not give the information we look for. Consider the following example:

from theano import tensor as T, function, printing x = T.dvector() def myfunc(x): print "x.shape=",x.shape return x f = function([x], myfunc(x)) result = f([1, 2, 3])

which prints the following:

x.shape= Shape.0

which is usually not what we want.

It turns out however that `theano.printing.Print(..)`

can print attributes of a tensor when given a list of attributes to be printed to the `attrs`

parameter of its constructor. To print the shape we can therefore do:

from theano import tensor as T, function, printing x = T.dvector() printing_op = printing.Print('vector', attrs = [ 'shape' ]) printed_x = printing_op(x) f = function([x], printed_x) result = f([1, 2, 3])

which then gives the desired printout:

vector shape = (3,)

This also works for other attributes like `min`

or `max`

(which can be useful to check the validity of the values in a tensor) etc.

(the above example code is admittedly inspired by examples found on deeplearning.net)

**Read Full Post**|

**Make a Comment**( None so far )