Skip to content
fn

parse_shape

→dict of str to int
parse_shape(tensor: Tensor, pattern: str)

Read named axis lengths off the shape of tensor.

The result can be passed as the keyword lengths of the other entry points, so a length is written once.

Parameters

tensorTensor
The tensor whose shape is read.
patternstr
One name per dimension, _ for a dimension to skip, ... for any run of dimensions, 1 or () for a unit axis and a number for an axis of that length. Parenthesised groups of several axes are refused: a length cannot be read through a composition.

Returns

dict of str to int

Each named axis and its length.

Raises

lucid._C.engine.InvalidArgument
The pattern is malformed or composes axes.
lucid._C.engine.ShapeMismatch
The rank of tensor does not fit the pattern, or a unit or numbered axis is not the length the pattern writes.

Examples

>>> import lucid
>>> x = lucid.zeros(2, 3, 5, 7)
>>> lucid.einops.parse_shape(x, "batch _ h w")
{'batch': 2, 'h': 5, 'w': 7}
>>> y = lucid.zeros(70)
>>> lucid.einops.rearrange(y, "(c h w) -> c h w", **lucid.einops.parse_shape(x, "_ _ h w")).shape
(2, 5, 7)