Why does tensorflow.rank always return shape with null value

Viewed 221

Being a beginner to TensorFlow I couldn't get why does tensorflow.rank always return shape with null value?

This is what I am working on:

import tensorflow as tf
%tensorflow_version 2.x

list_2d = [[1,2,3,4],
             [5,6,7,8],
             [9,10,11,12]
]
tensor_2d = tf.Variable(list_2d)

print(tensor_2d.shape)
print(tf.rank(tensor_2d))

and the output is

(3, 4)
tf.Tensor(2, shape=(), dtype=int32)

So my question is what is this shape=() from tf.rank output?

I couldn't get much from here - https://www.tensorflow.org/api_docs/python/tf/rank

2 Answers

It's because the output of tf.rank() is a tensor itself. Instead of returning the rank as an integer, the output is a tensor with a single int32 value that represents the rank of the given input tensor. The shape () is the shape of the output value, not the input tensor (single value tensors have shape ()).

The returned value represents the number of required indices to uniquely address each element (in your case 2). And 2 is a 0-dim tensor which explains why shape is null. Refer to the documentation for more infos.

Related