Simple JS Neural Network

Information

A functional port of my C++ neural network. This version is written in JavaScript and can be used in more environments.

The repository also gives a working demonstration of functionality. Practice has shown it works well and is still speedy!

Example usage

var NN = new NeuralNetwork(2,2,1);

function resetNN()
{
	var hiddenneurons = parseInt($("#nn1").val());
	NN = new NeuralNetwork(2,hiddenneurons,1);
	NN.SaveToLocalStorage();
}

function runInput()
{
	var input1 = parseFloat($("#input1").val());
	var input2 = parseFloat($("#input2").val());
	var input = [input1, input2];
	var desired = [selectFunc()(input1, input2)];
	var res = NN.TrainNetwork(input, desired);
	$("#output").html("Test: Expected " + desired + ", got " + res + " (" +(res[0] > 0.5 ? "true" : "false") + ")");
}

function selectFunc()
{
	var opt = $("#func").val();
	if(opt == "avg")
    	return function(i1,i2) { return (i1+i2)/2; };
	if(opt == "or")
		return function(i1,i2) { 
			var a1 = (i1>0.5) ? true : false;
			var a2 = (i2>0.5) ? true : false;
			return a1 || a2;
		};
	if(opt == "and")
		return function(i1,i2) { 
			var a1 = (i1>0.5) ? true : false;
			var a2 = (i2>0.5) ? true : false;
			return a1 && a2;
		};
	if(opt == "xor")
		return function(i1,i2) { 
			var a1 = (i1>0.5) ? true : false;
			var a2 = (i2>0.5) ? true : false;
			return a1 ^ a2;
		};
}

function automate(visual)
{
	var TCount = parseInt($("#tcount").val());
	var func = selectFunc() ;
	if(visual)
	{
		runRandomData(0,TCount, true, func);
	} else {
		for(var i=0;i<TCount;i++)
		{
			runRandomData(i,TCount, false, func );
		}
	}
}

GitHub

Screenshots