Building a Neural Network

 

Building a neural network with Brain.js:

Example:

<!DOCTYPE html>
<html>
<script src="//unpkg.com/brain.js"></script>
<body>
<h1>Deep Learning with brain.js</h1>
<div id="demo"></div>

<script>
// 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]?
let result = network.run([1,0]);

// Display the probability for "zero" and "one"
document.getElementById("demo").innerHTML =
"one: " + result["one"] + "<br>" + "zero: " + result["zero"];
</script>
</body>
</html>

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:

 
 

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

Example:

<!DOCTYPE html>
<html>
<script src="//unpkg.com/brain.js"></script>
<body>
<h1>Deep Learning with brain.js</h1>
<div id="demo"></div>

<script>
// 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}},
// Lightgrey (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"
document.getElementById("demo").innerHTML =
"Dark: " + result["dark"] + "<br>" + "Light: " + result["light"];
</script>
</body>
</html>

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?

 

 
ML Brain.js

Login
ADS CODE