theano – print value of TensorVariable

Question:

How can I print the numerical value of a theano TensorVariable?
I’m new to theano, so please be patient 🙂

I have a function where I get y as a parameter.
Now I want to debug-print the shape of this y to the console.
Using

print y.shape

results in the console output (i was expecting numbers, i.e. (2,4,4)):

Shape.0

Or how can I print the numerical result of for example the following code (this counts how many values in y are bigger than half the maximum):

errorCount = T.sum(T.gt(T.abs_(y),T.max(y)/2.0))

errorCount should be a single number because T.sum sums up all the values.
But using

print errCount

gives me (expected something like 134):

Sum.0
Asked By: Stefan Profanter

||

Answers:

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(...)})
Answered By: nouiz

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.

Answered By: zuuz

print Value of a Tensor Variable.

Do the following:

print tensor[dimension].eval() # this will print the content/value at that position in the Tensor

Example, for a 1 d tensor:

print tensor[0].eval()
Answered By: Chandan Maruthi

Use theano.printing.Print to add print operator to your computational graph.

Example:

import numpy
import theano

x = theano.tensor.dvector('x')

x_printed = theano.printing.Print('this is a very important value')(x)

f = theano.function([x], x * 5)
f_with_print = theano.function([x], x_printed * 5)

#this runs the graph without any printing
assert numpy.all( f([1, 2, 3]) == [5, 10, 15])

#this runs the graph with the message, and value printed
assert numpy.all( f_with_print([1, 2, 3]) == [5, 10, 15])

Output:

this is a very important value __str__ = [ 1. 2. 3.]

Source: Theano 1.0 docs: “How do I Print an Intermediate Value in a Function?”

Answered By: Nicolas Ivanov

I found @zuuz ‘s answer is pretty helpful,
for values,

print(your_variable.tag.test_value)

for shapes it should be updated as,

print(np.shape(your_variable.tag.test_value))
Answered By: TracyYXChen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.