Shape

class Shape(*shape: int | None)[source]

Bases: object

Tensor shape with support for placeholder dimensions.

Parameters:

*shape (int | None) – dimensions of the tensor, either positive integers for fixed dimensions or none for unspecified dimensions.

Important

Scalar tensors (i.e. tensors with no dimensions) are unsupported, as are tensors with any dimension of size 0.

property bshape: tuple[int, ...]

Tensor shape, with placeholder dimensions set to unit length.

Returns:

broadcastable tensor shape.

Return type:

tuple[int | None, …]

coalesce(tensor: Tensor) tuple[Tensor, dict[str, int]][source]

Coalesces a tensor into a matrix, with placeholder dimensions first and fixed dimensions second.

For a tensor with \(V_1, \ldots, V_m\) placeholder dimensions and \(C_1, \ldots, C_n\) fixed dimensions, the output matrix will have a shape of \((V_1 \times \cdots \times V_m) \times (C_1 \times \cdots \times C_n)\), and dimensions of unit length will used if the tensor has no placeholder/fixed dimensions.

Parameters:

tensor (Tensor) – tensor to coalesce.

Returns:

tuple of the coalesced tensor and the

required shape information to revert it.

Return type:

tuple[Tensor, dict[str, int]]

compat(*shape: int) bool[source]

Tests if a shape is compatible with the specified constraints.

Parameters:

*shape (int | None) – dimensions of the tensor.

Returns:

if the shape is compatible.

Return type:

bool

disperse(tensor: Tensor, pragma: dict[str, int]) Tensor[source]

Disperses dimensions of a coalesced tensor to their original positions.

Parameters:
  • tensor (Tensor) – tensor to disperse.

  • pragma (dict[str, int]) – shape information to revert the tensor.

Returns:

dispersed tensor.

Return type:

Tensor

filled(*fill: int) tuple[int, ...][source]

Fills placeholder dimensions with specified values.

Returns:

shape with the placeholder dimensions filled.

Return type:

tuple[int, …]

property nconcrete: int

Number of fixed dimensions.

Returns:

number of concrete dimensions.

Return type:

int

property ndim: int

Number of dimensions specified by the shape.

Returns:

dimensionality of a compatible tensor.

Return type:

int

property nvirtual: int

Number of placeholder dimensions.

Returns:

number of virtual dimensions.

Return type:

int

property rshape: tuple[int | None, ...]

Tensor shape, including placeholder dimensions.

Returns:

raw tensor shape.

Return type:

tuple[int | None, …]

property size: int

Number of elements specified by the shape.

Returns:

minimal number of tensor elements.

Return type:

int