🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to AI Notes
Topic #18

ML Brain.js

Brain.js is a JavaScript library that makes it easy to understand Neural Networks because it hides the complexity of the mathematics.

Building a Neural Network

Building a neural network with Brain.js:

Example:

// Create a Neural Network
const network = new brain.NeuralNetwork();

// Train the Network with 4 input objects
network.train([
 {input:[0,0], output:{zero:1}},
 {input:[0,1], output:{one:1}},
 {input:[1,0], output:{one:1},
 {input:[1,1], output:{zero:1},
]);

// What is the expected output of [1,0]?
result = network.run([1,0]);

// Display the probability for "zero" and "one"
... result["one"] + " " + result["zero"];

Example Explained:

A Neural Network is created with: new brain.NeuralNetwork()

The network is trained with network.train([examples])

The examples represent 4 input values with a corresponding output value.

With network.run([1,0]), you ask "What is the likely output of [1,0]?"

The answer from the network is:

  • one: 93% (close to 1)
  • zero: 6% (close to 0)

How to Predict a Contrast

With CSS, colors can be set by RGB:

Example

<div class="w3-responsive">
<table class="ws-table-all notranslate">
<tr>
<th style="width:50%">Color</th>
<th>RGB</th>
</tr>
<tr><td style="background-color:rgb(0,0,0);color:white">Black</td><td>RGB(0,0,0)</td></tr>
<tr><td style="background-color:rgb(255,255,0)">Yellow</td><td>RGB(255,255,0)</td></tr>
<tr><td style="background-color:rgb(255,0,0);color:white">Red</td><td>RGB(255,0,0)</td></tr>
<tr><td style="background-color:rgb(255,255,255)">White</td><td>RGB(255,255,255)</td></tr>
<tr><td style="background-color:rgb(192,192,192)">Light Gray</td><td>RGB(192,192,192)</td></tr>
<tr><td style="background-color:rgb(65,65,65);color:white">Dark Gray</td><td>RGB(65,65,65)</td></tr>
</table>
</div>

The example below demonstrates how to predict the darkness of a color:

Example:

// Create a Neural Network
const net = new brain.NeuralNetwork();

// Train the Network with 4 input objects
net.train([
// White RGB(255, 255, 255)
{input:[255/255, 255/255, 255/255], output:{light:1}},
// Light grey (192,192,192)
{input:[192/255, 192/255, 192/255], output:{light:1}},
// Darkgrey (64, 64, 64)
{ input:[65/255, 65/255, 65/255], output:{dark:1}},
// Black (0, 0, 0)
{ input:[0, 0, 0], output:{dark:1}},
]);

// What is the expected output of Dark Blue (0, 0, 128)?
let result = net.run([0, 0, 128/255]);

// Display the probability of "dark" and "light"
... result["dark"] + " " + result["light"];

Example Explained:

A Neural Network is created with: new brain.NeuralNetwork()

The network is trained with network.train([examples])

The examples represent 4 input values a corresponding output value.

With network.run([0,0,128/255]), you ask "What is the likely output of dark blue?"

The answer from the network is:

  • Dark: 95%
  • Light: 4%

Why not edit the example to test the likely output of yellow or red?

Want to go beyond the notes?

Join CodingNow 2.0's AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

ML Brain.js – FAQs

Quick answers about learning ML Brain.js in AI.

This free note from CodingNow 2.0 explains ML Brain.js in AI — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every AI topic on CodingNow 2.0, including ML Brain.js, is 100% free with no signup required.
With focused practice, most students grasp ML Brain.js in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now