Printing a tensor’s shape in Theano

Posted on January 3, 2016. Filed under: Uncategorized | Tags: , |

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 )

Recently on Andre Holzner's Blog…

An illustration of Minuit minimizing a function

Posted on February 11, 2014. Filed under: Uncategorized | Tags: , , , |

A world map of particle physics

Posted on July 7, 2013. Filed under: Uncategorized | Tags: , |

RooFitExplorer

Posted on October 13, 2012. Filed under: Uncategorized | Tags: , , , , , , |

A list of follow-ups on the recent CNGS/Opera result on the neutrino velocity

Posted on September 28, 2011. Filed under: Uncategorized | Tags: , , , , , , |

A TMVA example in pyROOT

Posted on August 27, 2011. Filed under: Uncategorized | Tags: , , , , , , |

Debugging unresponsive Java GUI applications

Posted on June 25, 2011. Filed under: Uncategorized | Tags: , , , , , , , , |

Facebook Hacker Cup 2011 Round 2 Problem 3 – some discussion

Posted on February 8, 2011. Filed under: Uncategorized | Tags: , , , , , , , , , , , , , |

Facebook Hacker Cup 2011 Round 2 Problem 1 – some discussion

Posted on February 7, 2011. Filed under: Uncategorized | Tags: , , , , , , , , , , , |

Using Jython as a configuration language for Java programs

Posted on November 26, 2010. Filed under: Uncategorized | Tags: , , , , |

Liked it here?
Why not try sites on the blogroll...