tf.TensorShape

Represents the shape of a Tensor.

Used in the notebooks

Used in the guide Used in the tutorials

A TensorShape represents a possibly-partial shape specification for a Tensor. It may be one of the following:

  • Fully-known shape: has a known number of dimensions and a known size for each dimension. e.g. TensorShape([16, 256])
  • Partially-known shape: has a known number of dimensions, and an unknown size for one or more dimension. e.g. TensorShape([None, 256])
  • Unknown shape: has an unknown number of dimensions, and an unknown size in all dimensions. e.g. 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, the shape may be set explicitly using tf.Tensor.set_shape.

dims A list of Dimensions, or None if the shape is unspecified.

TypeError If dims cannot be converted to a list of dimensions.

dims Deprecated. Returns list of dimensions for this shape.

Suggest TensorShape.as_list instead.

ndims Deprecated accessor for rank.
rank Returns the rank of this shape, or None if it is unspecified.

Methods

as_list

View source

Returns a list of integers or None for each dimension.

Returns
A list of integers or None for each dimension.

Raises
ValueError If self is an unknown shape with an unknown rank.

as_proto

View source

Returns this shape as a TensorShapeProto.

assert_has_rank

View source

Raises an exception if self is not compatible with the given rank.

Args
rank An integer.

Raises
ValueError If self does not represent a shape with the given rank.

assert_is_compatible_with

View source

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 self and other do not represent the same shape.

assert_is_fully_defined

View source

Raises an exception if self is not fully defined in every dimension.

Raises
ValueError If self does not have a known value for every dimension.

assert_same_rank

View source

Raises an exception if self and other do not have compatible ranks.

Args
other Another TensorShape.

Raises
ValueError If self and other do not represent shapes with the same rank.

concatenate

View source

Returns the concatenation of the dimension in self and other.

Args
other Another TensorShape.

Returns
A TensorShape whose dimensions are the concatenation of the dimensions in self and other.

is_compatible_with

View source