theano - print value of TensorVariable

For future readers: the previous answer is quite good. But, I found the 'tag.test_value' mechanism more beneficial for debugging purposes (see theano-debug-faq):

from theano import config
from theano import tensor as T
config.compute_test_value = 'raise'
import numpy as np    
#define a variable, and use the 'tag.test_value' option:
x = T.matrix('x')
x.tag.test_value = np.random.randint(100,size=(5,5))

#define how y is dependent on x:
y = x*x

#define how some other value (here 'errorCount') depends on y:
errorCount = T.sum(y)

#print the tag.test_value result for debug purposes!
errorCount.tag.test_value

For me, this is much more helpful; e.g., checking correct dimensions etc.


If y is a theano variable, y.shape will be a theano variable. so it is normal that

print y.shape

return:

Shape.0

If you want to evaluate the expression y.shape, you can do:

y.shape.eval()

if y.shape do not input to compute itself(it depend only on shared variable and constant). Otherwise, if y depend on the x Theano variable you can pass the inputs value like this:

y.shape.eval(x=numpy.random.rand(...))

this is the same thing for the sum. Theano graph are symbolic variable that do not do computation until you compile it with theano.function or call eval() on them.

EDIT: Per the docs, the syntax in newer versions of theano is

y.shape.eval({x: numpy.random.rand(...)})