Skip to main content

core.Layout

core.Layout(blocks, shapes)

Parameter names grouped into blocks, and each parameter’s shape.

Parameters

Name Type Description Default
blocks Mapping[str, tuple[str, …]] Block name mapped to its parameter names, in flat order. required
shapes Mapping[str, tuple[int, …]] Parameter name mapped to its shape. required

Attributes

Name Description
n_params Entries of every parameter in the layout (\(P\)).

Methods

Name Description
by_module One block per module prefix (everything before the last dot).
flatten The block’s flat vector.
flatten_all Every block’s flat vector.
restrict The layout of some blocks only.
size Length of a block’s flat vector.
unflatten Back to the block’s parameters, the inverse of flatten.

by_module

core.Layout.by_module(params, /)

One block per module prefix (everything before the last dot).

Names without a dot go to ROOT_BLOCK.

Parameters

Name Type Description Default
params Params Parameter mapping. Its order sets the block order and the order within each block. required

Returns

Name Type Description
Layout The layout, with each parameter’s shape.

flatten

core.Layout.flatten(params, block, /, *, lead=0)

The block’s flat vector.

Parameters

Name Type Description Default
params Params Parameter mapping holding the block’s parameters. required
block str Block name. required
lead int Number of leading axes of each parameter that stay in front (a batch of parameter sets). The vectors run along the last axis. 0

Returns

Name Type Description
Tensor A tensor of shape (*lead_shape, size(block)).

Raises

Name Type Description
KeyError block is not a block of this layout.

flatten_all

core.Layout.flatten_all(params, /, *, lead=0)

Every block’s flat vector.

Parameters

Name Type Description Default
params Params Parameter mapping holding every block’s parameters. required
lead int Leading axes that stay in front, as in flatten. 0

Returns

Name Type Description
Mapping[str, Tensor] Block name mapped to its flat vector.

restrict

core.Layout.restrict(blocks, /)

The layout of some blocks only.

Parameters

Name Type Description Default
blocks Iterable[str] Names of the blocks to keep, in the order the new layout lists them. required

Returns

Name Type Description
Layout A layout with those blocks and their parameters’ shapes.

Raises

Name Type Description
KeyError A name in blocks is not a block of this layout.

size

core.Layout.size(block, /)

Length of a block’s flat vector.

Parameters

Name Type Description Default
block str Block name. required

Returns

Name Type Description
int The number of entries in the block’s parameters.

Raises

Name Type Description
KeyError block is not a block of this layout.

unflatten

core.Layout.unflatten(vec, block, /)

Back to the block’s parameters, the inverse of flatten.

Parameters

Name Type Description Default
vec Tensor The block’s flat vector along the last axis. Leading axes (a batch) stay in front. required
block str Block name. required

Returns

Name Type Description
Params Parameter mapping of the block’s names, each shaped (*lead_shape, *shape).

Raises

Name Type Description
KeyError block is not a block of this layout.