简体   繁体   English

如何在tensorflow中打印局部张量?

[英]How do I print a local tensor in tensorflow?

I want to print a tensor in my program to see its internal values once it gets evaluated. 我想在程序中打印张量,以便在对其求值后查看其内部值。 The problem, however, is that the tensor being declared inside a function. 但是,问题在于张量是在函数内部声明的。 To understand my problem better, here is some example code to better explain what it is I want to do: 为了更好地理解我的问题,下面是一些示例代码,以更好地解释我要执行的操作:

a = tf.Variable([[2,3,4], [5,6,7]])
b = tf.Variable([[1,2,2], [3,3,3]])

def divide(a,b):
    with tf.variable_scope('tfdiv', reuse=True):
        c = tf.divide(a,b, name='c')
    # Cannot print(c) here, as this will only yield tf info on c
    return c

d = divide(a,b)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(d)
    sess.run(tf.get_variable('tfdiv/c:0').eval(session=sess))

Previously, I have been able to do a print(c.eval(session=sess)), but as c is a local variable inside a function now, that does not work. 以前,我已经能够执行print(c.eval(session = sess)),但是由于c现在是函数内部的局部变量,因此无法正常工作。 As can be seen in the code above, I have tried to use tensorflow's variable scope in order to access the variable and then evaluate it. 从上面的代码中可以看出,我试图使用tensorflow的变量作用域来访问变量,然后对其求值。 Unfortunately, this results in the error message: 不幸的是,这会导致错误消息:

ValueError: Shape of a new variable (tfdiv/c:0) must be fully defined, but 
instead was <unknown>.

I tried to use the reuse=True flag, but I still get the same error. 我尝试使用reuse = True标志,但是仍然出现相同的错误。 Any thoughts on how I can solve this problem? 关于如何解决此问题有任何想法吗? Best would be if there is a print(c) equivalent that can be put into the divide function, as written in the code above. 最好的办法是,如上面的代码所示,是否可以将一个等效的print(c)放入除法函数中。

This will achieve what you want to do: 这将实现您想要执行的操作:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(d))

Alternatively, you could replace the last line with: 或者,您可以将最后一行替换为:

print(sess.run(tf.get_default_graph().get_tensor_by_name('tfdiv/c:0')))

It is important to understand the difference between Python side code and TensorFlow side code. 重要的是要了解Python辅助代码与TensorFlow辅助代码之间的区别。 In python, you only setup the graph: d = divide(a, b) creates something like: 在python中,您只需设置图形即可: d = divide(a, b)创建如下内容: 在此处输入图片说明

You set up a node (square) that will divide the data in the nodes a and b . 您设置了一个节点 (正方形),该数据将划分节点 ab的数据。 It doesn't divide them right away! 它不会立即将它们分开! Note that in black you have the python variable names, and in gray you have the TensorFlow node names 1 . 请注意,黑色为python变量名,灰色为TensorFlow节点名1 a and b also have some default names, if you didn't specify them. 如果未指定,则ab也具有一些默认名称。 The gray "c" you specified with name='c' . 您使用name='c'指定的灰色“ c”。 And local variable c and global variable d (Python) both refer to that same operation (node). 局部变量c和全局变量d (Python)都引用相同的操作(节点)。

This is why if you say print(d) merely prints the info about that node. 这就是为什么如果您说print(d)仅打印有关该节点的信息。 Once you setup the graph, doing sess.run(d) runs all the nodes required by the node in d on TensorFlow side . 设置完图形后,执行sess.run(d)运行TensorFlowd的节点所需的所有节点。 Then it retrieves the result and makes it available on python side as a numpy array. 然后,它检索结果并将其作为numpy数组在python端提供。

You can use tf.Print(input, data) to print tensors on TF side . 您可以使用tf.Print(input, data) 在TF side打印张量。 Note this is an operation (a node in the graph) that does nothing to the input tensor, it merely passes it through, while also printing everything in data . 请注意,这是对input张量不执行任何操作的操作(图中的一个节点),它只是将其传递,同时还打印了data所有内容。

In your case, you can use Print on tensorflow side it like this: 在您的情况下,您可以像这样在tensorflow端使用Print

def divide(a,b):
    with tf.variable_scope('tfdiv', reuse=True):
        c = tf.divide(a,b, name='c')
        cp = tf.Print(c, [c], message='Value of c: ', name='P')

    return cp

This effectively adds another node in the graph (named P on TF side): 这有效地在图中添加了另一个节点(在TF端命名为P ):

张量图说明上述情况

Now the value of operation c will be printed every time it will be evaluated. 现在,将在每次评估操作时打印操作 c的值。 Note it will also be printed every time one of its dependencies will be evaluated, for example if you later do e = d + 1 , when you evaluate e , it needs d , which refers to the printing node (returned from the function divide ). 请注意,每当它的一个依赖项被求值时,它也会被打印,例如,如果您以后执行e = d + 1 ,当您求e ,它需要d ,它指向打印节点(从函数divide返回) 。

Finally, note that if you do this in a Jupyter notebook, the print will appear in the terminal of the notebook server. 最后,请注意,如果您在Jupyter笔记本中执行此操作,则打印将出现在笔记本服务器的终端中。 The details of this are not important for now :). 目前,此细节并不重要:)。

1 the :0 is added by default so that you can retrieve any tensor with by using name_of_op:0 . 1默认情况下会添加:0 ,以便您可以使用name_of_op:0来检索任何张量。 Distinction between name of operation ( tfdiv/c ) and name of tensor( tfdiv/c:0 ). 操作名称( tfdiv/c )和张量名称( tfdiv/c:0 )之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM