Skip to main content

DenseHead

Struct DenseHead 

Source
pub struct DenseHead<V, const IN: usize, const OUT: usize, L = SoftmaxCrossEntropyLoss> {
    pub weights: [[V; IN]; OUT],
    pub biases: [V; OUT],
    /* private fields */
}
Expand description

A trainable dense layer to put on top of a frozen model: head-only fine-tuning (C++ DenseHead).

The frozen model turns a sample into an embedding of IN values; the head maps it to OUT logits and learns from labelled samples. The training state is the head’s parameters and one embedding: the update is fused into the backward pass, so there is no gradient buffer, and the RAM is (IN + 1) x OUT values.

The SGD step is plain gradient descent, learning rate x (delta x input), the product with the learning rate rounded to nearest (Scalar::mul_round_nearest), as the crate’s SGD step is. There is no momentum. The Q formats wrap on overflow, so keep the embedding and the logits well inside the format: z-scored inputs and a Q16.16 head are the working recipe, and Q8.8 is too narrow for a wide head.

To undo an adaptation, reload the shipped parameters with load_weights, keeping them in flash rather than spending RAM on a copy.

Fields§

§weights: [[V; IN]; OUT]

weights[o][i] connects input i to output o.

§biases: [V; OUT]

One bias per output.

Implementations§

Source§

impl<V, const IN: usize, const OUT: usize, L> DenseHead<V, IN, OUT, L>

Source

pub const fn from_weights(weights: [[V; IN]; OUT], biases: [V; OUT]) -> Self

A head with the given parameters, for example the shipped head kept in flash.

Source§

impl<V: Scalar, const IN: usize, const OUT: usize, L> DenseHead<V, IN, OUT, L>

Source

pub fn new() -> Self

A head with every weight and bias 0. It predicts class 0 for every input until it is trained.

Source

pub fn clear(&mut self)

Set every weight and bias to 0.

Source

pub fn initialize_weights(&mut self, random_weight: impl FnMut() -> V)

Draw every weight from random_weight, output by output, and set the biases to 0 (C++ initializeWeights).

Source

pub fn load_weights(&mut self, weights: &[[V; IN]; OUT], biases: &[V; OUT])

Replace every parameter, for example to restore the shipped head.

Source

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

logits[o] = bias[o] + sum_i weight[o][i] x input[i].

Source

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

The index of the largest logit (the first one on a tie).

Source

pub fn backward(&self, output_deltas: &[V; OUT]) -> [V; IN]

input_deltas[i] = sum_o weight[o][i] x output_deltas[o], with the weights as they are now: the gradient with respect to the embedding (times -1, the same delta convention), for a caller that also adapts what lies in front of the head. Call it before update.

Source

pub fn update( &mut self, output_deltas: &[V; OUT], input: &[V; IN], learning_rate: V, )

One SGD step: weight[o][i] += learning rate x (delta[o] x input[i]) and bias[o] += learning rate x delta[o], each product with the learning rate rounded to nearest.

Source§

impl<V: Scalar, const IN: usize, const OUT: usize, L: HeadLoss<V>> DenseHead<V, IN, OUT, L>

Source

pub fn output_deltas(&self, logits: &[V; OUT], targets: &[V; OUT]) -> [V; OUT]

The output deltas (t - y under the loss) for logits from forward.

Source

pub fn train( &mut self, input: &[V; IN], targets: &[V; OUT], learning_rate: V, input_deltas: Option<&mut [V; IN]>, ) -> usize

Forward, loss, optional backward and update on one sample. input_deltas, when given, receives backward of this sample’s deltas, computed before the update. Returns the prediction made before the update, for a running accuracy.

Source

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

train with a one-hot target for class. A class of OUT or more gives an all-zero target, as in the C++.

Trait Implementations§

Source§

impl<V: Copy, const IN: usize, const OUT: usize, L> Clone for DenseHead<V, IN, OUT, L>

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 OUT: usize, L> Copy for DenseHead<V, IN, OUT, L>

Source§

impl<V: Scalar, const IN: usize, const OUT: usize, L> Default for DenseHead<V, IN, OUT, L>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<V, const IN: usize, const OUT: usize, L> Freeze for DenseHead<V, IN, OUT, L>
where V: Freeze,

§

impl<V, const IN: usize, const OUT: usize, L> RefUnwindSafe for DenseHead<V, IN, OUT, L>

§

impl<V, const IN: usize, const OUT: usize, L> Send for DenseHead<V, IN, OUT, L>
where V: Send, L: Send,

§

impl<V, const IN: usize, const OUT: usize, L> Sync for DenseHead<V, IN, OUT, L>
where V: Sync, L: Sync,

§

impl<V, const IN: usize, const OUT: usize, L> Unpin for DenseHead<V, IN, OUT, L>
where V: Unpin, L: Unpin,

§

impl<V, const IN: usize, const OUT: usize, L> UnsafeUnpin for DenseHead<V, IN, OUT, L>
where V: UnsafeUnpin,

§

impl<V, const IN: usize, const OUT: usize, L> UnwindSafe for DenseHead<V, IN, OUT, L>
where V: UnwindSafe, L: 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.