Skip to main content

PrototypeHead

Struct PrototypeHead 

Source
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>

Source

pub fn new() -> Self

A head that knows no class, with the cap at u16::MAX.

Source

pub fn clear(&mut self)

Forget every class (the cap is kept).

Source

pub fn forget(&mut self, class: usize)

Forget one class: it is not predicted again until it is learned.

Source

pub fn learn(&mut self, input: &[V; IN], class: usize)

Fold one labelled embedding into its class’s prototype.

Source

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.

Source

pub fn maximum_count(&self) -> u16

The cap on a class’s count.

Source

pub fn count(&self, class: usize) -> u16

How many samples a class’s prototype holds (0: not learned).

Source

pub fn prototype(&self, class: usize) -> Option<&[V; IN]>

A class’s prototype, or None for a class index out of range.

Source

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).

Source§

impl<V: Scalar, const IN: usize, const CLASSES: usize, D: Distance<V>> PrototypeHead<V, IN, CLASSES, D>

Source

pub fn distance(&self, input: &[V; IN], class: usize) -> Option<V>

The distance from input to a class’s prototype, or None for a class index out of range.

Source

pub fn predict(&self, input: &[V; IN]) -> usize

The learned class whose prototype is nearest (the first one on a tie), or CLASSES when no class has been learned.

Trait Implementations§

Source§

impl<V: Copy, const IN: usize, const CLASSES: usize, D> Clone for PrototypeHead<V, IN, CLASSES, D>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Copy, const IN: usize, const CLASSES: usize, D> Copy for PrototypeHead<V, IN, CLASSES, D>

Source§

impl<V: Scalar, const IN: usize, const CLASSES: usize, D> Default for PrototypeHead<V, IN, CLASSES, D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V, const IN: usize, const CLASSES: usize, D> Freeze for PrototypeHead<V, IN, CLASSES, D>
where V: Freeze,

§

impl<V, const IN: usize, const CLASSES: usize, D> RefUnwindSafe for PrototypeHead<V, IN, CLASSES, D>

§

impl<V, const IN: usize, const CLASSES: usize, D> Send for PrototypeHead<V, IN, CLASSES, D>
where D: Send, V: Send,

§

impl<V, const IN: usize, const CLASSES: usize, D> Sync for PrototypeHead<V, IN, CLASSES, D>
where D: Sync, V: Sync,

§

impl<V, const IN: usize, const CLASSES: usize, D> Unpin for PrototypeHead<V, IN, CLASSES, D>
where D: Unpin, V: Unpin,

§

impl<V, const IN: usize, const CLASSES: usize, D> UnsafeUnpin for PrototypeHead<V, IN, CLASSES, D>
where V: UnsafeUnpin,

§

impl<V, const IN: usize, const CLASSES: usize, D> UnwindSafe for PrototypeHead<V, IN, CLASSES, D>
where D: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.