Brain.js
🤖 GPU accelerated Neural networks in JavaScript for Browsers and Node.js
Install / Use
/learn @BrainJS/Brain.jsREADME
brain.js
GPU accelerated Neural networks in JavaScript for Browsers and Node.js
<p style="text-align: center" align="center"><a href="https://brain.js.org"><img src="https://img.shields.io/website?up_message=brain.js.org&url=https%3A%2F%2Fbrain.js.org" alt="GitHub"></a>
<a href="https://twitter.com/brainjsfnd"><img src="https://img.shields.io/twitter/follow/brainjsfnd?label=Twitter&style=social" alt="Twitter"></a>
About
brain.js is a GPU accelerated library for Neural Networks written in JavaScript.
:bulb: This is a continuation of the harthur/brain, which is not maintained anymore. More info
Table of Contents
- Installation and Usage
- Examples
- Training
- Methods
- Failing
- JSON
- Standalone Function
- Options
- Streams
- Utilities
- Neural Network Types
Installation and Usage
NPM
If you can install brain.js with npm:
npm install brain.js
CDN
<script src="//unpkg.com/brain.js"></script>
Download
Download the latest brain.js for browser
Installation note
Brain.js depends on a native module headless-gl for GPU support. In most cases installing brain.js from npm should just work. However, if you run into problems, this means prebuilt binaries are not able to download from GitHub repositories and you might need to build it yourself.
Building from source
Please make sure the following dependencies are installed and up to date and then run:
npm rebuild
System dependencies
Mac OS X
Ubuntu/Debian
- A supported version of Python
- A GNU C++ environment (available via the
build-essentialpackage onapt) - libxi-dev
- Working and up-to-date OpenGL drivers
- GLEW
- pkg-config
sudo apt-get install -y build-essential libglew-dev libglu1-mesa-dev libxi-dev pkg-config
Windows
- A supported version of Python See: https://apps.microsoft.com/store/search/python
- Microsoft Visual Studio Build Tools 2022
- run in cmd:
npm config set msvs_version 2022Note: This no longer works in modern versions of npm. - run in cmd:
npm config set python python3Note: This no longer works in modern versions of npm.
* If you are using Build Tools 2017 then run npm config set msvs_version 2017 Note: This no longer works in modern versions of npm.
Examples
Here's an example showcasing how to approximate the XOR function using brain.js:
more info on config here.
:bulb: A fun and practical introduction to Brain.js
// provide optional config object (or undefined). Defaults shown.
const config = {
binaryThresh: 0.5,
hiddenLayers: [3], // array of ints for the sizes of the hidden layers in the network
activation: 'sigmoid', // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
leakyReluAlpha: 0.01, // supported for activation type 'leaky-relu'
};
// create a simple feed-forward neural network with backpropagation
const net = new brain.NeuralNetwork(config);
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] },
]);
const output = net.run([1, 0]); // [0.987]
or more info on config here.
// provide optional config object, defaults shown.
const config = {
inputSize: 20,
inputRange: 20,
hiddenLayers: [20, 20],
outputSize: 20,
learningRate: 0.01,
decayRate: 0.999,
};
// create a simple recurrent neural network
const net = new brain.recurrent.RNN(config);
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] },
]);
let output = net.run([0, 0]); // [0]
output = net.run([0, 1]); // [1]
output = net.run([1, 0]); // [1]
output = net.run([1, 1]); // [0]
However, there is no reason to use a neural network to figure out XOR. (-: So, here is a more involved, realistic example: Demo: training a neural network to recognize color contrast.
More Examples
You can check out this fantastic screencast, which explains how to train a simple neural network using a real-world dataset: How to create a neural network in the browser using Brain.js.
Training
Use train() to train the network with an array of training data. The network has to be trained with all the data in bulk in one call to train(). More training patterns will probably take longer to train, but will usually result in a network better at classifying new patterns.
Note
Training is computationally expensive, so you should try to train the network offline (or on a Worker) and use the toFunction() or toJSON() options to plug the pre-trained network into your website.
Data format
For training with NeuralNetwork
Each training pattern should have an input and an output, both of which can be either an array of numbers from 0 to 1 or a hash of numbers from 0 to 1. For the color contrast demo it looks something like this:
const net = new brain.NeuralNetwork();
net.train([
{ input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 } },
{ input: { r: 0.16, g: 0.09, b: 0.2 }, output: { white: 1 } },
{ input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 } },
]);
const output = net.run({ r: 1, g: 0.4, b: 0 }); // { white: 0.99, black: 0.002 }
Here's another variation of the above example. (Note that input objects do not need to be similar.)
net.train([
{ input: { r: 0.03, g: 0.7 }, output: { black: 1 } },
{ input: { r: 0.16, b: 0.2 }, output: { white: 1 } },
{ input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 } },
]);
const output = net.run({ r: 1, g: 0.4, b: 0 }); // { white: 0.81, black: 0.18 }
For training with RNNTimeStep, LSTMTimeStep and GRUTimeStep
Each training pattern can either:
- Be an array of numbers
- Be an array of arrays of numbers
Example using an array of numbers:
const net = new brain.recurrent.LSTMTimeStep();
net.train([[1, 2, 3]]);
const output = net.run([1, 2]); // 3
Example using an array of arrays of numbers:
const net = new brain.recurrent.LSTMTimeStep({
inputSize: 2,
hiddenLayers: [10],
outputSize: 2,
});
net.train([
[1, 3],
[2, 2],
[3, 1],
]);
const output = net.run([
[1, 3],
[2, 2],
]); // [3, 1]
For training with RNN, LSTM and GRU
Each training pattern can either:
- Be an array of values
- Be a string
- Have an
inputand anoutput- Either of which can have an array of values or a string
CAUTION: When using an array of values, you can use ANY value, however, the values are represented in the neural network by a single input. So the more _distinct valu
Related Skills
gh-issues
339.1kFetch GitHub issues, spawn sub-agents to implement fixes and open PRs, then monitor and address PR review comments. Usage: /gh-issues [owner/repo] [--label bug] [--limit 5] [--milestone v1.0] [--assignee @me] [--fork user/repo] [--watch] [--interval 5] [--reviews-only] [--cron] [--dry-run] [--model glm-5] [--notify-channel -1002381931352]
node-connect
339.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
oracle
339.1kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
tmux
339.1kRemote-control tmux sessions for interactive CLIs by sending keystrokes and scraping pane output.

