DEV Community

Cover image for Building a Basic Neural Network with Brain.js
koushikmaratha
koushikmaratha

Posted on

Building a Basic Neural Network with Brain.js

Neural networks are a powerful tool for solving complex problems in machine learning and artificial intelligence. They are used to make predictions, classify data, and recognize patterns. In this article, we will explore how to build a basic neural network using Brain.js, a popular JavaScript library for neural networks.

for more details please check brain.js official documentation.

Getting Started with Brain.js

Before we dive into the code, let's first get familiar with Brain.js. Brain.js is a JavaScript library that makes it easy to build and train neural networks. It provides a simple API that allows you to create, train, and test your neural network.

To get started with Brain.js, you will need to install it using npm. Open your terminal and run the following command:


npm install brain.js

Enter fullscreen mode Exit fullscreen mode

Once Brain.js is installed, you can start building your neural network.

Building a Basic Neural Network

In this example, we will build a basic neural network that learns to recognize patterns in data. We will create a dataset of 10 examples, each with two inputs and one output. The output will be a binary value, either 0 or 1, depending on the inputs.

Let's start by defining our dataset:


const data = [  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] },
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] },
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
];

Enter fullscreen mode Exit fullscreen mode

Next, we will create a neural network using Brain.js. Our network will have two inputs, one hidden layer with three neurons, and one output.


const net = new brain.NeuralNetwork({
  inputSize: 2,
  hiddenLayers: [3],
  outputSize: 1,
});

Enter fullscreen mode Exit fullscreen mode

Now that we have defined our dataset and our neural network, we can train our network using the dataset. We will use the train method of the NeuralNetwork class to train the network.


net.train(data);

Enter fullscreen mode Exit fullscreen mode

Finally, we can test our neural network by passing in new input data and getting a prediction from the network.


const output = net.run([0, 1]);
console.log(output);

Enter fullscreen mode Exit fullscreen mode

In this code, we pass in an array of two inputs, [0, 1], and get a prediction from the network. The output should be close to 1, since [0, 1] corresponds to an output of 1 in our dataset.

Conclusion

Brain.js makes it easy to build and train neural networks in JavaScript. In this article, we explored how to build a basic neural network that learns to recognize patterns in data. By understanding the basics of neural networks, you can build more complex models to solve a wide range of machine learning problems.

Top comments (0)