| View source on GitHub | 
Represents the shape of a Tensor.
Inherits From: TraceType
tf.TensorShape(
    dims
)
  A TensorShape represents a possibly-partial shape specification for a Tensor. It may be one of the following:
TensorShape([16, 256])
TensorShape([None, 256])
TensorShape(None)
If a tensor is produced by an operation of type "Foo", its shape may be inferred if there is a registered shape function for "Foo". See Shape functions for details of shape functions and how to register them. Alternatively, you may set the shape explicitly using tf.Tensor.set_shape.
| Args | |
|---|---|
| dims | A list of Dimensions, or None if the shape is unspecified. | 
| Raises | |
|---|---|
| TypeError | If dims cannot be converted to a list of dimensions. | 
| Attributes | |
|---|---|
| dims | Deprecated. Returns list of dimensions for this shape. Suggest  | 
| ndims | Deprecated accessor for rank. | 
| rank | Returns the rank of this shape, or None if it is unspecified. | 
as_listas_list()
Returns a list of integers or None for each dimension.
| Returns | |
|---|---|
| A list of integers or Nonefor each dimension. | 
| Raises | |
|---|---|
| ValueError | If selfis an unknown shape with an unknown rank. | 
as_protoas_proto()
Returns this shape as a TensorShapeProto.
assert_has_rank
assert_has_rank(
    rank
)
 Raises an exception if self is not compatible with the given rank.
| Args | |
|---|---|
| rank | An integer. | 
| Raises | |
|---|---|
| ValueError | If selfdoes not represent a shape with the givenrank. | 
assert_is_compatible_with
assert_is_compatible_with(
    other
)
 Raises exception if self and other do not represent the same shape.
This method can be used to assert that there exists a shape that both self and other represent.
| Args | |
|---|---|
| other | Another TensorShape. | 
| Raises | |
|---|---|
| ValueError | If selfandotherdo not represent the same shape. | 
assert_is_fully_definedassert_is_fully_defined()
Raises an exception if self is not fully defined in every dimension.
| Raises | |
|---|---|
| ValueError | If selfdoes not have a known value for every dimension. | 
assert_same_rank
assert_same_rank(
    other
)
 Raises an exception if self and other do not have compatible ranks.
| Args | |
|---|---|
| other | Another TensorShape. | 
| Raises | |
|---|---|
| ValueError | If selfandotherdo not represent shapes with the same rank. | 
concatenate
concatenate(
    other
)
 Returns the concatenation of the dimension in self and other.
Note: If eitherselforotheris completely unknown, concatenation will discard information about the other shape. In future, we might support concatenation that preserves this information for use with slicing.
| Args | |
|---|---|
| other | Another TensorShape. | 
| Returns | |
|---|---|
| A TensorShapewhose dimensions are the concatenation of the dimensions inselfandother. | 
is_compatible_with
is_compatible_with(
    other
)
 Returns True iff self is compatible with other.
Two possibly-partially-defined shapes are compatible if there exists a fully-defined shape that both shapes can represent. Thus, compatibility allows the shape inference code to reason about partially-defined shapes. For example:
TensorShape(None) is compatible with all shapes.
TensorShape([None, None]) is compatible with all two-dimensional shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is not compatible with, for example, TensorShape([None]) or TensorShape([None, None, None]).
TensorShape([32, None]) is compatible with all two-dimensional shapes with size 32 in the 0th dimension, and also TensorShape([None, None]) and TensorShape(None). It is not compatible with, for example, TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).
TensorShape([32, 784]) is compatible with itself, and also TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None, None]) and TensorShape(None). It is not compatible with, for example, TensorShape([32, 1, 784]) or TensorShape([None]).
The compatibility relation is reflexive and symmetric, but not transitive. For example, TensorShape([32, 784]) is compatible with TensorShape(None), and TensorShape(None) is compatible with TensorShape([4, 4]), but TensorShape([32, 784]) is not compatible with TensorShape([4, 4]).
| Args | |
|---|---|
| other | Another TensorShape. | 
| Returns | |
|---|---|
| True iff selfis compatible withother. | 
is_fully_definedis_fully_defined()
Returns True iff self is fully defined in every dimension.
is_subtype_of
is_subtype_of(
    other: tf.types.experimental.TraceType
) -> bool
 Returns True iff self is subtype of other.
