Linear encoder layer¤
Linear encoder layer.
LinearEncoder
¤
Bases: Module
Linear encoder layer.
Linear encoder layer (essentially nn.Linear
, with a ReLU activation function). Designed to be
used as the encoder in a sparse autoencoder (excluding any outer tied bias).
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
activation_function: ReLU = ReLU()
instance-attribute
¤
Activation function.
bias: Float[Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.LEARNT_FEATURE)] = Parameter(torch.zeros(shape_with_optional_dimensions(n_components, learnt_features)))
instance-attribute
¤
Bias parameter.
reset_optimizer_parameter_details: list[ResetOptimizerParameterDetails]
property
¤
Reset optimizer parameter details.
Details of the parameters that should be reset in the optimizer, when resetting dictionary vectors.
Returns:
Type | Description |
---|---|
list[ResetOptimizerParameterDetails]
|
List of tuples of the form |
list[ResetOptimizerParameterDetails]
|
reset (e.g. encoder.weight), and |
weight: Float[Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.LEARNT_FEATURE, Axis.INPUT_OUTPUT_FEATURE)] = Parameter(torch.empty(shape_with_optional_dimensions(n_components, learnt_features, input_features)))
instance-attribute
¤
Weight parameter.
Each row in the weights matrix acts as a dictionary vector, representing a single basis element in the learned activation space.
__init__(input_features, learnt_features, n_components)
¤
Initialize the linear encoder layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features |
PositiveInt
|
Number of input features to the autoencoder. |
required |
learnt_features |
PositiveInt
|
Number of learnt features in the autoencoder. |
required |
n_components |
PositiveInt | None
|
Number of source model components the SAE is trained on. |
required |
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
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 |
|
extra_repr()
¤
String extra representation of the module.
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
215 216 217 218 219 220 221 |
|
forward(x)
¤
Forward pass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Float[Tensor, names(BATCH, COMPONENT_OPTIONAL, INPUT_OUTPUT_FEATURE)]
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Float[Tensor, names(BATCH, COMPONENT_OPTIONAL, LEARNT_FEATURE)]
|
Output of the forward pass. |
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
reset_parameters()
¤
Initialize or reset the parameters.
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
109 110 111 112 113 114 115 116 117 118 |
|
update_bias(update_parameter_indices, updated_bias_features, component_idx=None)
¤
Update encoder bias.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update_parameter_indices |
Int64[Tensor, names(COMPONENT_OPTIONAL, LEARNT_FEATURE_IDX)]
|
Indices of the bias features to update. |
required |
updated_bias_features |
Float[Tensor, names(COMPONENT_OPTIONAL, LEARNT_FEATURE_IDX)]
|
Updated bias features for just these indices. |
required |
component_idx |
int | None
|
Component index to update. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If there are multiple components and |
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
update_dictionary_vectors(dictionary_vector_indices, updated_dictionary_weights, component_idx=None)
¤
Update encoder dictionary vectors.
Updates the dictionary vectors (columns in the weight matrix) with the given values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary_vector_indices |
Int64[Tensor, names(LEARNT_FEATURE_IDX)]
|
Indices of the dictionary vectors to update. |
required |
updated_dictionary_weights |
Float[Tensor, names(LEARNT_FEATURE_IDX, INPUT_OUTPUT_FEATURE)]
|
Updated weights for just these dictionary vectors. |
required |
component_idx |
int | None
|
Component index to update. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If there are multiple components and |
Source code in sparse_autoencoder/autoencoder/components/linear_encoder.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|