Skip to main content

core.Selection

core.Selection(base, index)

Some entries of some parameters, each turned into its own 1-D parameter.

params holds the selected values under "<name>[idx]", and apply(model) scatters them back into the full tensors.

base holds live views of the parameters, so the entries left out follow the module when it changes. Training the module after fitting is caught by the posterior’s check_versions on outputs, predict and sample, since every module parameter is a fixed parameter of a posterior over a Selection.

A Selection reads only the tensors it was given.

Parameters

Name Type Description Default
base Params Each source name mapped to the full tensor, a detached view of the given parameter. required
index Mapping[str, Tensor] Each source name mapped to its selected flat (row-major) indices: sorted, unique, non-empty int64. required

Raises

Name Type Description
StructureError base and index have different names or order, nothing is selected, or an index is not 1-D int64, is empty, is not sorted and unique, or is out of range for its parameter.

Attributes

Name Description
params Selected name mapped to a copy of the selected entries, in base order.
selected_index Selected name mapped to its flat indices, keyed like params.

Methods

Name Description
apply model called with the selected parameters scattered back into the full tensors.
from_masks The entries where each bool mask is True.
masks The inverse of from_masks, for saving a selection.

apply

core.Selection.apply(model, /)

model called with the selected parameters scattered back into the full tensors.

Names without the suffix pass through unchanged. The scatter is out of place, so the result differentiates in the selected values under torch.func transforms.

Parameters

Name Type Description Default
model ModelFn The model over the full parameters. required

Returns

Name Type Description
ModelFn A model over the selected parameters (plus any unsuffixed ones), with model.module. When called, it raises StructureError when a suffixed name is not selected, a value has the wrong shape or dtype, or a parameter comes both whole and selected.

from_masks

core.Selection.from_masks(params, masks, /)

The entries where each bool mask is True.

Masks with no True are dropped.

Parameters

Name Type Description Default
params Params The parameters. The result follows their order, so the selected parameters stay grouped by module. required
masks Mapping[str, Tensor] Parameter name mapped to a bool mask shaped like its parameter. required

Returns

Name Type Description
Selection The selection.

Raises

Name Type Description
StructureError A mask names a parameter not in params, is not bool, has another shape than its parameter, or no mask selects an entry.

masks

core.Selection.masks()

The inverse of from_masks, for saving a selection.

Returns

Name Type Description
dict[str, Tensor] Source name mapped to a bool mask shaped like the full tensor.