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>
impl<V, const IN: usize, const OUT: usize, L> DenseHead<V, IN, OUT, L>
Sourcepub const fn from_weights(weights: [[V; IN]; OUT], biases: [V; OUT]) -> Self
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>
impl<V: Scalar, const IN: usize, const OUT: usize, L> DenseHead<V, IN, OUT, L>
Sourcepub fn new() -> Self
pub fn new() -> Self
A head with every weight and bias 0. It predicts class 0 for every input until it is trained.
Sourcepub fn initialize_weights(&mut self, random_weight: impl FnMut() -> V)
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).
Sourcepub fn load_weights(&mut self, weights: &[[V; IN]; OUT], biases: &[V; OUT])
pub fn load_weights(&mut self, weights: &[[V; IN]; OUT], biases: &[V; OUT])
Replace every parameter, for example to restore the shipped head.
Sourcepub fn forward(&self, input: &[V; IN]) -> [V; OUT]
pub fn forward(&self, input: &[V; IN]) -> [V; OUT]
logits[o] = bias[o] + sum_i weight[o][i] x input[i].
Sourcepub fn predict(&self, input: &[V; IN]) -> usize
pub fn predict(&self, input: &[V; IN]) -> usize
The index of the largest logit (the first one on a tie).
Sourcepub fn backward(&self, output_deltas: &[V; OUT]) -> [V; IN]
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§impl<V: Scalar, const IN: usize, const OUT: usize, L: HeadLoss<V>> DenseHead<V, IN, OUT, L>
impl<V: Scalar, const IN: usize, const OUT: usize, L: HeadLoss<V>> DenseHead<V, IN, OUT, L>
Sourcepub fn output_deltas(&self, logits: &[V; OUT], targets: &[V; OUT]) -> [V; OUT]
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.
Sourcepub fn train(
&mut self,
input: &[V; IN],
targets: &[V; OUT],
learning_rate: V,
input_deltas: Option<&mut [V; IN]>,
) -> usize
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.