Revisiting My Binary Classification Neural Network with Raw JavaScript

Quite some time ago I implemented a neural network binary classifier in raw JavaScript. One Saturday morning, I was going to walk my two dogs but it was raining so I decided to revisit my code while I waited for the rain to stop.

I didn’t run into any major problems but working with raw JavaScript is always a bit slow. My raw JavaScript neural network can only handle a single hidden layer but even so the results were pretty good.

I used one of my standard binary classification datasets. The data looks like:

1  0.24  1  0  0  0.2950  0  0  1
0  0.39  0  0  1  0.5120  0  1  0
1  0.63  0  1  0  0.7580  1  0  0
. . .

Each line represents a person. The fields are sex (male = 0, female = 1), age (divided by 100), state (Michigan = 100, Nebraska = 010, Oklahoma = 001), income (divided by $100,000) and political leaning (conservative = 100, moderate = 010, liberal = 001). The goal is to predict sex from age, state, income, and politics.

Implementing a neural network from scratch (in any language) is difficult. My implementation is several hundred lines of code long so I can’t present it in its entirety in this blog post.

Loading the training and test data looks like:

let U = require("../../Utilities/utilities_lib.js");
let FS = require("fs");

function main()
{
  console.log("Begin binary classification demo  ");

  // 1. load data
  // 1  0.24  1  0  0  0.2950  0  0  1
  // 0  0.39  0  0  1  0.5120  0  1  0
  let trainX = U.loadTxt3(".\\Data\\people_train.txt",
    "\t", [1,2,3,4,5,6,7,8], "//");
  let trainY = U.loadTxt3(".\\Data\\people_train.txt", 
    "\t", [0], "//");

  let testX = U.loadTxt3(".\\Data\\people_test.txt",
    "\t", [1,2,3,4,5,6,7,8], "//");
  let testY = U.loadTxt3(".\\Data\\people_test.txt", 
    "\t", [0], "//");
. . .

And creating and training the network is:

  // 2. create network
  console.log("Creating 8-100-1 tanh, sigmoid NN ");
  let seed = 0;
  let nn = new NeuralNet(8, 100, 1, seed);

  // 3. train network
  let lrnRate = 0.01;
  let maxEpochs = 10000;
  console.log("Starting train learn rate = " +
    lrnRate.toString());
  nn.train(trainX, trainY, lrnRate, maxEpochs);
  console.log("Done ");
. . .

Notice that I made train() a method that belongs to the NeuralNet class rather than a standalone function that accepts a neural net object. Design decisions like this are often more difficult than coding implementation. Anyway, it was a good mental exercise on a rainy Pacific Northwest morning.



Left: Riley (girl, black and white) and Kevin (boy, brown) waiting for the rain to stop. Right: When dog Llama stopped in from Denver to visit, I made a mini golf hole for her and my two dogs, but none of them were very interested.


This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s