peal.sparse_dictionaries.singular_value_decomposition.SVDDictionary¶
- class peal.sparse_dictionaries.singular_value_decomposition.SVDDictionary[source]¶
Bases:
SparseDictionaryDictionary whose components are the top right-singular vectors of the data.
fitstoresU,SandVtof the (already centred) feature matrix truncated toconfig.n_components;fit_from_dataloadersadditionally records the feature meanmuthat was subtracted.- Parameters:
config (
SVDDictionaryConfig, optional) – Number of components and factory metadata.- Variables:
Vt (U, S,) – Truncated SVD factors,
Noneuntil fitted.mu (
torch.TensororNone) – Feature mean subtracted before the SVD (only set byfit_from_dataloaders).
Methods
Compute the truncated SVD of a feature matrix.
Extract features for every batch, centre them and call
fit.Return the dictionary as a
(n_features, n_components)matrix.Restore the factors written by
save_on_disk()frompath.Save
U,S,Vtandmuwithtorch.savetopath.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 (
iterableoftorch.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.
- load_from_disk(path)[source]¶
Restore the factors written by
save_on_disk()frompath.