peal.sparse_dictionaries.orthogonal_procrustes_dictionary.OrthogonalProcrustesDictionary

class peal.sparse_dictionaries.orthogonal_procrustes_dictionary.OrthogonalProcrustesDictionary[source]

Bases: SparseDictionary

Label-supervised dictionary of class-mean directions in feature space.

fit solves min ||X W^T - Y|| subject to W W^T = I via the SVD of the cross-covariance Y^T X (or, with config.orthogonal=False, takes the normalised rows of that cross-covariance directly). fit_from_dataloaders extracts and centres the features first.

Parameters:

config (OrthogonalProcrustesDictionaryConfig) – Fit options; see the config class.

Variables:
  • W_ortho (torch.Tensor or None) – Fitted directions of shape [K, D] (rows are unit atoms).

  • mu (torch.Tensor or None) – Feature mean of shape [D] that was subtracted before fitting; edits project z - mu onto the atoms.

Methods

fit

Solves the Orthogonal Procrustes problem: Find W such that ||XW^T - Y|| is minimized subject to W @ W^T = I.

fit_from_dataloaders

Extract features for every dataloader, centre them and call fit.

get_components

Return the atoms as a [D, K] matrix (columns are directions).

load_from_disk

Restore W_ortho and mu from a file written by save_on_disk.

save_on_disk

Save W_ortho and mu with torch.save to path.

Attributes

config

__init__(config=pydantic.BaseModel)[source]

Store the config; W_ortho and mu stay None until fitted.

fit(X, y)[source]

Solves the Orthogonal Procrustes problem: Find W such that ||XW^T - Y|| is minimized subject to W @ W^T = I.

Parameters:
fit_from_dataloaders(dataloaders, feature_extractor)[source]

Extract features for every dataloader, centre them and call fit.

The dataset’s task_config is temporarily replaced by TaskConfig(**config.task) (or None) while iterating so the loader yields the attributes the dictionary should be supervised with, then restored. Features are collected on CPU; self.mu is set to their mean.

Parameters:
  • dataloaders (list of torch.utils.data.DataLoader) – Loaders yielding (inputs, targets) batches; all are concatenated.

  • feature_extractor (torch.nn.Module) – Encoder mapping inputs to [N, D] features; put into eval mode and run without gradients. Its parameters’ device is used for inference.

get_components()[source]

Return the atoms as a [D, K] matrix (columns are directions).

Returns:

W_ortho transposed to match the [D, K] layout of the other dictionaries, or None before fit was called.

Return type:

torch.Tensor or None

save_on_disk(path)[source]

Save W_ortho and mu with torch.save to path.

load_from_disk(path)[source]

Restore W_ortho and mu from a file written by save_on_disk.