peal.sparse_dictionaries.batch_topk.BatchTopKSAE¶
- class peal.sparse_dictionaries.batch_topk.BatchTopKSAE[source]¶
Bases:
SparseDictionarySparse dictionary backed by the vendored BatchTopK SAE.
Activations are centred by
mu(the training mean) before encoding and the mean is added back after decoding, soencode/decodework on raw activations of sizeconfig.act_size.- Parameters:
config (
BatchTopKSAEConfig) – Hyper-parameters;config.cfgmay override the whole internal config dict passed to the dependency.- Variables:
sae (
peal.sparse_dictionaries.batch_topk_network.BatchTopKNetwork) – The underlying model withW_enc,W_dec,b_enc,b_decand (after training)threshold.mu (
torch.Tensor) – Shape(act_size,): mean of the training activations.
Methods
Reconstruct raw activations from sparse codes (adds
muback).Sparse codes of raw activations (thresholded, as at inference).
Pre-activation codes without the learned threshold applied.
Train the SAE with
train_batch_topk_saes.Everything fit_from_dataloaders does once the encoder has been run.
Run
feature_extractorover the dataloaders and fit on the result.Train the SAE with periodic F1 evaluation against ground truth labels.
Return the encoder weight
W_encof shape(act_size, dict_size).Load a checkpoint written by
save_on_diskontoconfig.device.Save
W_enc,W_dec,b_enc,b_dec,muandthreshold(if present) as a CPUtorchcheckpoint.Attributes
config- fit(X)[source]¶
Train the SAE with
train_batch_topk_saes.- Parameters:
X (
torch.TensororActivationStore) – Activations of shape(N, act_size)(wrapped into anActivationStore) or an object withnext_batch. Note thatXis used as is; centring bymuis the caller’s job (fit_from_activationsdoes it).
- fit_with_evaluation(activation_store, ground_truth_labels, log_dir=None)[source]¶
Train the SAE with periodic F1 evaluation against ground truth labels. Logs F1 scores for the first 10 components to TensorBoard.
- Parameters:
activation_store – ActivationStore wrapping the centered activations
ground_truth_labels – Tensor (N, K) of ground truth labels
log_dir – Directory for TensorBoard logs. If None, uses config base_path.
- fit_from_dataloaders(dataloaders, feature_extractor=<function BatchTopKSAE.<lambda>>)[source]¶
Run
feature_extractorover the dataloaders and fit on the result.- Parameters:
dataloaders (
iterableoftorch.utils.data.DataLoader) – Batches whose first element is the input and, optionally, whose second element holds ground-truth labels for evaluation.feature_extractor (
callable) – Maps an input batch onconfig.deviceto activations of shape(B, act_size).
- fit_from_activations(X, y=None)[source]¶
Everything fit_from_dataloaders does once the encoder has been run.
Sets
muto the activation mean, wraps the centred activations in anActivationStoreand trains withfit_with_evaluationwhen labels are given, otherwise withfit.- Parameters:
X (
torch.Tensor) – Activations of shape(N, act_size)on CPU.y (
torch.Tensor, optional) – Ground-truth labels of shape(N,)or(N, K)used for the final SAE evaluation.
- save_on_disk(path)[source]¶
Save
W_enc,W_dec,b_enc,b_dec,muandthreshold(if present) as a CPUtorchcheckpoint.- Parameters:
path (
str) – Target file; parent directories are created.
- load_from_disk(path)[source]¶
Load a checkpoint written by
save_on_diskontoconfig.device.- Parameters:
path (
str) – Checkpoint file.
- encode(x)[source]¶
Sparse codes of raw activations (thresholded, as at inference).
- Parameters:
x (
torch.Tensor) – Shape(B, act_size).- Returns:
Shape
(B, dict_size).- Return type:
- encode_no_threshold(x)[source]¶
Pre-activation codes without the learned threshold applied.
- Parameters:
x (
torch.Tensor) – Shape(B, act_size).- Returns:
Shape
(B, dict_size); first output ofsae.compute_activationson the centred input.- Return type:
- decode(acts)[source]¶
Reconstruct raw activations from sparse codes (adds
muback).- Parameters:
acts (
torch.Tensor) – Shape(B, dict_size).- Returns:
Shape
(B, act_size).- Return type: