Navleen Singh Thukral

← Selected projects

A neural-network library written from scratch in JavaScript

A small neural-network library with backpropagation written by hand, and three demos I used to test and tune it: XOR, Iris classification on held-out data, and a color classifier that keeps learning from your clicks.

Read the code on GitHub

My role
Sole author
Context
Personal project
Period
Oct to Nov 2018
Stack
JavaScript, linear-algebra, seedrandom, p5.js, d3, Chart.js
Repository
SNavleen/Simple-NeuralNetwork-Library

Why I built it

The repository describes it as a simple neural-network library to help me and beginners learn the basics of neural networks. I wrote the network and its training by hand, then built demos around it to see whether it actually learned.

The library

  • About 230 lines in one file, with matrix operations from a linear-algebra library.
  • Input and output sizes are set when you create a network, and hidden layers are added one call at a time.
  • Weights get a bias term and seeded initialisation, so a run can be repeated exactly. Activations are sigmoid or tanh. A min-max normalisation helper scales inputs.
  • Backpropagation runs from the output layer back, with an adjustable learning rate (default 0.1).

Three demos, three kinds of test

  • XOR, the classic problem a single-layer network cannot solve. A 2-2-2 network learns it and draws its decision surface as a grid, so I could see the boundary form.
  • Iris, the real test. Four measurements in, three species out, with hidden layers of 8 and 6 nodes. It trains on one CSV and is scored on a separate test CSV the network has never seen, with the cost drawn as a chart.
  • A color classifier. A 3-input network learns a white-or-black label from a million generated examples, then keeps training from your clicks, where you tell it which label is right.

What the commit history shows

  • I added tanh after sigmoid and made normalisation explicit once inputs were raw colour values.
  • Testing on the Iris held-out data, I noticed the network was classifying versicolor and virginica the same way. I noted in the commit that their measurements are close together, and went on to tune the learning rate, hidden-layer sizes and number of training steps.
  • I picked 5,000 training steps by trying the extremes: 1,000 was too few for the network to guess with confidence, and 10,000 was much better but slowed the run down.
  • I fixed a seeding issue that made every run start with the same training set.

Limits

Training runs one example at a time. Mini-batches are a documented TODO in the code, and the library was built for learning, not for production use. The repository records no accuracy numbers for the Iris run.