Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

python - theano - print value of TensorVariable

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
question from:https://stackoverflow.com/questions/17445280/theano-print-value-of-tensorvariable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(...)})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...