Basic Neural Network

JavaScript

//http://www.codeproject.com/Articles/14342/Designing-And-Implementing-A-Neural-Network-Library

const ITERATIONS = 5000;
const NETWORK_LAYERS = [2, 5, 7 , 5 , 1];

const ANIMATE_LEARNING = true;
const ANIMATION_DELAY = 1;  // Time to wait between each frame in milliseconds

const LEARNING_RATE = 0.5;

const LABEL_BG = "rgba(250, 220, 100, 0.9)";
const DATA_BG = "rgba(150, 255, 150, 0.9)";

class Connection {
	constructor(neuron) {
		this.weight = Math.random();
		this.neuron = neuron;
	}
};

class Neuron {
	constructor(id) {
		this.label = "N" + id;
		this.bias = Math.random();
		this.out;
		this.inputs = []; // Neurons that provide values to this neuron
		this.outputs = []; // Neurons that this neuron provides a value to
	}

	updateOutput() {
		// Summation unit
		let netValue = this.bias;

		this.inputs.forEach(connection => {
			netValue += connection.weight * connection.neuron.out;
		});

		// Transfer (sigmoid)
		this.out = 1 / (1 + Math.exp(-netValue));
	}

	updateDelta(error) {
		this.delta = this.out * (1 - this.out) * error;
	}

	updateFreeParams() {
		this.bias += LEARNING_RATE * 1 * this.delta;

		this.inputs.forEach(connection => {
			connection.weight += LEARNING_RATE * 1 * connection.neuron.out * this.delta;
		});
	}
}

class Network {
	constructor(layerSizes) {
		// Create and populate each layer with neurons
		let neuronId = 0;
		this.layers = layerSizes.map(size => {
			return new Array(size).fill().map(n => {
				return new Neuron(neuronId++);
			});
		});

		// Connect neurons to each neuron in the next layer
		for (let i = 0; i < layerSizes.length; ++i) {
			let neuronLayer = this.layers[i];
			let neuronOutputLayer = this.layers[i + 1];
			let neuronInputLayer = this.layers[i - 1];

			neuronLayer.forEach(neuron => {
				if (neuronOutputLayer) {
					neuron.outputs.push.apply(neuron.outputs, neuronOutputLayer.map(outputNeuron => {
						return new Connection(outputNeuron);
					}));
				}

				if (neuronInputLayer)...