Company blog

Neural Networks – brain.js

Post date 10.12.2020

What is a neural network?

A neural network is a set of artificial neurons put together. The activity of a neural network is modeled on the way brain processes work. Every neuron in an artificial neural network contains weights and errors. A neuron is a function that makes calculations after receiving the data and then sends it further to the next neuron via an activation function. A neural network consists of layers of neurons connected with each other. In order for the network to work properly at least three layers must be used.

Neural network layers:

An input layer (red circles in the image below)- gathers the input data and sends it to a hidden layer.

Hidden layers (blue circles in the image below) – used to look for links between neurons, the number of hidden layers may be infinite.

An output layer (green circles in the image below) – returns a result from a trained network.

Types of neural networks:

  • Layered unidirectional networks

Unidirectional networks can be one-layered or multi-layered. There is no loopback in one-layered networks. A signal passes through every neuron precisely once in its cycle. To train multi-layered networks a backpropagation algorithm is used.

  • Recursive networks

In recursive networks links between neurons form a graph with cycles. In this network type, a loopback takes place between input and output. In recursive networks – as opposed to unidirectional ones – signals pass in two different directions.

Training a neural network

A neural network training is an automatic tuning of weight values (based on a proper algorithm) in a way that the network would execute its task as good as it is possible. Two types of neural network training can be distinguished:

  • Supervised – appears when data that is passed to network training contain an expected answer. In this case, the output data is given to the input data;
  • Not supervised – appears if data that is passed to network training does not contain the answer. The network should classify the input data on its own.

What is brain.js?

Brain.js is a library for neural networks written in javascript. The library can be used on the server-side as well as on the client’s side (Internet browser). The library has unidirectional and recursive networks programmed. Additionally, a unidirectional network can be turned on in GPU mode. In this article, we will focus on how the library works on the client’s side. One of the benefits of using a neural network written in javascript is the possibility of turning it on in the Internet browser.

Installation of brain.js

The easiest way of using brain.js library is to import a script with a source in the head tag in an Html file.

<script src="[email protected]/browser.js"></script>

In order to do so, we can create a new Html file, and then add the script to the head tag.

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <script src=""></script>
    // Our exemplary code will be put here

Example of use

In this example, we will define an undirectional neural network in which a backpropagation algorithm is used. Our network will be finding out the (output) result for the last example in the chart below. The network would be taught by the output and the input data (examples from 1 to 5). A trained network returns back the output for the input data [1, 0, 1] from example no.6. For a simple neural network training, data must be presented in a form of a board in which json objects with input and output attributes are located.  An input and an output may be an array consisting of numbers or a hash in which key values are numbers.


Code implementation is presented below. Our actions are described step by step in javascript code comments:

document.addEventListener("DOMContentLoaded", function() {
  // 1. We create some training data on the basis of examples 1-5  for which we have the input and output data.
  const trainingData = [
    { input: [0, 0, 0], output: [0] },
    { input: [0, 0, 1], output: [0] },
    { input: [0, 1, 1], output: [0] },
    { input: [1, 0, 1], output: [1] },
    { input: [1, 1, 1], output: [1] }
  // 2.	Then we create the simplest unidirectional neural network with a backpropagation mechanism.

  const network = new brain.NeuralNetwork();
  // 3.	We train the network by calling train ()function on the object network.
  // 4. With a use of the run () function we can inquire the trained network:
(As the first parameter of the run () function we pass input data for which result interests us).

  const result =[1, 0, 0]);
  // 5. We show the result of our network on the screen.

Opening an index.html folder in a browser evokes the calling of a script. As a result of the input data [1,0,0], we got 0.9080509543418884 or a very similar output result. Basing on this result, we can see that the answer for the last example is 1. The network returned back the number closer to 1 than to 0. In order to check if the network works correctly we can execute operations once again but for the different input data e.g.: [0,1,1]. To do so we modify run () function and we pass [0,1,1] as a parameter.

1 const result =[0, 1, 1]);

After restarting we receive the result 0.05815340578556061 or a very similar one. 0 is the result of another test as the network returned a number closer to 0 than to 1. It’s a correct result/outcome which we can verify in example no.3. in the chart above.



In this article, I presented a very general description of a neural network and a simple implementation of a brain.js library. In the next one, I will define a backpropagation algorithm that is used in neural networks. I will present an installation of a brain.js library with the use of package manager npm and yarn. I will train a neural network to recognize numbers from 0 to 9 and I will discuss basic parameters that can be configurated during network definition.

Author: Marcin Mąsior

Links and sources:

Alternatives for brain.js:

  • Tenser flow –
  • Synaptic-

You Tube clips that explain how neural networks work.

  • But what is a Neural Network? –
  • How Deep Neural Networks Work –



Other sources:


Problem solved in our example –


Comments (0)

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

All articles

Do you have a question?
Write to us!