tensorflow:检查张量等级

时间:2017-09-27 14:19:33

标签: tensorflow rank

我想检查一下张量等级。这是我的代码:

import tensorflow as tf
x = tf.constant([[0,1,0], [0,1,0]])
print(tf.rank(x))

返回

Tensor("Rank_14:0", shape=(), dtype=int32)

Rank_14:0继续增长。 我希望它能回归2.我做错了什么?

1 个答案:

答案 0 :(得分:1)

Rank_14:0是返回张量的名称而不是它的值,您需要在Session中评估张量以获得实际值:

with tf.Session() as sess:
    sess.run(tf.rank(x))
import tensorflow as tf
x = tf.constant([[0,1,0], [0,1,0]])
r = tf.rank(x)

sess = tf.InteractiveSession()
​
print("My tensor is: ", r)
print("The value of my tensor is: ", r.eval())

My tensor is:  Tensor("Rank_3:0", shape=(), dtype=int32)
The value of my tensor is:  2