Left: Network architecture diagram | Center: Training loss & error curves | Right: Misclassified test samples
A faithful reproduction of the convolutional neural network described in:
Y. LeCun, B. Boser, J. S. Denker, D. Henderson, R. E. Howard, W. Hubbard, L. D. Jackel "Backpropagation Applied to Handwritten Zip Code Recognition" Neural Computation, Vol. 1, pp. 541โ551, 1989 โ AT&T Bell Laboratories
This was one of the earliest successful applications of backpropagation to a real-world problem. The network was used by the U.S. Postal Service to automatically read handwritten zip codes on envelopes.
Built entirely in pure Node.js with zero dependencies โ no PyTorch, no TensorFlow, no NumPy. Every operation (convolution, backpropagation, SGD) is implemented from scratch.
| Feature | Description | |
|---|---|---|
| ๐ข | Complete CNN Implementation | All 4 layers with manual forward & backward pass |
| ๐ | Training from Scratch | SGD optimizer, MSE loss, 23 epochs (~27 seconds) |
| ๐ | Interactive Web Demo | Draw digits and get real-time predictions in browser |
| ๐ | Kernel Visualization | See what the 12 H1 convolutional filters learned |
| ๐ฆ | Zero Dependencies | Pure Node.js โ no ML frameworks needed |
| ๐ฏ | 4.19% Test Error | Closely matches Karpathy's PyTorch reproduction (4.09%) |
| ๐ฅ | Auto MNIST Download | Preprocessor downloads and prepares data automatically |
| ๐งฎ | Exact 1989 Architecture | Per-unit biases, sparse H2 connectivity, tanh activation |
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโ
โ INPUT โ โ H1 โ โ H2 โ โ H3 โ โ OUTPUT โ
โ 1ร16ร16 โโโโโโถโ 12ร8ร8 โโโโโโถโ 12ร4ร4 โโโโโโถโ 30 โโโโโโถโ 10 โ
โ Image โ โ Conv 5ร5/2 โ โ Sparse 5ร5/2โ โ FC โ โ FC โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโ
1,068 params 2,592 params 5,790 params 310 params
| Layer | Type | Output | Params |
|---|---|---|---|
| Input | Image | 1ร16ร16 | โ |
| H1 | Conv 5ร5, stride 2 | 12ร8ร8 | 1,068 |
| H2 | Sparse Conv 5ร5, stride 2 | 12ร4ร4 | 2,592 |
| H3 | Fully Connected | 30 | 5,790 |
| Output | Fully Connected | 10 | 310 |
| Total | 9,760 |
- ๐ Weight Sharing โ Units in the same feature map share weights, drastically reducing parameters
- ๐บ๏ธ Feature Maps โ H1 has 12 feature maps, each detecting different patterns (edges, corners, etc.)
- ๐ธ๏ธ Sparse Connectivity โ H2 filters connect to only 8 of 12 H1 maps (not all), improving generalization
- ๐ Per-Unit Biases โ Each spatial position has its own bias (shape [12,8,8]), unlike modern CNNs
- โ tanh Activation โ Used throughout (ReLU wasn't popular yet)
- ๐ MSE Loss โ Mean Squared Error with ยฑ1 targets (cross-entropy wasn't standard)
| Metric | ๐ Paper (1989) | ๐ Karpathy (PyTorch) | ๐ Ours (Node.js) |
|---|---|---|---|
| Train Loss | 2.5e-3 | 4.07e-3 | 4.85e-3 |
| Train Error | 0.14% | 0.62% | 0.86% |
| Test Loss | 1.8e-2 | 2.84e-2 | 3.02e-2 |
| Test Error | 5.0% | 4.09% | 4.19% |
| Test Misses | 102 | 82 | 84 |
๐ก Small differences are expected due to different datasets (original USPS zip codes vs. MNIST) and PRNG implementations.
- Node.js 18+ (uses ESM modules, native
fetch,zlib)
git clone https://github.com/romizone/lecun1989-cnn.git
cd lecun1989-cnnnode prepro.jsThis will:
- ๐ฅ Download MNIST from Google Cloud Storage (~11MB)
- ๐ Resize images from 28ร28 โ 16ร16 (bilinear interpolation)
- ๐ฒ Select 7,291 training + 2,007 test samples (seed 1337)
- ๐พ Save binary files to
data/
node repro.jsThis will:
- ๐๏ธ Train for 23 epochs (~27 seconds on modern hardware)
- ๐ Print loss & error after each epoch
- ๐พ Save trained weights to
data/weights.json
node server.jsOpen http://localhost:3000 โ draw a digit and see real-time predictions!
lecun1989-cnn/
โโโ ๐ prepro.js # MNIST download & preprocessing
โโโ ๐ repro.js # CNN training (forward + backward pass)
โโโ ๐ server.js # Simple HTTP server for web demo
โโโ ๐ vercel.json # Vercel deployment config
โโโ ๐ package.json # Project metadata (zero deps)
โโโ ๐ lib/
โ โโโ ๐ tensor.js # N-dimensional tensor implementation
โ โโโ ๐ rng.js # Seedable PRNG (Mulberry32)
โโโ ๐ web/
โ โโโ ๐ index.html # Single-file web UI (HTML + CSS + JS)
โโโ ๐ data/
โ โโโ ๐ weights.json # Trained model weights (~200KB)
โโโ ๐ lecun-89e.pdf # Original 1989 paper
โโโ ๐ lecun1989.png # Demo screenshot
MNIST (28ร28) โโโถ Resize (16ร16) โโโถ Normalize [-1,+1] โโโถ Forward Pass โโโถ MSE Loss
โ
โผ
SGD Update โโโ Backward Pass (Manual Gradients)
Canvas (280ร280) โโโถ Resize (16ร16) โโโถ Normalize [-1,+1] โโโถ Forward Pass โโโถ Softmax โโโถ Prediction
โ
weights.json (200KB)
The entire forward pass runs client-side in JavaScript โ no server round-trip needed for predictions.
| Resource | Link | |
|---|---|---|
| ๐ | Original Paper | LeCun et al., 1989 |
| ๐ | Karpathy's PyTorch Reproduction | karpathy/lecun1989-repro |
| ๐๏ธ | MNIST Dataset | Yann LeCun's MNIST Page |
| ๐ | Live Demo | lecun1989-cnn.vercel.app |
| Technology | Purpose |
|---|---|
| Runtime & training | |
| Browser inference | |
| Web UI (single file) | |
| Dark theme styling | |
| Drawing & visualization | |
| Deployment |