← Back to harthur/brain

How to Deploy & Use harthur/brain

Brain Neural Network Library - Deployment and Usage Guide

Prerequisites

  • Node.js - Required for running the library and examples
  • npm - Node package manager for installing dependencies
  • Browser - For browser-based usage (optional)

Installation

Using npm (Node.js)

npm install brain

Using in Browser

  1. Download the latest brain.js from the GitHub releases
  2. Include it in your HTML file:
<script src="brain.js"></script>

Configuration

Training Options

The neural network can be configured with the following options:

var net = new brain.NeuralNetwork({
  hiddenLayers: [4],        // Array specifying hidden layer sizes
  learningRate: 0.6,        // Global learning rate (0-1)
  binaryThresh: 0.5,        // Threshold for binary output
  momentum: 0.1             // Momentum for training (0-1)
});

Training Parameters

When training the network, you can specify:

net.train(data, {
  errorThresh: 0.005,       // Error threshold to reach
  iterations: 20000,        // Maximum training iterations
  log: true,                // Log progress periodically
  logPeriod: 10,            // Iterations between logging
  learningRate: 0.3         // Learning rate (0-1)
});

Build & Run

Node.js Usage

var brain = require('brain');
var net = new brain.NeuralNetwork();

// Train the network
net.train([
  {input: [0, 0], output: [0]},
  {input: [0, 1], output: [1]},
  {input: [1, 0], output: [1]},
  {input: [1, 1], output: [0]}
]);

// Run the network
var output = net.run([1, 0]);  // [0.987]

Browser Usage

<script src="brain.js"></script>
<script>
  var net = new brain.NeuralNetwork();

  net.train([
    {input: [0, 0], output: [0]},
    {input: [0, 1], output: [1]},
    {input: [1, 0], output: [1]},
    {input: [1, 1], output: [0]}
  ]);

  var output = net.run([1, 0]);
  console.log(output);
</script>

Stream Training (Node.js)

var net = new brain.NeuralNetwork();

var trainStream = net.createTrainStream({
  floodCallback: function() {
    flood(trainStream, xor);
  },
  doneTrainingCallback: function(obj) {
    console.log("Trained in " + obj.iterations + " iterations");
    var result = net.run([0, 1]);
    console.log("0 XOR 1: ", result);
  }
});

function flood(stream, data) {
  for (var i = 0; i < data.length; i++) {
    stream.write(data[i]);
  }
  stream.write(null);
}

Deployment

Static Website Deployment

For browser-based applications using Brain.js:

  1. Vercel (formerly Zeit Now)

    • Install Vercel CLI: npm install -g vercel
    • Deploy: vercel
  2. Netlify

    • Connect your GitHub repository
    • Build command: (none needed for static files)
    • Publish directory: .
  3. GitHub Pages

    • Push your HTML file with Brain.js included
    • Enable GitHub Pages in repository settings

Node.js Application Deployment

For Node.js applications using Brain:

  1. Heroku

    heroku create my-brain-app
    git push heroku main
    
  2. AWS Lambda (for serverless inference)

    • Package your trained model using toJSON()
    • Deploy as a Lambda function

Important Note

Training is computationally expensive. For production applications:

  • Train the network offline or on a Worker
  • Use toJSON() or toFunction() to export the trained model
  • Deploy only the trained model for inference

Troubleshooting

Common Issues

1. Network fails to train

Symptoms: Training error remains high (e.g., 0.4) after many iterations Solutions:

  • Check if training data is too noisy
  • Increase hidden layers or nodes: hiddenLayers: [3, 4]
  • Increase training iterations: iterations: 50000
  • Adjust learning rate: learningRate: 0.5

2. Browser performance issues

Symptoms: Training is slow in browser Solutions:

  • Train offline and export model with toJSON()
  • Use Web Workers for training
  • Reduce training data size

3. Input/output format errors

Symptoms: "Cannot read property of undefined" or NaN outputs Solutions:

  • Ensure all input/output values are between 0 and 1
  • Use consistent data format (arrays or hashes)
  • Check that input/output dimensions match training data

4. Serialization issues

Symptoms: toJSON() or fromJSON() fails Solutions:

  • Ensure the network is fully trained before serialization
  • Check for circular references in custom objects
  • Use toFunction() for standalone deployment

Performance Tips

  • For large datasets, use stream training with createTrainStream()
  • Cache trained models to avoid retraining
  • Use appropriate hidden layer sizes based on problem complexity
  • Monitor training error to detect overfitting or underfitting

Version Compatibility

  • This project has reached end of development
  • Consider using maintained alternatives like brain.js or convnetjs for new projects
  • Check package.json for exact version dependencies if upgrading