Prototypes¶
Compact representations of a support set — statistical summaries and a learnable encoder — that condition the weight-generating teacher.
polyweave.prototypes ¶
Prototype builders — compact support-set representations fed to a teacher.
A prototype is the teacher's only view of a few-shot support set. Every builder
returns a tensor shaped [1, channels, H, W] (a batch of one "image" with
channels statistic maps) that a convolutional teacher consumes.
Two flavours are provided:
-
Statistical (parameter-free), matching the paper:
- :func:
feature_class_stats->[1, 4, num_classes, feature_dim](per-class mean/variance/kurtosis/contrast of student features; Exp 1) - :func:
image_grid_stats->[1, 4, num_classes, grid^2 * in_ch](class-conditional stats over a spatial grid of raw inputs; Exp 2) - :func:
relation_cross_moments->[1, 4, d_model, d_model](embedding-space query/key cross-moments; Exp 3)
- :func:
-
Learnable (:class:
LearnablePrototypeEncoder): a trainable encoder that maps support features to a prototype, for real-world settings where hand-built statistics are too rigid. Output shape matches the statistical feature builder so it is a drop-in replacement for the teacher.
LearnablePrototypeEncoder ¶
LearnablePrototypeEncoder(in_dim: int, num_classes: int, out_channels: int = 4, embed_dim: Optional[int] = None, hidden_dim: Optional[int] = None, normalize: bool = True)
Bases: Module
Encode a support set into a learned prototype tensor.
Each support example's feature vector is passed through a small MLP that
emits out_channels * embed_dim values, reshaped to [out_channels,
embed_dim] and mean-pooled within each class. Empty classes yield zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dim
|
int
|
dimensionality of the input support features. |
required |
num_classes
|
int
|
number of classes (rows of the prototype). |
required |
out_channels
|
int
|
number of statistic channels to emit (match the teacher's
|
4
|
embed_dim
|
Optional[int]
|
width of each channel's per-class vector. Defaults to
|
None
|
hidden_dim
|
Optional[int]
|
hidden width of the encoder MLP. Defaults to |
None
|
normalize
|
bool
|
apply per-channel normalisation to the pooled prototype (default True), matching the statistical builders. |
True
|
Source code in polyweave/prototypes/learnable.py
forward ¶
Map support features to a prototype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feats
|
Tensor
|
support features |
required |
y
|
Tensor
|
integer labels |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Prototype |
Source code in polyweave/prototypes/learnable.py
feature_class_stats ¶
feature_class_stats(feats: Tensor, y: Tensor, num_classes: int, normalize: bool = True) -> torch.Tensor
Per-class statistics of student features (Experiment 1).
Channels: 0=mean, 1=variance, 2=excess kurtosis, 3=inter-class contrast (per-class absolute deviation from the global feature mean).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feats
|
Tensor
|
student features |
required |
y
|
Tensor
|
integer labels |
required |
num_classes
|
int
|
number of classes (rows in the prototype). |
required |
normalize
|
bool
|
apply per-channel normalisation (default True). |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Prototype |
Source code in polyweave/prototypes/statistical.py
image_grid_stats ¶
image_grid_stats(x: Tensor, y: Tensor, num_classes: int, grid: int = 4, normalize: bool = True) -> torch.Tensor
Class-conditional statistics over a spatial grid of raw inputs (Exp 2).
The image is split into a grid x grid array of cells; per cell and channel
we take the mean intensity, giving a grid^2 * in_ch feature vector per
example. Channels: 0=mean, 1=variance, 2=excess kurtosis, 3=inter-class
contrast (mean absolute deviation from the other classes' means).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
input images |
required |
y
|
Tensor
|
integer labels |
required |
num_classes
|
int
|
number of classes. |
required |
grid
|
int
|
cells per spatial axis. |
4
|
normalize
|
bool
|
apply per-channel normalisation (default True). |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Prototype |
Source code in polyweave/prototypes/statistical.py
normalize_prototype ¶
Per-channel standardisation across the last two (spatial) dims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proto
|
Tensor
|
tensor shaped |
required |
eps
|
float
|
stabiliser added to the per-channel std. |
EPS
|
Source code in polyweave/prototypes/statistical.py
relation_cross_moments ¶
relation_cross_moments(embeddings: Tensor, y: Tensor, num_key_slots: int, normalize: bool = True) -> torch.Tensor
Embedding-space query/key cross-moments for the relational task (Exp 3).
Channels (each a D x D second-moment matrix over the support batch):
0. R_qk = E[ e_query (x) e_matched_key ] — the relation signal
1. C_qq = E[ e_query (x) e_query ] — query geometry
2. C_kk = E[ e_key (x) e_key ] — key geometry (all slots)
3. R_qctx = E[ e_query (x) mean_key ] — query vs. mean-key context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
Tensor
|
token embeddings |
required |
y
|
Tensor
|
matched-key slot index |
required |
num_key_slots
|
int
|
number of key slots |
required |
normalize
|
bool
|
apply per-channel normalisation (default True). |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Prototype |