Shape A is a subtype of shape B if shape B can successfully represent it:
A TensorShape of any rank is a subtype of TensorShape(None).
TensorShapes of equal ranks are covariant, i.e. TensorShape([A1, A2, ..]) is a subtype of TensorShape([B1, B2, ..]) iff An is a subtype of Bn.
An is subtype of Bn iff An == Bn or Bn is None.
TensorShapes of different defined ranks have no subtyping relation.
The subtyping relation is reflexive and transitive, but not symmetric.
TensorShape([32, 784]) is a subtype of TensorShape(None), and TensorShape([4, 4]) is also a subtype of TensorShape(None) but TensorShape([32, 784]) and TensorShape([4, 4]) are not subtypes of each other.
All two-dimensional shapes are subtypes of TensorShape([None, None]), such as TensorShape([32, 784]). There is no subtype relationship with, for example, TensorShape([None]) or TensorShape([None, None, None]).
TensorShape([32, None]) is also a subtype of TensorShape([None, None]) and TensorShape(None). It is not a subtype of, for example, TensorShape([32]), TensorShape([32, None, 1]), TensorShape([64, None]) or TensorShape([None, 32]).
TensorShape([32, 784]) is a subtype of itself, and also TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None, None]) and TensorShape(None). It has no subtype relation with, for example, TensorShape([32, 1, 784]) or TensorShape([None]).
| Args | |
|---|---|
| other | Another TensorShape. | 
| Returns | |
|---|---|
| True iff selfis subtype ofother. | 
merge_with
merge_with(
    other
)
 Returns a TensorShape combining the information in self and other.
The dimensions in self and other are merged element-wise, according to the rules below:
Dimension(n).merge_with(Dimension(None)) == Dimension(n) Dimension(None).merge_with(Dimension(n)) == Dimension(n) Dimension(None).merge_with(Dimension(None)) == Dimension(None) # raises ValueError for n != m Dimension(n).merge_with(Dimension(m))
ts = tf.TensorShape([1,2]) ot1 = tf.TensorShape([1,2]) ts.merge_with(ot).as_list() [1,2]
ot2 = tf.TensorShape([1,None]) ts.merge_with(ot2).as_list() [1,2]
ot3 = tf.TensorShape([None, None]) ot3.merge_with(ot2).as_list() [1, None]
| Args | |
|---|---|
| other | Another TensorShape. | 
| Returns | |
|---|---|
| A TensorShapecontaining the combined information ofselfandother. | 
| Raises | |
|---|---|
| ValueError | If selfandotherare not compatible. | 
most_specific_common_supertype
most_specific_common_supertype(
    others: Sequence[tf.types.experimental.TraceType]
) -> Optional['TensorShape']
 Returns the most specific supertype TensorShape of self and others.
TensorShape([None, 1]) is the most specific TensorShape supertyping both TensorShape([2, 1]) and TensorShape([5, 1]). Note that TensorShape(None) is also a supertype but it is not "most specific".
TensorShape([1, 2, 3]) is the most specific TensorShape supertyping both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are other less specific TensorShapes that supertype above mentioned TensorShapes, e.g. TensorShape([1, 2, None]), TensorShape(None).
TensorShape([None, None]) is the most specific TensorShape supertyping both TensorShape([2, None]) and TensorShape([None, 3]). As always, TensorShape(None) is also a supertype but not the most specific one.
TensorShape(None) is the only TensorShape supertyping both TensorShape([1, 2, 3]) and TensorShape([1, 2]). In general, any two shapes that have different ranks will only have TensorShape(None) as a common supertype.
TensorShape(None) is the only TensorShape supertyping both TensorShape([1, 2, 3]) and TensorShape(None). In general, the common supertype of any shape with TensorShape(None) is TensorShape(None).
| Args | |
|---|---|
| others | Sequence of TensorShape. | 
| Returns | |
|---|---|
| A TensorShapewhich is the most specific supertype shape ofselfandothers. None if it does not exist. | 
most_specific_compatible_shape
most_specific_compatible_shape(
    other
)
 Returns the most specific TensorShape compatible with self and other.
