Wondering what’s next for npm?Check out our public roadmap! »

    novabrain

    0.8.7 • Public • Published

    Novabrain

    Novabrain is a javascript neural network library for Node.js and browser. This library implements a multilayer perceptron network that you can train to learn XOR, OR, AND ... for example.

    Perceptron

    In Node.js

    You can install Novabrain with npm

    $ npm install novabrain --save
    
    var Novabrain = require('novabrain');
    var Neuron    = Novabrain.Neuron;
    var Layer     = Novabrain.Layer;
    var Network   = Novabrain.Network;
    var Trainer   = Novabrain.Trainer;
    var Transfer  = Novabrain.Transfer;
    var Samples   = Novabrain.Samples;

    In the browser

    You can also use the minified version to increase your web page loading

    <script type="text/javascript" src="novabrain.js"></script>
    <script type="text/javascript">
        (function() {
        
            var network = new Novabrain.Network(2,1);
            
            network.import(Novabrain.Samples.XOR.config);
            
            network.transfer = Novabrain.Transfer.BOOLEAN;
            
            console.log([0,0], network.output([0,0])); // [false]
            console.log([0,1], network.output([0,1])); // [true]
            console.log([1,0], network.output([1,0])); // [true]
            console.log([1,1], network.output([1,1])); // [false]
            
        })();
    </script> 

    Create a network

    Constructor expected an intergers suite. The first value is the input size The last value is the output size Between this values you can set many hidden size (2, 3, ..., 1)

    new Novabrain.Network(2,1);
    new Novabrain.Network(2,3,1);
    new Novabrain.Network(5,4,4,2);

    Samples

    Novabrain samples contains training and config for basics functions

    Novabrain.Samples.XOR
    Novabrain.Samples.AND
    Novabrain.Samples.OR

    Back Propagation Training

    This example shows how the neural network is trained to learn XOR

    var network = new Novabrain.Network(2,1);
    var trainer = new Novabrain.Trainer(network);
     
    trainer.train([
        { input: [0,0], output: [0] },
        { input: [0,1], output: [1] },
        { input: [1,0], output: [1] },
        { input: [1,1], output: [0] },
    ]);
     
    console.log([0,0], network.output([0,0])); // [~0.05]
    console.log([0,1], network.output([0,1])); // [~0.93]
    console.log([1,0], network.output([1,0])); // [~0.93]
    console.log([1,1], network.output([1,1])); // [~0.09]
     
    network.transfer = Novabrain.Transfer.BOOLEAN;
     
    console.log([0,0], network.output([0,0])); // [false]
    console.log([0,1], network.output([0,1])); // [true]
    console.log([1,0], network.output([1,0])); // [true]
    console.log([1,1], network.output([1,1])); // [false]

    Transfer functions

    The transfer functions are used to change the value of the outputs. By default, neurons uses a Logistic Sigmoid transfer. You can change those properties the following way.

    network.transfer = Novabrain.Transfer.BOOLEAN;
     
    console.log([0,0], network.output([0,0])); // [false]
    console.log([0,1], network.output([0,1])); // [true]
    console.log([1,0], network.output([1,0])); // [true]
    console.log([1,1], network.output([1,1])); // [false]

    LOGISTIC
    Return logistic sigmoid values

    HARDLIMIT
    Return 0 or 1 values

    BOOLEAN
    Return boolean values like HARDLIMIT

    IDENTIFY
    Return sum values without transfer

    TANH
    Return values between -1 and 1

    Export and import data

    var n1 = new Novabrain.Network(2,1);
    var n2 = new Novabrain.Network(2,1);
     
    n2.import(n1);
    // or
    n2.import(n1.export());
     
    var results = n2.output([...]));

    Create a standalone function

    By default the transfer function used is LOGISITC but you can change this by two ways. Define your custom transfer before the standalone function export or set the transfer param when you use the standalone function.

    var standalone = network.standalone();
    var booleanResults = standalone([...], Novabrain.Transfer.BOOLEAN));
    var standalone = network.standalone(Novabrain.Transfer.BOOLEAN);
    var booleanResults = standalone([...]));
    var tanhResults = standalone([...], Novabrain.Transfer.TANH));

    Mocha is used for unit testing

    $ npm test
    
    $ make tests
    
    $ npm install mocha -g
    $ mocha
    

    Contribute

    Novabrain is an Open Source project started in France by François Mathey. Anybody is welcome to contribute to the development of this project.

    If you want to contribute feel free to send PR's, just make sure to run the make before submiting it. This way you'll run all the test specs and build the web distribution files.

    $ make
    

    Thank you <3

    Install

    npm i novabrain

    DownloadsWeekly Downloads

    102

    Version

    0.8.7

    License

    MIT

    Last publish

    Collaborators

    • avatar