[英]Tensorflow how to get tensor value
In python I can use 在python中我可以使用
a = np.array([[3], [6], [9]])
Obviously, 明显,
a[0][0] = 3
a[1][0] = 6
a[2][0] = 9
But I tried to do the same thing with tensorflow 但是我试图用张量流做同样的事情
import tensorflow as tf
a = tf.Variable(np.array([[3], [6], [9]]))
init = tf.initialize_all_variables()
with tf.Session() as ss:
ss.run(init)
for i in range(3):
print sess.run(a[i][0])
If I print it(use for loop), I got TypeError: 'Variable' object is not callable
如果我打印它(用于循环),我得到
TypeError: 'Variable' object is not callable
How can I resolve this error? 如何解决此错误? Thanks very much for any help!
非常感谢您的帮助!
You can define another op, that is dependent on the original variable, that contains the slice of your tensor: 您可以定义另一个op,该op取决于原始变量,其中包含张量的切片:
import tensorflow as tf
a = tf.Variable(np.array([[3], [6], [9]]))
part = []
for i in range(3):
part.append(a[i][0])
init = tf.initialize_all_variables()
with tf.Session() as ss:
ss.run(init)
for op in part:
print ss.run(op)
Despite tensorflow and numpy are quite similar at the first glance, tensorflow workflow substantially differs from numpy's. 尽管tensorflow和numpy乍一看非常相似,但tensorflow的工作流程与numpy的工作却大不相同。 When using tensorflow, you should first define the computational graph -- the rules defining the connections between tensors.
使用张量流时,您应该首先定义计算图-定义张量之间的连接的规则。
In your case, the graph consists of only one variable a
. 在您的情况下,图形仅包含一个变量
a
。 Once the graph was defined, you would be able to compute the values of different nodes in a graph by running a tensorflow session. 定义图后,您将可以通过运行张量流会话来计算图中不同节点的值。 In your case, to print a value of
a
, use the following code: 在你的情况下,打印的值
a
,使用下面的代码:
sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
print(sess.run(a))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.