<commit_before># brain.js [![Build Status](https://img.shields.io/travis/ClaudioAlbertin/brainjs.svg)](https://travis-ci.org/ClaudioAlbertin/brainjs) [![Dependencies](https://img.shields.io/david/ClaudioAlbertin/brainjs.svg)](https://david-dm.org/ClaudioAlbertin/brainjs)

Flexible library for creating, training and analyzing multi-layer feed-forward artificial neural networks.<commit_msg>Add basic usage instructions to README.md<commit_after># brain.js [![Build Status](https://img.shields.io/travis/ClaudioAlbertin/brainjs.svg)](https://travis-ci.org/ClaudioAlbertin/brainjs) [![Dependencies](https://img.shields.io/david/ClaudioAlbertin/brainjs.svg)](https://david-dm.org/ClaudioAlbertin/brainjs)

Flexible library for creating, training and analyzing multi-layer feed-forward artificial neural networks.

## Usage

### Setup

Simply install the package from npm…

    $ npm install brainjs

…and require it in your application:

    const brain = require('brainjs');

### Initialising a network

Networks consist of an array describing the number and size of its layers, a matrix of weights and an activation function used in the neurons.

Vectors and matrices should be given as `sylvester` objects.

    let inputs = new brain.sylvester.Vector([2, 5, 6, 4, 3]);

    let weights = new brain.sylvester.Matrix([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]);

To create a network, pass an array describing the desired layers where every element of the array is an integer specifying the number of neurons in the layer corresponding to the element's index, as well as some weights:

    let network = new brain.Network([2, 4, 1], weights);

This would correspond to the following network topology featuring two input neurons, a single output neuron, a hidden layer consisitig of foursc neurons, as well as bias neurons for every layer but the last one:

        ◆
    ◆   o
    o   o
    o   o   o
        o

The activation function is automatically set to a sigmoid function which is a solid choice for most applications, however it can be changed using `setActivation()`:

    network.setActivation((x) => {
      return Math.round(x) * 2 - 1
    });

When creating a network from scratch you will want to initialise it with random weights. This library provides a handy utility function for this task, which takes an array of layers as its argument:

    let weights = brain.utils.randomWeights([2, 4, 1]);

Alternatively, to load a network that has already been specified, potentially trained, and saved to a JSON file, simply do:

    let network = brain.Network.fromJSON(require('xnor.json'));

### Executing a network

Network execution is straight forward. Given a vector of inputs, calling `run()` on a network returns the computed outputs as another vector:

    let inputs  = new brain.sylvester.Vector([1, 0]);
    let outputs = network.run(inputs);

{'fraction_non_alphanumeric': 0.09358752166377816, 'fraction_numerical': 0.008318890814558059, 'mean_word_length': 4.010416666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2781357', 'n_tokens_mistral': 859, 'n_tokens_neox': 794, 'n_words': 330}