Tensorflow 张量流:检查张量秩

Tensorflow 张量流:检查张量秩,tensorflow,rank,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。我做错了什么?秩14:0是返回的张量的名称,而不是它的值,您需要在会话中计算张量以获得实际值: with tf.Session() as sess:

我想检查张量的秩。下面是我的代码:

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。我做错了什么?

秩14:0
是返回的张量的名称,而不是它的值,您需要在会话中计算张量以获得实际值:

with tf.Session() as sess:
    sess.run(tf.rank(x))

请问为什么形状=()?应该是(2,3)吗?
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