Tuesday, July 11, 2017

Encog-Node: Simple Machine Learning on Node.js

Before I got into serious Machine Learning and Computer Vision coding (which I mostly use Python for), I did a lot of my development on Node.js. Few years back (around 2012), I was trying to add a simple neural network to one of my Node.js applications. I looked around, but couldn't find a satisfactory node module which was lightweight and flexible. Around that time, I came across the Encog Machine Learning framework, which was created by Jeff Heaton, and was one of the most popular Machine Learning libraries for Java at the time. I noticed that there was a Javascript version of the Encog library, which worked surprisingly well, and set myself on to porting that to Node.js.

I released the first version of Encog-Node in early 2012, and the latest version v0.3.0 is now available from NPM - https://www.npmjs.com/package/encog-node, and is recommended for anyone who wants to add lightweight, simple machine learning capabilities to their Node.js applications.

GitHub user Rui Cardoso contributed a lot for the latest release, with restructuring and cleaning up the codebase, and adding more examples.

You can install it by simply running,
 npm install encog-node  
in your node application.


Here's an example code to get a simplest neural network working on Node.js
 var ENCOG = require('encog-node');  
    
 var XOR_INPUT = [  
   [0, 0],  
   [1, 0],  
   [0, 1],  
   [1, 1]  
 ];  
    
 var XOR_IDEAL = [  
   [0],  
   [1],  
   [1],  
   [0]  
 ];  
    
 var network = ENCOG.networks.basic.create([  
   ENCOG.layers.basic.create(ENCOG.activationFunctions.sigmoid.create(), 2, 1),  
   ENCOG.layers.basic.create(ENCOG.activationFunctions.sigmoid.create(), 3, 1),  
   ENCOG.layers.basic.create(ENCOG.activationFunctions.sigmoid.create(), 1, 0)  
 ]);  
    
 network.randomize();  
    
 var train = ENCOG.trainers.propagation.create(network, ENCOG.errorFunctions.linear.create(), XOR_INPUT, XOR_IDEAL, "RPROP", 0, 0);  
    
 var iteration = 1;  
    
 do {  
   train.iteration();  
   var trainResultString = "Training Iteration #" + iteration + ", Error: " + train.error;  
   console.log(trainResultString + "\n");  
   iteration++;  
 } while (iteration < 1000 && train.error > 0.01);  
    
 var input = [0, 0];  
 var output = [];  
    
 console.log("Testing neural network: \n");  
    
 for (var i = 0; i < XOR_INPUT.length; i++) {  
   output = network.compute(XOR_INPUT[i]);  
   var testResultString = "Input: " + String(XOR_INPUT[i][0]) +  
     " ; " + String(XOR_INPUT[i][1]) +  
     "  Output: " + String(output[0]) +  
     "  Ideal: " + String(XOR_IDEAL[i][0]);  
   console.log(testResultString + "\n");  
 }  

Which will output,
 >node index.js  
 Training Iteration #1, Error: 0.33306242864283925  
 Training Iteration #2, Error: 0.30684930995968274  
 Training Iteration #3, Error: 0.2816136873215376  
 Training Iteration #4, Error: 0.2614275886340755  
 ..........  
 ..........  
 ..........  
 Training Iteration #44, Error: 0.010807377445510056  
 Training Iteration #45, Error: 0.005187735146628829  
 Testing neural network  
 Input: 0 ; 0  Output: 0.000056493461985276595  Ideal: 0  
 Input: 1 ; 0  Output: 0.9995493238264583  Ideal: 1  
 Input: 0 ; 1  Output: 0.9987763730629743  Ideal: 1  
 Input: 1 ; 1  Output: 0.08974271940228784  Ideal: 0  

More examples and instructions available at the NPM Page of the Encog-Node package.
And, you can get the source code from the GitHub page.

Feel free to use, test, improve, and contribute to the code.

Related links:


Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.

Get your copy now!

4 comments:

  1. Wonderful Blog!!! Your post is very informative about AI technology. Thank you for sharing the article with us.

    Regards:

    Machine Learning Course in Chennai | Machine Learning Course

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

    ReplyDelete
  3. Really useful information. we are providing best data science online training from industry experts.

    ReplyDelete
  4. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Also Visit this site for Machine Learning with Python Training

    ReplyDelete