quanda.utils.cache module

Module for caching explanations.

class quanda.utils.cache.BatchedCachedExplanations(cache_dir: str, device: str | None = None)[source]

Bases: object

Utility class for lazy loading and saving batched explanations.

__init__(cache_dir: str, device: str | None = None)[source]

Load and save batched explanations.

Parameters:
  • cache_dir (str) – Directory containing the cached explanations.

  • device (Optional[str]) – Device to load the explanations on.

keys()[source]

Return the num_ids available in the cache.

class quanda.utils.cache.Cache[source]

Bases: object

Abstract class for caching. Methods of this class are static.

static exists(*args, **kwargs) bool[source]

Check if the explanation exists in the cache.

static load(*args, **kwargs) Any[source]

Load the explanation from the cache.

static save(*args, **kwargs) None[source]

Save the explanation to the cache.

class quanda.utils.cache.ExplanationsCache[source]

Bases: Cache

Class for caching generated explanations at a given path.

static exists(path: str, num_id: str | int | None = None) bool[source]

Check if the explanations exist at the given path.

Parameters:
  • path (str) – Path to the explanations.

  • num_id (Optional[Union[str, int]]) – Number identifier for the explanations.

Returns:

True if the explanations exist, False otherwise.

Return type:

bool

static load(path: str, device: str | None = None) BatchedCachedExplanations[source]

Load the explanations from the given path.

Parameters:
  • path (str) – Path to load the explanations.

  • device (Optional[str]) – Device to load the explanations on.

Returns:

BatchedCachedExplanations object that can load explanations lazily by index.

Return type:

BatchedCachedExplanations

static save(path: str, exp_tensors: Tensor, num_id: str | int) None[source]

Save the explanations to the given path.

Parameters:
  • path (str) – Path to save the explanations.

  • exp_tensors (torch.Tensor) – Explanations to save.

  • num_id (Union[str, int]) – Number identifier for the explanations.

Return type:

None