peal.sparse_dictionaries.singular_value_decomposition.SVDDictionary

class peal.sparse_dictionaries.singular_value_decomposition.SVDDictionary[source]

Bases: SparseDictionary

Dictionary whose components are the top right-singular vectors of the data.

fit stores U, S and Vt of the (already centred) feature matrix truncated to config.n_components; fit_from_dataloaders additionally records the feature mean mu that was subtracted.

Parameters:

config (SVDDictionaryConfig, optional) – Number of components and factory metadata.

Variables:
  • Vt (U, S,) – Truncated SVD factors, None until fitted.

  • mu (torch.Tensor or None) – Feature mean subtracted before the SVD (only set by fit_from_dataloaders).

Methods

fit

Compute the truncated SVD of a feature matrix.

fit_from_dataloaders

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

get_components

Return the dictionary as a (n_features, n_components) matrix.

load_from_disk

Restore the factors written by save_on_disk() from path.

save_on_disk

Save U, S, Vt and mu with torch.save to path.

Attributes

config

__init__(config=pydantic.BaseModel)[source]

Store the config and leave the SVD factors unfitted (None).

fit(X)[source]

Compute the truncated SVD of a feature matrix.

Parameters:

X (torch.Tensor) – Feature matrix of shape (n_samples, n_features); no centring is applied here.

fit_from_dataloaders(dataloaders, feature_extractor)[source]

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

Parameters:
  • dataloaders (iterable of torch.utils.data.DataLoader) – Loaders whose first batch element is the input image; it is moved to CUDA before feature extraction.

  • feature_extractor (callable) – Maps a batch of inputs to a (batch, n_features) tensor.

get_components()[source]

Return the dictionary as a (n_features, n_components) matrix.

save_on_disk(path)[source]

Save U, S, Vt and mu with torch.save to path.

load_from_disk(path)[source]

Restore the factors written by save_on_disk() from path.