Code Monkey home page Code Monkey logo

nnetjs's Introduction

nNetJs

Neural Networks for javaScript; a self-contained library

Written for general use in web-based aplications.

This is the openSource version.

###Example 1

We construct a 5-layers deep neural network, outputing a numeric 3-vector, and train it for a 1000 steps.

//generate Random data for demo
//input -> 5 x 50 matrix, 1x50 is one case
var input=getRandomBatchIn(5,50)
//output -> 5x3 matrix, 1x3 is one case
var output=getRandomBatchOut(5,3,{mean:10,variance:5});
var data={in:input,out:output};
//declare the net
var n=new net();
//specify its' structure
n.add({shape:[1,50],type:"input"});
n.add({shape:[50,10],type:"fc",activation:"sigmoid"});
n.add({shape:[10,10],type:"fc",activation:"reLu"});
n.add({shape:[10,10],type:"fc",activation:"sigmoid"});
n.add({shape:[10,10],type:"fc",activation:"sigmoid"});
n.add({shape:[10,3],type:"fc",activation:"id"});
//initialize neurons (uses variance normalization by layer length)
n.init(gaussian(0, 1));
//train for 1000 steps
for(var k=0;k<1000;k++) n.trainStep({data:data,cost:"meanSquare",stepSize:0.1});
//evaluate on all 5 cases
var prediction=[];
for(k in input){
    prediction[k]=n.eval(input[k]);
    forAll(prediction[k],function(x){return x.toFixed(2)});
}
//display results
console.log("Correct predictions");
console.log(output);
console.log("\nPredictions:");
console.log(prediction);

###Example 2

We construct a 5-layers deep neural network, outputing a 3-class classification probability, and train it for a 1000 steps.

//generate Random data for demo
//input -> 5 x 50 matrix, 1x50 is one case
var input=getRandomBatchIn(5,50)
//output -> 5x3 matrix, 1x3 is one case
var output=getRandomClassBatch(5,3);
var data={in:input,out:output};
//declare the net
var n=new net();
//specify its' structure
n.add({shape:[1,50],type:"input"});
n.add({shape:[50,10],type:"fc",activation:"sigmoid"});
n.add({shape:[10,10],type:"fc",activation:"reLu"});
n.add({shape:[10,10],type:"fc",activation:"sigmoid"});
n.add({shape:[10,10],type:"fc",activation:"reLu"});
n.add({shape:[10,3],type:"fc",activation:"sigmoid"});
//initialize neurons (uses variance normalization by layer length)
n.init(gaussian(0, 1));
//train for 1000 steps
for(var k=0;k<1000;k++) n.trainStep({data:data,cost:"entropyBinary",stepSize:0.1});
//evaluate on all data
var classification=[];
for(k in input){
    classification[k]=n.eval(input[k]);
    forAll(classification[k],function(x){return x.toFixed(2)});
}
//display results
console.log("Correct predictions");
console.log(output);
console.log("\nPredictions:");
console.log(classification);

Creative Commons License
nNetJs by Žiga Sajovic is licensed under a Creative Commons Attribution 4.0 International License.

nnetjs's People

Contributors

zigasajovic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.