peal.sparse_dictionaries.orthogonal_procrustes_dictionary.OrthogonalProcrustesDictionary¶
- class peal.sparse_dictionaries.orthogonal_procrustes_dictionary.OrthogonalProcrustesDictionary[source]¶
Bases:
SparseDictionaryLabel-supervised dictionary of class-mean directions in feature space.
fitsolvesmin ||X W^T - Y||subject toW W^T = Ivia the SVD of the cross-covarianceY^T X(or, withconfig.orthogonal=False, takes the normalised rows of that cross-covariance directly).fit_from_dataloadersextracts and centres the features first.- Parameters:
config (
OrthogonalProcrustesDictionaryConfig) – Fit options; see the config class.- Variables:
W_ortho (
torch.TensororNone) – Fitted directions of shape[K, D](rows are unit atoms).mu (
torch.TensororNone) – Feature mean of shape[D]that was subtracted before fitting; edits projectz - muonto the atoms.
Methods
Solves the Orthogonal Procrustes problem: Find W such that ||XW^T - Y|| is minimized subject to W @ W^T = I.
Extract features for every dataloader, centre them and call
fit.Return the atoms as a
[D, K]matrix (columns are directions).Restore
W_orthoandmufrom a file written bysave_on_disk.Save
W_orthoandmuwithtorch.savetopath.Attributes
config- __init__(config=pydantic.BaseModel)[source]¶
Store the config;
W_orthoandmustayNoneuntil 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:
X (torch.Tensor)
y (torch.Tensor)
- fit_from_dataloaders(dataloaders, feature_extractor)[source]¶
Extract features for every dataloader, centre them and call
fit.The dataset’s
task_configis temporarily replaced byTaskConfig(**config.task)(orNone) while iterating so the loader yields the attributes the dictionary should be supervised with, then restored. Features are collected on CPU;self.muis set to their mean.- Parameters:
dataloaders (
listoftorch.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_orthotransposed to match the[D, K]layout of the other dictionaries, orNonebeforefitwas called.- Return type:
torch.TensororNone