Skip to content

Latest commit

ย 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  LeCun 1989 CNN

Reproduction of One of the First Convolutional Neural Networks

Live Demo Paper Based On


Node.js JavaScript Zero Dependencies License Parameters Test Error


LeCun 1989 CNN โ€” Architecture, Training Curves & Misclassified Digits

Left: Network architecture diagram | Center: Training loss & error curves | Right: Misclassified test samples


๐Ÿ“– About

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.


โœจ Features

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

๐Ÿ—๏ธ Network Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   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

๐Ÿ”‘ Key Design Decisions (1989)

  • ๐Ÿ”— 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)

๐Ÿ“Š Results

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.


๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+ (uses ESM modules, native fetch, zlib)

1๏ธโƒฃ Clone

git clone https://github.com/romizone/lecun1989-cnn.git
cd lecun1989-cnn

2๏ธโƒฃ Download & Preprocess MNIST

node prepro.js

This 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/

3๏ธโƒฃ Train the CNN

node repro.js

This will:

  • ๐Ÿ‹๏ธ Train for 23 epochs (~27 seconds on modern hardware)
  • ๐Ÿ“ˆ Print loss & error after each epoch
  • ๐Ÿ’พ Save trained weights to data/weights.json

4๏ธโƒฃ Launch Web Demo

node server.js

Open http://localhost:3000 โ€” draw a digit and see real-time predictions!


๐Ÿ“ Project Structure

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

๐Ÿงช How It Works

Training Pipeline

MNIST (28ร—28) โ”€โ”€โ–ถ Resize (16ร—16) โ”€โ”€โ–ถ Normalize [-1,+1] โ”€โ”€โ–ถ Forward Pass โ”€โ”€โ–ถ MSE Loss
                                                                โ”‚
                                                                โ–ผ
                                          SGD Update โ—€โ”€โ”€ Backward Pass (Manual Gradients)

Browser Inference

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.


๐Ÿ”— References

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

๐Ÿ› ๏ธ Tech Stack

Technology Purpose
Node.js Runtime & training
JavaScript Browser inference
HTML5 Web UI (single file)
CSS3 Dark theme styling
Canvas Drawing & visualization
Vercel Deployment

โญ Star this repo if you find it interesting!

Built with โค๏ธ as a tribute to the pioneers of deep learning.

"The most important thing is to keep the most important thing the most important thing."

About

๐Ÿง  Faithful reproduction of LeCun et al. 1989 CNN in pure Node.js (zero dependencies) โ€” interactive web demo with real-time browser inference

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages