如何在 TensorFlow 中获取张量的值(无需进行其他会话) [英] How to get a tensor's value in TensorFlow (without making another session)

查看:101
本文介绍了如何在 TensorFlow 中获取张量的值(无需进行其他会话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种获取张量值的方法.在大多数情况下,问题将通过调用sess.run(target_op)"来解决.不过,我想知道另一种方式.我正在编辑从 GitHub 下载的代码,所以那里已经有一个运行代码的会话.在不接触会话运行部分的情况下,有没有办法获得一些特定的张量值?就我而言,代码是为了获得图像识别的准确性而构建的.在会话运行并进行准确性评估时,我还希望在同一会话中获得预测"张量值,而无需创建另一个会话.例如,像 tf.Print 这样的操作通过终端窗口显示张量值,而无需直接运行 session(在第一个图中,我们只需要执行 sess.run(e) 从 c 打印出张量)tf.Print 示例

I'm finding a way to getting a tensor's value. In most case, the problem would be solved by calling "sess.run(target_op)". However, I want to know another way. I am editing the code downloaded from GitHub so there's already a session running code there. Without touching the session running part, is there any way to get some specific tensor value? In my case, the code is built for getting accuracy for image recognition. While session runs and doing the accuracy evaluation I also want to get "prediction" tensor value in the same session without creating another session. For example, an operation like tf.Print shows tensor value througha terminal window without running session directly(in the first figure we just have to do sess.run(e) to print out tensor from c) example of tf.Print

a = tf.constant(5)
b = tf.constant(3)
c = tf.add(a,b)

#print tensor c (which is 8)
d = tf.Print(c,[c])
f = tf.constant(2)
e = tf.multiply(f,d)
sess = tf.Session()

#print operation can be executed without running the session directly
g = sess.run(e)`

像 tf.Print 一样,有没有不直接运行 session 就可以获取张量值的操作?(像第二张图)我正在寻找的操作示例

Like the tf.Print is there any operation that gets tensor value without running session directly? (like the second figure) example of operation I am looking for

更具体地说,我想要的是获取张量的值(使用实际数字和数组,而不仅仅是张量"数据结构)并将其传递给全局变量以在会话关闭后自由访问该值.会话只执行位于图末尾的运算符,而我想要的张量值位于图的中间.由于限制我不能创建比原始代码更多的会话,有没有办法获得特定的张量值?(我不能使用 .eval() 或 .run() 因为两者都需要访问会话".我正在编辑的代码使用 slim.evaluate_once 函数运行代码,并且由于 session() 绑定到该函数,我无法接近 session())

More specifically, what I want is to get the value of tensor(with actual numbers and arrays, not just 'tensor' data structure)and pass it to the global variable to access the value freely even after the session closes. The session only executes the operator which is located at end of the graph while the tensor I want the value is located in the middle of the graph. With restriction that I cannot create more session than the original code has, is there any way to get the specific tensor value?( I can't use .eval() or .run() because either needs to access 'session'. the code I am editing runs the code by using slim.evaluate_once function and as session() is binded to the function, I cannot approach to session())

推荐答案

只要您输入适当的 feed_dict,就没有理由不从图中调用任何张量.例如,假设您想要一个名为 biasAdd:0 的张量,而您所谓的结束张量称为 prediction

There is no reason why you can't just call any tensor from the graph, provided you feed in the appropriate feed_dict. For instance say you want a tensor called biasAdd:0 and your so called-end tensor is called prediction

然后你就可以得到这个张量并对其进行评估:

Then you can just get this tensor and evaluate it:

tensor = graph.get_tensor_by_name("biasAdd:0")
tensor_value, prediction_value = ses.run([tensor, prediction],... )

在 tensorflow 中,您必须使用 run 或 eval 从图中获取数值

In tensorflow you have to use run or eval to get a numerical value from the graph

这篇关于如何在 TensorFlow 中获取张量的值(无需进行其他会话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