Buckets:
| """ | |
| Neural network modules for SAE-based topic classification. | |
| """ | |
| import torch | |
| import torch.nn as nn | |
| class TopKSAE(nn.Module): | |
| """Sparse autoencoder that keeps only the top-k activations per token.""" | |
| def __init__(self, d_model, d_sae, k): | |
| super().__init__() | |
| self.d_model = d_model | |
| self.d_sae = d_sae | |
| self.k = k | |
| # register_buffer so weights move with .to(device) and appear in state_dict | |
| self.register_buffer("W_enc", torch.zeros(d_sae, d_model)) # (d_sae, d_model) | |
| self.register_buffer("b_enc", torch.zeros(d_sae)) | |
| def topk_indices(self, x): | |
| """Return indices of the top-k pre-activations for each token in x.""" | |
| # W_enc is (d_sae, d_model), so x @ W_enc.T gives (n_tokens, d_sae) | |
| pre = x @ self.W_enc.T + self.b_enc | |
| return pre.topk(self.k, dim=-1).indices | |
| class TopicClassifier(nn.Module): | |
| """Two-layer MLP that maps SAE feature vectors to topic logits.""" | |
| def __init__(self, n_in, n_classes, hidden): | |
| super().__init__() | |
| self.net = nn.Sequential( | |
| nn.Linear(n_in, hidden), | |
| nn.ReLU(), | |
| nn.Linear(hidden, n_classes), | |
| ) | |
| def forward(self, x): | |
| return self.net(x) | |
Xet Storage Details
- Size:
- 1.25 kB
- Xet hash:
- 7e2c14ff3425a9ed1c20d3a3098b69ba532e5554c49ea4db7ff5149821ce6bcb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.