Skip to content

HTML / CSS

Digit Recogniser Machine Learning Tensorflow Javascript And React

I wanted to build a digit recognizer that runs entirely in the browser. No server-side ML. No Python. Just TensorFlow.js and React.

14 Oct 2023 · 2 min read

Digit Recogniser Machine Learning Tensorflow Javascript And React

I wanted to build a digit recognizer that runs entirely in the browser. No server-side ML. No Python. Just TensorFlow.js and React.

Github: https://github.com/ehsangazar/digit-recogniser-machine-learning-tensorflow-javascript

Demo: https://ehsangazar.github.io/digit-recogniser-machine-learning-tensorflow-javascript/

The Core Concepts

Neural networks are systems of interconnected nodes — loosely inspired by biological neurons. You define layers, feed in training data, and the network learns to recognize patterns.

1_qZQ66BqWXkgyJ7b59efKXw.webp

The idea is straightforward: input data goes through hidden layers that transform it, and the output layer produces a prediction. Each layer extracts increasingly abstract features from the input.

You can experiment with this visually using the TensorFlow Playground.

Why CNN?

For image recognition, you want a Convolutional Neural Network (CNN). CNNs are designed specifically for visual data. Each convolutional layer applies filters that detect features — edges, curves, shapes — and passes those features to the next layer.

1_FAAs1UaOlf5wKE3xERw0Cg.webp

A standard neural network treats every pixel as an independent input. A CNN understands spatial relationships — it knows that adjacent pixels matter. That's what makes it effective for recognizing handwritten digits.

Building It

Start with TensorFlow.js:

Bash
npm install @tensorflow/tfjs

Import and set up:

Typescript
import * as tf from '@tensorflow/tfjs'

From here, you define a sequential model with convolutional layers, pooling layers, and dense layers. Train it on the MNIST dataset (70,000 handwritten digit images), and you get a model that can classify digits 0–9.

The model runs entirely client-side. The user draws a digit on a canvas element, the drawing gets resized to 28x28 pixels (MNIST's format), and TensorFlow.js runs inference right in the browser.

The Trade-offs

Benefits: No server cost for inference. Low latency — predictions happen instantly. The entire project is a static site you can host on GitHub Pages.

Costs: Training in the browser is slow compared to Python/GPU setups. The model accuracy on a canvas (where people draw with a mouse) is lower than on clean MNIST data. TensorFlow.js bundles are large — you're shipping a ML runtime to the client.

What I Learned

ML in the browser is viable for small models and demos. For production ML, you'd train on a server and deploy the model for client-side inference only. But as a learning exercise, building the entire pipeline in JavaScript gave me a much deeper understanding of how neural networks work than any tutorial did.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading