tf.shape | TensorFlow v2.16.1 (original) (raw)
tf.shape
Stay organized with collections Save and categorize content based on your preferences.
Returns a tensor containing the shape of the input tensor.
tf.shape(
input, out_type=None, name=None
)
Used in the notebooks
tf.shape returns a 1-D integer tensor representing the shape of input
. For a scalar input, the tensor returned has a shape of (0,) and its value is the empty vector (i.e. []).
For example:
tf.shape(1.)
<tf.Tensor: shape=(0,), dtype=int32, numpy=array([], dtype=int32)>
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)>
a = tf.keras.layers.Input((None, 10))
tf.shape(a)
<... shape=(3,) dtype=int32...>
In these cases, using tf.Tensor.shape will return more informative results.
a.shape
TensorShape([None, None, 10])
(The first None
represents the as yet unknown batch size.)
tf.shape and Tensor.shape should be identical in eager mode. Withintf.function or within a compat.v1 context, not all dimensions may be known until execution time. Hence, when defining custom layers and models for graph mode, prefer the dynamic tf.shape(x) over the static x.shape
.
Args | |
---|---|
input | A Tensor or SparseTensor. |
out_type | (Optional) The specified output type of the operation (int32 orint64). Defaults to tf.int32. (Note: there is an experimental flag, tf_shape_default_int64 that changes the default to tf.int64. This is an unsupported, experimental setting that causes known breakages.) |
name | A name for the operation (optional). |
Returns |
---|
A Tensor of type out_type. |