TensorShape([None, 1]) is the most specific TensorShape compatible with both TensorShape([2, 1]) and TensorShape([5, 1]). Note that TensorShape(None) is also compatible with above mentioned TensorShapes.
TensorShape([1, 2, 3]) is the most specific TensorShape compatible with both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are more less specific TensorShapes compatible with above mentioned TensorShapes, e.g. TensorShape([1, 2, None]), TensorShape(None).
| Args | |
|---|---|
| other | Another TensorShape. | 
| Returns | |
|---|---|
| A TensorShapewhich is the most specific compatible shape ofselfandother. | 
num_elementsnum_elements()
Returns the total number of elements, or none for incomplete shapes.
with_rank
with_rank(
    rank
)
 Returns a shape based on self with the given rank.
This method promotes a completely unknown shape to one with a known rank.
| Args | |
|---|---|
| rank | An integer. | 
| Returns | |
|---|---|
| A shape that is at least as specific as selfwith the given rank. | 
| Raises | |
|---|---|
| ValueError | If selfdoes not represent a shape with the givenrank. | 
with_rank_at_least
with_rank_at_least(
    rank
)
 Returns a shape based on self with at least the given rank.
| Args | |
|---|---|
| rank | An integer. | 
| Returns | |
|---|---|
| A shape that is at least as specific as selfwith at least the given rank. | 
| Raises | |
|---|---|
| ValueError | If selfdoes not represent a shape with at least the givenrank. | 
with_rank_at_most
with_rank_at_most(
    rank
)
 Returns a shape based on self with at most the given rank.
| Args | |
|---|---|
| rank | An integer. | 
| Returns | |
|---|---|
| A shape that is at least as specific as selfwith at most the given rank. | 
| Raises | |
|---|---|
| ValueError | If selfdoes not represent a shape with at most the givenrank. | 
__add__
__add__(
    other
)
 __bool____bool__()
Returns True if this shape contains non-zero information.
__concat__
__concat__(
    other
)
 __eq__
__eq__(
    other
)
 Returns True if self is equivalent to other.
It first tries to convert other to TensorShape. TypeError is thrown when the conversion fails. Otherwise, it compares each element in the TensorShape dimensions.
>>> t_a = tf.TensorShape([1,2]) >>> a = [1, 2] >>> t_b = tf.TensorShape([1,2]) >>> t_c = tf.TensorShape([1,2,3]) >>> t_a.__eq__(a) True >>> t_a.__eq__(t_b) True >>> t_a.__eq__(t_c) False
>>> p_a = tf.TensorShape([1,None]) >>> p_b = tf.TensorShape([1,None]) >>> p_c = tf.TensorShape([2,None]) >>> p_a.__eq__(p_b) True >>> t_a.__eq__(p_a) False >>> p_a.__eq__(p_c) False
>>> unk_a = tf.TensorShape(None) >>> unk_b = tf.TensorShape(None) >>> unk_a.__eq__(unk_b) True >>> unk_a.__eq__(t_a) False
| Args | |
|---|---|
| other | A TensorShapeor type that can be converted toTensorShape. | 
| Returns | |
|---|---|
| True if the dimensions are all equal. | 
| Raises | |
|---|---|
| TypeError if othercan not be converted toTensorShape. | 
__getitem__
__getitem__(
    key
)
 Returns the value of a dimension or a shape, depending on the key.
| Args | |
|---|---|
| key | If keyis an integer, returns the dimension at that index; otherwise ifkeyis a slice, returns a TensorShape whose dimensions are those selected by the slice fromself. | 
| Returns | |
|---|---|
| An integer if keyis an integer, or aTensorShapeifkeyis a slice. | 
| Raises | |
|---|---|
| ValueError | If keyis a slice andselfis completely unknown and the step is set. | 
__iter____iter__()
Returns self.dims if the rank is known, otherwise raises ValueError.
__len____len__()
Returns the rank of this shape, or raises ValueError if unspecified.
__nonzero____nonzero__()
Returns True if this shape contains non-zero information.
__radd__
__radd__(
    other
)
  
    © 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
    https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/TensorShape