Skip to main content

Module head

Module head 

Source
Expand description

Head-only fine-tuning: small classifiers that sit on top of a frozen model and learn on the device from a few labelled samples. Ported from tinymind (C++) cpp/densehead.hpp and cpp/prototypehead.hpp.

A model trained on the host meets data on the device that it never saw: a sensor ages, a user moves differently, a new class appears. Retraining the whole network on a microcontroller needs the training state of every layer. Head-only fine-tuning keeps the shipped model frozen and adapts only a head on top of its embedding. Nothing is backpropagated through the frozen model, so there are no stored activations and no gradients for it, and neither head allocates.

The frozen model can be anything that produces an embedding: an InferenceNetwork, an int8 pipeline whose output the application converts to a Q format, or the layers in front of the last one of a trained network. The gas_sensor_drift example adapts a Q16.16 128 -> 32 -> 6 classifier to drifted sensor data both ways.

Both heads run on Q16_16, Q8_8, f32 and f64. The softmax needs an exponential (SoftmaxExp): the 96-entry exp lookup table for the Q formats, the math library for the float types (feature std or libm). SquaredErrorLoss and PrototypeHead need only arithmetic.

Every operation produces the same bits as tinymind (C++) (floating point: with std on a glibc host).

use tinymind::Q16_16;
use tinymind::head::{DenseHead, PrototypeHead};

// A 4-value embedding and 2 classes.
let a = [1, 1, 0, 0].map(Q16_16::from_int);
let b = [0, 0, 1, 1].map(Q16_16::from_int);

let mut dense = DenseHead::<Q16_16, 4, 2>::new();
let rate = Q16_16::pow2_neg(2); // 0.25
for _ in 0..20 {
    dense.train_class(&a, 0, rate, None);
    dense.train_class(&b, 1, rate, None);
}
assert_eq!((dense.predict(&a), dense.predict(&b)), (0, 1));

let mut prototypes = PrototypeHead::<Q16_16, 4, 2>::new();
prototypes.learn(&a, 0);
prototypes.learn(&b, 1);
assert_eq!((prototypes.predict(&a), prototypes.predict(&b)), (0, 1));

Structs§

DenseHead
A trainable dense layer to put on top of a frozen model: head-only fine-tuning (C++ DenseHead).
ManhattanDistance
sum_i |a[i] - b[i]| (C++ ManhattanDistance). No multiplies, so it is cheaper on a core without a fast multiplier and has far more headroom before it overflows.
PrototypeHead
A nearest-class-mean (prototype) classifier to put on top of a frozen model: head-only adaptation without gradients (C++ PrototypeHead).
SoftmaxCrossEntropyLoss
Softmax over the logits with cross-entropy loss, the usual classifier head (C++ SoftmaxCrossEntropyHeadLoss): delta = t - softmax(z).
SquaredErrorLoss
A linear output with squared-error loss L = 0.5 x sum((t - z)^2), for a regression head or a classifier that regresses a one-hot target (C++ SquaredErrorHeadLoss): delta = t - z. Arithmetic only.
SquaredEuclideanDistance
sum_i (a[i] - b[i])^2, the usual nearest-class-mean distance (C++ SquaredEuclideanDistance). In a Q format the sum must stay inside the format (it wraps): with Q16.16, N x (largest difference)^2 below 32767.

Traits§

Distance
The distance a PrototypeHead compares embeddings with.
HeadLoss
How a DenseHead turns its logits and a target into output deltas, following the library’s convention delta = -dL/dz: a parameter moves by +learning rate x delta x input.
SoftmaxExp
exp(x) for the softmax’s shifted logits, x <= 0 (C++ detail::HeadExp).