Few-Shot Meta-Learning — Hebbian Associative Memory¶
Learn from 1-5 examples using spike-timing plasticity instead of gradient descent. Two approaches are exposed: class-indexed Hebbian memory and prototypical classification in spike-rate space.
HebbianFewShot — Associative Memory¶
Support patterns are stored via one-shot Hebbian update:
memory[label] += lr * pattern. Queries are classified by cosine similarity to
the stored class memories. The few_shot_episode() method handles the full
N-way K-shot protocol: reset -> store support set -> classify query set.
| Parameter | Default | Meaning |
|---|---|---|
n_features |
(required) | Input feature dimension |
n_classes |
(required) | Number of classes |
lr_hebbian |
0.1 | Hebbian learning rate for storage |
Accepts spike-rate vectors (n_features,) or raw spike trains (T, n_features) — automatically averaged over time.
query_scores() returns one bounded cosine score per class. Classes without
support examples score 0.0, and querying before storage raises ValueError.
export_weights() returns a defensive copy of the class-memory matrix for
hardware export or downstream inspection.
SpikePrototypeNet — Prototypical Network¶
Computes class prototypes as mean spike-rate vectors from the support set. Classifies queries by nearest prototype using cosine similarity, negative Euclidean distance, or negative normalized Hamming disagreement. It stores the most recent prototypes only so they can be inspected or exported.
| Parameter | Default | Meaning |
|---|---|---|
n_features |
(required) | Feature dimension |
metric |
"cosine" | Distance metric: "cosine", "euclidean", or "hamming" |
Usage¶
from sc_neurocore.few_shot import HebbianFewShot, SpikePrototypeNet
import numpy as np
# 5-way 1-shot with Hebbian memory
learner = HebbianFewShot(n_features=64, n_classes=5)
support_x = [np.random.rand(64) for _ in range(5)]
support_y = [0, 1, 2, 3, 4]
query_x = [np.random.rand(64) for _ in range(10)]
predictions = learner.few_shot_episode(support_x, support_y, query_x)
# Prototypical network (no training needed)
proto = SpikePrototypeNet(n_features=64, metric="cosine")
predictions = proto.classify(support_x, support_y, query_x)
prototypes = proto.export_prototypes()
Reference: HAAM (BICS 2024).
Validation and Benchmark Evidence¶
The public Python API is covered by the module-specific
tests/test_few_shot.py surface. The maintained polyglot mirrors are:
| Surface | Scope | Local check |
|---|---|---|
| Python | Public API, vector and temporal inputs, exports, validation guards | pytest tests/test_few_shot.py |
| Rust | Safety mirror for vector HAAM and prototype episodes | rustc --edition=2021 --test src/sc_neurocore/accel/rust/safety/haam.rs |
| Julia | Vector HAAM and prototype validation mirror | julia --startup-file=no --history-file=no ... validate_haam() |
| Mojo | Standalone vector/temporal HAAM and prototype validation kernel | mojo src/sc_neurocore/accel/mojo/kernels/haam.mojo |
benchmarks/results/bench_few_shot_haam.json records the latest local,
non-isolated evidence. The Python public API measured 1000 deterministic calls
at 3581.011 Hebbian episodes/s and 4770.742 prototype classifications/s on the
current workstation. Rust, Julia, and Mojo validation checks all passed in that
same run. The artifact is a local regression record, not an isolated production
benchmark claim.
See Tutorial 84: Few-Shot Meta-Learning.
sc_neurocore.few_shot.haam
¶
Spike-domain few-shot learners for associative-memory episodes.
The module provides two small deterministic learners for N-way K-shot spike
classification. HebbianFewShot stores support examples in class-indexed
associative memory and scores queries by cosine similarity. SpikePrototypeNet
keeps no training state between calls; it computes support-set prototypes and
classifies query vectors by cosine similarity, Euclidean distance, or binary
Hamming distance.
HebbianFewShot
¶
Class-indexed Hebbian memory for few-shot spike episodes.
Parameters¶
n_features : int Number of spike-rate features per pattern after temporal averaging. n_classes : int Number of class slots stored by the associative memory. lr_hebbian : float, default=0.1 Non-negative multiplier applied when support patterns are accumulated into their class memory rows.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
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 | |
store(spike_pattern, label)
¶
Store one support pattern in the class memory.
Parameters¶
spike_pattern : array_like
Spike-rate vector with shape (n_features,) or spike train with
shape (T, n_features). Temporal spike trains are averaged over
the first axis before storage.
label : int
Class slot to update.
Raises¶
ValueError If the label is out of range or the pattern cannot be resolved to a finite feature vector.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
query_scores(spike_pattern)
¶
Return cosine scores for a query against every stored class.
Parameters¶
spike_pattern : array_like Query spike-rate vector or temporal spike train.
Returns¶
numpy.ndarray
One score per class. Classes with no support examples receive a
score of 0.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
query(spike_pattern)
¶
Classify one query pattern by nearest stored memory.
Parameters¶
spike_pattern : array_like Query spike-rate vector or temporal spike train.
Returns¶
int Predicted class label.
Raises¶
ValueError If no support examples have been stored.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
few_shot_episode(support_x, support_y, query_x)
¶
Run one reset-store-query few-shot episode.
Parameters¶
support_x : list of array_like Support spike patterns. support_y : list of int Class label for each support pattern. query_x : list of array_like Query spike patterns to classify after support storage.
Returns¶
list of int Predicted labels for the query set.
Raises¶
ValueError If the support pattern and label lists have different lengths.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
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 | |
export_weights()
¶
Return a defensive copy of the class memory matrix.
Returns¶
numpy.ndarray
Matrix with shape (n_classes, n_features) containing the
accumulated Hebbian support memory.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
204 205 206 207 208 209 210 211 212 213 | |
reset()
¶
Clear the memory matrix and support counts.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
215 216 217 218 | |
SpikePrototypeNet
dataclass
¶
Nearest-prototype classifier for spike-rate few-shot episodes.
Parameters¶
n_features : int
Number of features per vector after temporal averaging.
metric : {"cosine", "euclidean", "hamming"}, default="cosine"
Distance or similarity metric used to score queries against support-set
prototypes. hamming thresholds vectors at zero and scores by negative
normalised bit disagreement.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
__post_init__()
¶
Validate the prototype classifier configuration after dataclass init.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
239 240 241 242 243 | |
classify(support_x, support_y, query_x)
¶
Classify query patterns from support-set prototypes.
Parameters¶
support_x : list of array_like Support spike patterns with one-dimensional or temporal shapes. support_y : list of int Label for each support pattern. query_x : list of array_like Query spike patterns to classify.
Returns¶
list of int Predicted class labels.
Raises¶
ValueError If support inputs are empty, labels are mismatched, or any pattern cannot be resolved to a finite feature vector.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
export_prototypes()
¶
Return defensive copies of the most recently computed prototypes.
Returns¶
dict of int to numpy.ndarray Mapping from class label to prototype vector.
Source code in src/sc_neurocore/few_shot/haam.py
| Python | |
|---|---|
290 291 292 293 294 295 296 297 298 | |