Python theano - TensorVariable 的打印值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17445280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
theano - print value of TensorVariable
提问by Stefan Profanter
How can I print the numerical value of a theano TensorVariable?I'm new to theano, so please be patient :)
如何打印 theano TensorVariable 的数值?我是 theano 的新手,所以请耐心等待 :)
I have a function where I get yas a parameter.
Now I want to debug-print the shape of this yto the console.
Using
我有一个函数y作为参数。现在我想将它的形状调试打印y到控制台。使用
print y.shape
results in the console output (i was expecting numbers, i.e. (2,4,4)):
结果在控制台输出(我期待数字,即(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 yare bigger than half the maximum):
或者我如何打印例如以下代码的数值结果(这会计算有多少值y大于最大值的一半):
errorCount = T.sum(T.gt(T.abs_(y),T.max(y)/2.0))
errorCountshould be a single number because T.sumsums up all the values.
But using
errorCount应该是一个数字,因为T.sum总结了所有的值。但是使用
print errCount
gives me (expected something like 134):
给我(预期类似134):
Sum.0
采纳答案by nouiz
If y is a theano variable, y.shape will be a theano variable. so it is normal that
如果 y 是一个 theano 变量,则 y.shape 将是一个 theano 变量。所以这是正常的
print y.shape
return:
返回:
Shape.0
If you want to evaluate the expression y.shape, you can do:
如果要计算表达式 y.shape,可以执行以下操作:
y.shape.eval()
if y.shapedo not input to compute itself(it depend only on shared variable and constant). Otherwise, if ydepend on the xTheano variable you can pass the inputs value like this:
如果y.shape不输入计算自身(它仅依赖于共享变量和常量)。否则,如果y依赖于xTheano 变量,您可以像这样传递输入值:
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.functionor call eval()on them.
这对于sum. Theano 图是符号变量,在您使用它们编译theano.function或调用eval()它们之前不会进行计算。
EDIT:Per the docs, the syntax in newer versions of theano is
编辑:根据文档,theano 较新版本中的语法是
y.shape.eval({x: numpy.random.rand(...)})
回答by zuuz
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):
对于未来的读者:以前的答案非常好。但是,我发现 'tag.test_value' 机制对调试更有利(请参阅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.
对我来说,这更有帮助;例如,检查正确的尺寸等。
回答by Chandan Maruthi
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
print tensor[dimension].eval()# 这将打印张量中那个位置的内容/值
Example, for a 1 d tensor:
例如,对于 1 d 张量:
print tensor[0].eval()
回答by Nicolas Ivanov
Use theano.printing.Printto add print operator to your computational graph.
使用theano.printing.Print打印操作添加到您的计算图表。
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.]
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?”
回答by Tracy Chen
I found @zuuz 's answer is pretty helpful, for values,
我发现@zuuz 的回答对价值观很有帮助,
print(your_variable.tag.test_value)
for shapes it should be updated as,
对于形状,它应该更新为,
print(np.shape(your_variable.tag.test_value))

