Click here to Skip to main content
15,881,812 members
Articles / Artificial Intelligence / Machine Learning

JavaScript Machine Learning and Neural Networks with Encog

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
16 Oct 2012Apache18 min read 88.4K   1.4K   27  
Use Encog genetic algorithms, simulated annealing, neural networks and more with HTML5 Javascript.
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<h3>Current Stats</h3>
<p id="pStats"></p>
<h3>Create Network</h3>
Input Layer Count: <input type="text" id="txtInputCount" value="2"/><br>
Hidden Layer 1 Count: <input type="text" id="txtHidden1Count" value="3"/><br>
Hidden Layer 2 Count: <input type="text" id="txtHidden2Count" value="0"/><br>
Output Layer Count: <input type="text" id="txtOutputCount" value="1"/><br>
<input type="submit" id="btnCreateNetwork" value="Create Network">
<input type="submit" id="btnEvaluateNetwork" value="Evaluate Network">
<input type="submit" id="btnTrainNetwork" value="Train Network">
<h3>Input/Output</h3>
<textarea id="textIO" rows="5" cols="40"></textarea><br>
<input type="button" id="btnReadNetwork" value="Read Network"><br>
<input type="button" id="btnWriteNetwork" value="Write Network"><br>
<input type="button" id="btnReadTraining" value="Read Training"><br>
Input Count: <input id="inputCount" value="2"><br>
<h1>Output</h1>
<div id="out"> </div>

<script src="../encog.js"></script>
<script src="../encog-widget.js"></script>

<script type="text/javascript">
    var textIO, pStats;
    var btnCreateNetwork, btnReadNetwork, btnWriteNetwork,btnReadTraining, btnEvaluateNetwork, btnTrainNetwork;
    var txtInputCount,txtHidden1Count,txtHidden2Count,txtOutputCount;
    var network = null, inputData = null, idealData = null;

    if(window.addEventListener) {
        window.addEventListener('load', function () {

        function init() {
            var con = ENCOG.GUI.Console.create('out');

            textIO = document.getElementById('textIO');

            pStats = document.getElementById('pStats');
            btnCreateNetwork = document.getElementById('btnCreateNetwork');
            btnReadNetwork = document.getElementById('btnReadNetwork');
            btnWriteNetwork = document.getElementById('btnWriteNetwork');
            btnReadTraining = document.getElementById('btnReadTraining');
            btnEvaluateNetwork = document.getElementById('btnEvaluateNetwork');
            btnTrainNetwork = document.getElementById('btnTrainNetwork');

            txtInputCount= document.getElementById('txtInputCount');
            txtHidden1Count= document.getElementById('txtHidden1Count');
            txtHidden2Count= document.getElementById('txtHidden2Count');
            txtOutputCount= document.getElementById('txtOutputCount');

            btnCreateNetwork.addEventListener('click', ev_createNetwork, false);
            btnReadNetwork.addEventListener('click', ev_readNetwork, false);
            btnWriteNetwork.addEventListener('click', ev_writeNetwork, false);
            btnReadTraining.addEventListener('click', ev_readTraining, false);
            btnEvaluateNetwork.addEventListener('click', ev_evaluateNetwork, false);
            btnTrainNetwork.addEventListener('click', ev_trainNetwork, false);

            //var csv = ENCOG.ReadCSV.create();
            //csv.readCSV(csvText.value,2,1);

            con.writeLine('One');
            con.writeLine('Two');
            con.writeLine('Three');

            updateStats();
        }

        function ev_readNetwork(ev)
        {
            network = ENCOG.EGFILE.load(textIO.value);
            updateStats();
        }
        function ev_writeNetwork(ev)
        {
            if( network==null ) {
                textIO.value = 'No network defined';
            } else {
                textIO.value = ENCOG.EGFILE.save(network);
            }
        }
        function ev_readTraining(ev)
        {
            var csv, inputCount;

            inputCount = parseInt(txtInputCount.value);
            csv = ENCOG.ReadCSV.create();
            csv.readCSV(textIO.value,inputCount);
            inputData = csv.inputData;
            idealData = csv.idealData
            updateStats();

        }
        function ev_evaluateNetwork(ev)
        {
            var e;

            if( network==null || inputData==null ) {
                alert('Must have network and training data to do that.');
                return;
            }
            e = network.evaluate(inputData,idealData);
            alert("Error: " + e);
        }
        function ev_trainNetwork(ev)
        {

        }

        function updateStats()
        {
            var str = "";

            str+='Network: ';
            if( network == null ) {
                str+= 'Undefined';
            } else {
                str+= 'Input Count: ' + network.inputCount + ', Output Count: ' + network.outputCount;
            }
            str+='<br>Training Data: ';

            if( inputData==null ) {
                str+= 'Undefined';
            } else {
                str+= 'Elements: ' + inputData.length
                if( inputData.length>0 ) {
                    str+= ', Input Count: ' + inputData[0].length + ', Ideal Count: ' + idealData[0].length;
                }
            }


            pStats.innerHTML = str;
        }

        function ev_createNetwork(ev) {
            var inputCount, outputCount, hidden1Count, hidden2Count, layers;

            inputCount = parseInt(txtInputCount.value);
            hidden1Count = parseInt(txtHidden1Count.value);
            hidden2Count = parseInt(txtHidden2Count.value);
            outputCount = parseInt(txtOutputCount.value);

            if( hidden1Count==0 && hidden2Count==0 ) {
                layers =
                        [
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),inputCount,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),outputCount,0)];
            } else if( hidden1Count>0 && hidden2Count==0 ) {
                layers =
                        [
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),inputCount,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),hidden1Count,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),outputCount,0)];
            } else if( hidden1Count==0 && hidden2Count>0 ) {
                layers =
                        [
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),inputCount,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),hidden2Count,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),outputCount,0)];
            } else if( hidden1Count>0 && hidden2Count>=0 ) {
                layers =
                        [
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),inputCount,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),hidden1Count,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),hidden1Count,1),
                            ENCOG.BasicLayer.create(ENCOG.ActivationSigmoid.create(),outputCount,0)];
            }


            network = ENCOG.BasicNetwork.create( layers );
            network.randomize();

            updateStats();
        }

        init();
    }, false); }
</script>

            </body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Publisher
United States United States
Jeff Heaton, Ph.D., is a data scientist, an adjunct instructor for the Sever Institute at Washington University, and the author of several books about artificial intelligence. Jeff holds a Master of Information Management (MIM) from Washington University and a PhD in computer science from Nova Southeastern University. Over twenty years of experience in all aspects of software development allows Jeff to bridge the gap between complex data science problems and proven software development. Working primarily with the Python, R, Java/C#, and JavaScript programming languages he leverages frameworks such as TensorFlow, Scikit-Learn, Numpy, and Theano to implement deep learning, random forests, gradient boosting machines, support vector machines, T-SNE, and generalized linear models (GLM). Jeff holds numerous certifications and credentials, such as the Johns Hopkins Data Science certification, Fellow of the Life Management Institute (FLMI), ACM Upsilon Pi Epsilon (UPE), a senior membership with IEEE. He has published his research through peer reviewed papers with the Journal of Machine Learning Research and IEEE.

Comments and Discussions