brain-freeze-ml
v2.1.0
Published
A tiny neural network shape trainer that runs entirely in the browser on the CPU backend.
Maintainers
Readme
Brain Freeze ML
A neural network training simulator that runs entirely in the browser. You draw a shape on a pixel grid, tell it what you drew, and watch a small network learn to tell triangles, squares and circles apart.
The name is the design brief: a training app that will not overheat a weak laptop. It runs on the CPU backend, trains in short bursts with idle gaps between them, and holds its memory flat no matter how long you leave it running.
Pages
The app has two pages, switched with the tabs in the header. They are two views of one shared network: what you configure on Settings is what you train on the Train page, and the Train page keeps training in the background while you look at the other one.
- Train — draw and label shapes to train the network. The label buttons are the model's class names, so they follow whatever you named on Settings.
- Settings — name 2 to 10 classes, add reference drawings, and set the engine
and data-file configuration for that same network. Classes and references are
saved in
localStorage, so they survive a reload.
Both network diagrams are physical: the neurons are Matter.js bodies loose in a zero-gravity box the size of the panel. Drag one and it follows the pointer; fling it and it bumps into its neighbours and settles back. The neurons drift forever rather than freezing, so the diagram reads as alive. The engine only runs while the panel is on screen, so an off-screen page costs nothing.
Running it
Open index.html. That is the whole install. There is no build step, no package
manager, and no network access — TensorFlow.js is vendored in lib/ and
loaded with plain <script> tags, so it works from file:// as well as any static
host.
The app is also published to npm for convenience. Installing it pulls the same static files plus a tiny zero-dependency server, so you can serve it locally with one command:
npx brain-freeze-ml
# Brain Freeze ML is running at http://127.0.0.1:8000/The server only reads files out of the package directory and takes an optional
PORT (default 8000) and HOST (default 127.0.0.1). It is not required to
use the app — opening index.html directly works just as well.
How it works
Draw. Paint on the 12x12 grid. Right-drag erases. Random shape generates a
synthetic shape if you would rather not draw.
Label. Click a class button to add what you drew to the training set and train on it immediately. The buttons are the model's class names, so naming your own on Settings changes them. Repeat as often as you like; the training set caps at 300 examples and drops the oldest, so it can never grow without bound.
Watch. The network view lays the network out top to bottom: the drawing, one row per hidden layer, then the class outputs. The rows sit on an even pitch, so the vertical gaps between layers stay equal no matter how many layers there are. Each neuron is a square filled from the bottom in proportion to its activation, so a blank grid reads as an empty network rather than a confident one. Line weight is connection strength and colour is the gradient (blue negative, orange positive). Hover a neuron to see its activation, bias and bias gradient.
Play. The hidden and output neurons are physical bodies in a zero-gravity box. Drag one and it follows; fling it and it collides with the others and comes to rest. Each neuron is tethered to where the layout put it, and the tether is drawn, so you can see it pay out as a neuron drifts and reel back in as it returns. The input grid stays put — it is the anchor the first hidden layer reads from — and the connections are redrawn every frame to follow the squares. The simulation is capped at 30 fps, steps at a fixed rate, and stops entirely once the panel is hidden, so leaving it alone costs no CPU.
Each page is a full-height column that fits the viewport without the page scrolling: the network panel takes the leftover height, and if a short screen cannot fit the lower panels, those panels scroll inside themselves.
Tune. Learning rate, hidden units per layer and hidden layers are live. The
network is one to four hidden layers deep; changing either slider rebuilds it.
Train continuously runs a duty cycle; Train ×200 runs a chunked batch. Both
yield to the browser constantly, so the page stays interactive. Continuous
training also parks itself once the loss stops improving, so a converged network
costs no CPU; it picks up again the moment you label another shape.
Settings
This page configures the same network the Train page trains on, so a change here
reaches the Train page immediately: renaming a class relabels its buttons, moving
a slider rebuilds the shared architecture on both diagrams, and adding a reference
adds a training example you can see in Train's counters. It is configuration only:
it has no drawing board of its own, because you add examples by adding reference
drawings through the Add references modal (which has its own board). Name 2 to
10 classes, then draw a shape for a class and add it as a reference; each reference
is also a training example, so adding one trains the network a little. Applying a
new set of names resizes the output layer and clears the examples, since the old
labels no longer mean the same thing. Names and references are written to
localStorage (key brainfreeze.custom.v1) and restored on load; Forget saved
data clears them. Trained weights are not saved — only the class names and the
drawings you labelled.
The page is two panels. The left one holds the shapes: the class list, its add /
apply / reset buttons, and the reference drawings gallery. The right one holds the
configuration: the neural-engine sliders (learning rate, hidden units per layer,
hidden layers), the Reset model and Forget saved data buttons, and the Data file
buttons that create, open, load and download the model's JSON. The row is sized to
match the Train page's row, so the network diagram above keeps the same height on
both pages.
Why it stays cool
- CPU backend only. WebGL is the actual thermal and memory risk on a weak machine, and this model is far too small to benefit from a GPU.
- Bounded data. Examples are stored as
Float32Arrays and capped at 300. - Everything in
tf.tidy. Ops are freed as they go. - No leaks on rebuild. Changing the hidden size or layer count disposes the old model, its variables and their registry entries, and the optimizer's accumulators.
- Duty cycle. 6 ms of work, 40 ms of rest, so the fan does not spin up.
- Idle physics. The Matter.js world only steps while its panel is visible, and stops entirely when the view is switched away. The motion is perpetual by design, so a visible panel always animates; it steps at a fixed rate capped to 30 fps.
- Guardrails. Tensor and byte ceilings throw and report in the status line rather than quietly eating the machine.
Measured in headless Chromium: 12 to 20 tensors / under 40 KB of TF memory steady after a few hundred steps, 0 tensor drift across 30 architecture rebuilds, and the same flat footprint with both pages loaded (they share one network) as with one. The physics loop was measured to stop making animation-frame requests entirely once the view was switched away.
Files
index.html markup for both pages
styles.css theme
logo.svg the snowflake-neural-network mark (also the favicon)
js/brain.js the model: a network factory, training step, snapshot, probe
js/netview.js the network diagram canvas (shared by both pages) + physics
js/board.js the drawing grid canvas (shared by both pages)
js/app.js the UI: tabs, wiring, training loop
server.js a tiny static server for `npx brain-freeze-ml`
lib/*.min.js TensorFlow.js 4.22.0 (core + cpu backend + layers)
lib/matter.min.js Matter.js 0.20.0, for the draggable networkjs/brain.js exposes BrainFreeze.createNetwork(options), and a ready-made
instance at window.BrainFreeze that both pages drive — Train by drawing on it and
Settings by configuring it. The diagram and board are factories
(BrainFreezeNet, BrainFreezeBoard) so each page gets an independent canvas.
lib/tf-layers.min.js is required, not optional: tf-core plus the CPU
backend gives forward inference only, and the gradient functions needed for
training are registered by the layers bundle.
License
Brain Freeze ML is free software, licensed under the GNU General Public
License, version 3 or later (GPL-3.0-or-later). The full text is in
LICENSE. Each source file carries an SPDX header.
The vendored TensorFlow.js bundles in lib/ are a separate work, copyright
Google LLC, licensed under the Apache License 2.0. They keep their own license
headers and are compatible with GPLv3.
The vendored Matter.js bundle (lib/matter.min.js) is a separate work, copyright
Liam Brummitt, licensed under the MIT License. It keeps its own license header
and is compatible with GPLv3. See NOTICE for both.
