tf.shape

TensorFlow 1 version View source on GitHub

Returns the shape of a tensor.

See also tf.size.

This operation returns a 1-D integer tensor representing the shape of input. This represents the minimal set of known information at definition time.

For example:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.shape(t)
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>
tf.shape(t).numpy()
array([2, 2, 3], dtype=int32)
a = tf.keras.layers.Input((None, 10))
tf.shape(a)
<tf.Tensor ... shape=(3,) dtype=int32>

In these cases, using tf.Tensor.shape will return more informative results.

a.shape
TensorShape([None, None, 10])

tf.shape and Tensor.shape should be identical in eager mode. Within tf.function or within a compat.v1 context, not all dimensions may be known until execution time.

input A Tensor or SparseTensor.
out_type (Optional) The specified output type of the operation (int32 or int64). Defaults to tf.int32.
name A name for the operation (optional).

A Tensor of type out_type.