peal.sparse_dictionaries.interfaces.SparseDictionary

class peal.sparse_dictionaries.interfaces.SparseDictionary[source]

Bases: ABC

Abstract interface of a sparse dictionary.

Subclasses hold a config (a SparseDictionaryConfig) and implement fitting from a tensor of activations or from dataloaders, plus serialisation. Most concrete implementations additionally offer encode/decompose, get_components and eval, which the explainers call but which are not part of this minimal base class.

Variables:

config (SparseDictionaryConfig) – The configuration the dictionary was built from.

Methods

fit

Fit the dictionary on a tensor of activations.

fit_from_dataloaders

Fit the dictionary by iterating over one or more dataloaders.

load_from_disk

Restore the fitted state from path.

save_on_disk

Serialise the fitted state to path.

Attributes

config: SparseDictionaryConfig
abstract fit(X)[source]

Fit the dictionary on a tensor of activations.

Parameters:

X (torch.Tensor) – Activations of shape [N, act_size].

Raises:

NotImplementedError – Always, in the base class.

abstract fit_from_dataloaders(dataloaders)[source]

Fit the dictionary by iterating over one or more dataloaders.

Parameters:

dataloaders (list of torch.utils.data.DataLoader) – Loaders yielding (x, y) batches; implementations run the encoder themselves and collect the activations.

Raises:

NotImplementedError – Always, in the base class.

abstract save_on_disk(path)[source]

Serialise the fitted state to path.

Raises:

NotImplementedError – Always, in the base class.

abstract load_from_disk(path)[source]

Restore the fitted state from path.

Raises:

NotImplementedError – Always, in the base class.