pub struct PrototypeHead<V, const IN: usize, const CLASSES: usize, D = SquaredEuclideanDistance> { /* private fields */ }Expand description
A nearest-class-mean (prototype) classifier to put on top of a frozen
model: head-only adaptation without gradients (C++ PrototypeHead).
The head keeps one prototype per class, the running mean of the embeddings
it was shown for that class, and predicts the class whose prototype is
nearest. Learning is one pass with no learning rate and no epochs; a class
can be added at any time; a class never shown is never predicted. RAM:
CLASSES x IN values plus one u16 count per class.
Each learn moves the prototype by (input - prototype) / count. Once a class’s count reaches the cap set by
set_maximum_count, the count stops growing and
the prototype becomes an exponential moving average with weight 1 / cap,
so it keeps tracking a slowly drifting input instead of freezing on old
samples. The default cap, u16::MAX, is effectively unlimited.
Class indices of CLASSES or more are ignored by the methods that change
the head, and read as an unlearned class.
Implementations§
Source§impl<V: Scalar, const IN: usize, const CLASSES: usize, D> PrototypeHead<V, IN, CLASSES, D>
impl<V: Scalar, const IN: usize, const CLASSES: usize, D> PrototypeHead<V, IN, CLASSES, D>
Sourcepub fn forget(&mut self, class: usize)
pub fn forget(&mut self, class: usize)
Forget one class: it is not predicted again until it is learned.
Sourcepub fn learn(&mut self, input: &[V; IN], class: usize)
pub fn learn(&mut self, input: &[V; IN], class: usize)
Fold one labelled embedding into its class’s prototype.
Sourcepub fn set_maximum_count(&mut self, maximum_count: u16)
pub fn set_maximum_count(&mut self, maximum_count: u16)
The cap on a class’s count (at least 1; 0 is taken as 1). Counts above the new cap are lowered to it.
Sourcepub fn maximum_count(&self) -> u16
pub fn maximum_count(&self) -> u16
The cap on a class’s count.
Sourcepub fn count(&self, class: usize) -> u16
pub fn count(&self, class: usize) -> u16
How many samples a class’s prototype holds (0: not learned).
Sourcepub fn prototype(&self, class: usize) -> Option<&[V; IN]>
pub fn prototype(&self, class: usize) -> Option<&[V; IN]>
A class’s prototype, or None for a class index out of range.
Sourcepub fn set_prototype(&mut self, class: usize, prototype: &[V; IN], count: u16)
pub fn set_prototype(&mut self, class: usize, prototype: &[V; IN], count: u16)
Load one class’s prototype, for example a shipped one, with the weight it carries against later samples (capped; count 0 forgets the class).