Activation Store Base Class¤
Activation Store Base Class.
ActivationStore
¤
Bases: Dataset[Float[Tensor, names(COMPONENT_OPTIONAL, INPUT_OUTPUT_FEATURE)]]
, ABC
Activation Store Abstract Class.
Extends the torch.utils.data.Dataset
class to provide an activation store, with additional
:meth:append
and :meth:extend
methods (the latter of which should typically be
non-blocking). The resulting activation store can be used with a torch.utils.data.DataLoader
to iterate over the dataset.
Extend this class if you want to create a new activation store (noting you also need to create
__getitem__
and __len__
methods from the underlying torch.utils.data.Dataset
class).
Example:
import torch class MyActivationStore(ActivationStore): ... ... @property ... def current_activations_stored_per_component(self): ... raise NotImplementedError ... ... @property ... def n_components(self): ... raise NotImplementedError ... ... def init(self): ... super().init() ... self._data = [] # In this example, we just store in a list ... ... def append(self, item) -> None: ... self._data.append(item) ... ... def extend(self, batch): ... self._data.extend(batch) ... ... def empty(self): ... self._data = [] ... ... def getitem(self, index: int): ... return self._data[index] ... ... def len(self) -> int: ... return len(self._data) ... store = MyActivationStore() store.append(torch.randn(100)) print(len(store)) 1
Source code in sparse_autoencoder/activation_store/base_store.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
current_activations_stored_per_component: list[int]
abstractmethod
property
¤
Current activations stored per component.
n_components: int
abstractmethod
property
¤
Number of components.
__getitem__(index)
abstractmethod
¤
Get an Item from the Store.
Source code in sparse_autoencoder/activation_store/base_store.py
99 100 101 102 103 |
|
__len__()
abstractmethod
¤
Get the Length of the Store.
Source code in sparse_autoencoder/activation_store/base_store.py
95 96 97 |
|
append(item, component_idx)
abstractmethod
¤
Add a Single Item to the Store.
Source code in sparse_autoencoder/activation_store/base_store.py
65 66 67 68 69 70 71 |
|
empty()
abstractmethod
¤
Empty the Store.
Source code in sparse_autoencoder/activation_store/base_store.py
81 82 83 |
|
extend(batch, component_idx)
abstractmethod
¤
Add a Batch to the Store.
Source code in sparse_autoencoder/activation_store/base_store.py
73 74 75 76 77 78 79 |
|
fill_with_test_data(n_batches=1, batch_size=16, n_components=1, input_features=256)
¤
Fill the store with test data.
For use when testing your code, to ensure it works with a real activation store.
Warning
You may want to use torch.seed(0)
to make the random data deterministic, if your test
requires inspecting the data itself.
Example
from sparse_autoencoder.activation_store.tensor_store import TensorActivationStore store = TensorActivationStore(max_items=100, n_neurons=256, n_components=1) store.fill_with_test_data(batch_size=100) len(store) 100
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_batches |
PositiveInt
|
Number of batches to fill the store with. |
1
|
batch_size |
PositiveInt
|
Number of items per batch. |
16
|
n_components |
PositiveInt
|
Number of source model components the SAE is trained on. |
1
|
input_features |
PositiveInt
|
Number of input features per item. |
256
|
Source code in sparse_autoencoder/activation_store/base_store.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
shuffle()
¤
Optional shuffle method.
Source code in sparse_autoencoder/activation_store/base_store.py
105 106 |
|
StoreFullError
¤
Bases: IndexError
Exception raised when the activation store is full.
Source code in sparse_autoencoder/activation_store/base_store.py
144 145 146 147 148 149 150 151 152 153 |
|
__init__(message='Activation store is full')
¤
Initialise the exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
Override the default message. |
'Activation store is full'
|
Source code in sparse_autoencoder/activation_store/base_store.py
147 148 149 150 151 152 153 |
|