Build an AI Neural Network with Brain.js
Learn how to easily build, train, and test your own neural networks using Brain.js
Introduction to Brain.js
Brain.js is a popular JavaScript library that allows developers to build and train neural networks directly in their web browser or on Node.js applications. It provides an easy-to-use interface for creating and training networks for tasks such as pattern recognition, classification, prediction, and more. By abstracting the complexities of neural networks, Brain.js enables both beginners and experienced developers to incorporate machine learning into their projects.
With its lightweight nature, Brain.js is suitable for running in both the browser and Node.js environments. In this article, we will guide you through the process of installing and using Brain.js in both of these environments, step-by-step.
Table of Contents:
Installing Brain.js in the Browser
Using a CDN
Using a Module Bundler
Installing Brain.js with Node.js
Prerequisites
Installing via npm or yarn
Example usage in Node.js
Conclusion
1. Installing Brain.js in the Browser
A. Using a CDN (Content Delivery Network)
The simplest way to get started with Brain.js in the browser is by linking directly to the library through a CDN. This method doesn't require any build tools and allows you to include Brain.js in any HTML project with minimal setup.
Steps:
Open your HTML file.
Add the following
<script>tag in the<head>section of your HTML document:
<script src="https://cdn.jsdelivr.net/npm/brain.js@2.0.0-alpha.1/dist/brain-browser.min.js"></script>Now, you can start using Brain.js in your script. Here’s a simple example that creates a neural network and trains it:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brain.js Example</title>
<script src="https://cdn.jsdelivr.net/npm/brain.js@2.0.0-alpha.1/dist/brain-browser.min.js"></script>
</head>
<body>
<h1>Brain.js in the Browser</h1>
<script>
// Create a simple feedforward neural network
const net = new brain.NeuralNetwork();
// Training data: XOR problem
const trainingData = [
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] }
];
// Train the network
net.train(trainingData);
// Test the network
const output = net.run([1, 0]);
console.log(output); // Should output something close to [1]
</script>
</body>
</html>In this example, the neural network is trained on the XOR problem, a common benchmark for testing simple neural networks.
B. Using a Module Bundler (e.g., Webpack)
If you are using modern JavaScript with ES modules and a build system, you may want to install Brain.js via npm or yarn and bundle it using Webpack or another bundler.
Steps:
Create a new project (if you don’t have one already).
Install Brain.js via npm or yarn:
npm install brain.jsor
yarn add brain.jsIn your JavaScript file, import Brain.js:
import brain from 'brain.js';Now, you can use the library just like in the Node.js example below.
After setting up your bundler, your browser will automatically load the compiled output when you open the HTML file.
2. Installing Brain.js with Node.js
To use Brain.js on the server side with Node.js, you need to install it via a package manager like npm or yarn. This allows you to take full advantage of the library in your backend applications, including more advanced features and performance optimizations.
A. Prerequisites
Ensure you have Node.js and npm installed on your system. If you don't have them installed, you can download them from the official Node.js website: https://nodejs.org/
You can check if they are installed by running the following commands in your terminal:
node -v
npm -vB. Installing Brain.js
In your project directory, run the following command to install Brain.js via npm:
npm install brain.jsAlternatively, if you prefer using yarn:
yarn add brain.jsC. Example Usage in Node.js
Once installed, you can use Brain.js to build and train neural networks. Here's a simple example that trains a neural network to solve the XOR problem.
Steps:
Create a new JavaScript file, e.g.,
app.js.Add the following code to
app.js:
const brain = require('brain.js');
// Create a simple feedforward neural network
const net = new brain.NeuralNetwork();
// XOR training data
const trainingData = [
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] }
];
// Train the network
net.train(trainingData);
// Test the network
const output = net.run([1, 0]);
console.log(`Network output for [1, 0]: ${output}`);Run the file using Node.js:
node app.jsYou should see an output similar to:
Network output for [1, 0]: [0.993]This shows that the neural network is successfully learning the XOR function.
3. Conclusion
Brain.js is a powerful and easy-to-use JavaScript library that allows you to integrate neural networks into both browser and Node.js applications. In this guide, we've walked through how to install Brain.js in both environments, providing you with the tools to get started with machine learning in JavaScript.
In the browser: You can use Brain.js with a CDN for simple setups or with a module bundler for more advanced workflows.
With Node.js: You can install the library using npm or yarn and run neural networks in server-side applications.
With these installation methods in place, you're now ready to explore more advanced use cases of neural networks with Brain.js, such as image recognition, natural language processing, and much more. Happy coding!

