Training a tiny model
Turn each window into a few useful numbers, such as the mean and the energy, and train a small classifier. Then measure it honestly on held-out data.
In 60 seconds
Training a tiny model
Turn each window into a few useful numbers, such as the mean and the energy, and train a small classifier. Then measure it honestly on held-out data.
Two simple, powerful features
| Feature | How to compute it | What it tells you |
|---|---|---|
| Mean (per axis) | The average of the readings in the window | Which way gravity points, so how the device is tilted or lying |
| Energy (per axis) | The average of (reading − mean)², also called the variance | How much the device is moving or shaking |
| Others you may add | Minimum, maximum, peaks per second, frequency features | Speed of a rhythm, sharp hits, steps |
import numpy as np
rng = np.random.default_rng(1)
t = np.arange(50) / 50
still = np.column_stack([rng.normal(0, 0.02, 50), rng.normal(0, 0.02, 50), 1 + rng.normal(0, 0.02, 50)])
walk = still.copy()
walk[:, 2] += 0.3 * np.sin(2 * np.pi * 1.8 * t)
def features(window):
mean = window.mean(axis=0) # where gravity points
energy = ((window - mean) ** 2).mean(axis=0) # how much it moves
return np.concatenate([mean, energy])
np.set_printoptions(precision=4, suppress=True)
print("still: ", features(still))
print("walking:", features(walk))Choosing a small classifier
| Model | Why it suits a tiny chip | Watch out for |
|---|---|---|
| Decision tree (small depth) | Just a few comparisons; easy to read and to write in C | Deep trees overfit |
| Logistic regression | One weight per feature per class; very small | Only draws straight boundaries |
| Small neural network | Learns more complex patterns; well supported by TinyML runtimes | Needs more data and care |
Honest testing
- 1
Split before training
Keep a test set from other people or sessions, and do not look at it while you tune. - 2
Train on the training set
Fit the model and choose settings using only training data (or a separate validation set). - 3
Test once
Measure accuracy on the held-out test set. - 4
Read the confusion matrix
See which classes get mixed up. That tells you what data or features to add.
Watch and read more
Lab
A feature function that lets a small decision tree reach 90% or more on held-out data.
The problem
The starter makes made-up accelerometer windows for four classes: flat, tilted, walking and shaking. Training and test data come from different random seeds, so the test windows are new.
- Write
features(window). It gets one window of shape (50, 3) and returns a short list of numbers (between 2 and 12). - Start with only the mean of each axis. Run it and read the confusion matrix. Which classes get confused?
- Add the energy of each axis: the mean of (reading − mean)². Run again.
- Aim for 90% or more held-out accuracy. Write one sentence on what each feature adds.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1The mean-only model confuses "flat" with "walking" and "shaking". Why, and why does energy fix it?Reveal
All three have gravity mainly on the z axis, so their average readings are similar. What differs is movement: flat hardly changes, walking goes up and down, shaking moves a lot on all axes. Energy measures how much the readings move around their mean, so it separates them. The mean still matters for telling flat from tilted.
Q2Your features work in Python, but on the board the model predicts "shaking" for everything. What would you check?Reveal
Compare the feature numbers on the board and in Python for the same recorded window. Common causes: a different unit (raw sensor counts instead of g), a different axis order, a different window length or sampling rate, or a missing scaling step. The model must get exactly the same kind of numbers it was trained on.
Questions people ask
Why not feed the raw window straight into a neural network?
You can, and many TinyML projects do, especially with small convolutional networks. It needs more data and a bigger model. Features are a good first step: small, fast, and easy to understand. Try features first, then compare.
Should I compute features on the chip?
Yes, the same features must be computed on the board, in exactly the same way as in training. If your Python code and your C code compute "energy" differently, the model gets inputs it never saw. Test both on the same window and compare the numbers.
What is a confusion matrix?
A table with the true class in each row and the predicted class in each column. Numbers on the diagonal are correct; everything else shows which class was mistaken for which.
How do I know if my model is overfitting?
If training accuracy is much higher than held-out accuracy, the model learned details of the training data that do not carry over. Use a smaller model (for example a shallower tree), more varied data or fewer features.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.