DEV Community

Cover image for A Practical Guide to Brain.js
Ganesh Patil
Ganesh Patil

Posted on • Updated on

A Practical Guide to Brain.js

What is Brain.js?

Machine learning and neural network is the most exciting frontier for software development. As per Stack Overflow, survey javascript is the most widely usable programing language and is not limited only to developing web applications right now we can develop desktop, and mobile applications, and the most advance and secure systems. Advance systems mean to be predicting results by training or building data modules which we normally do with python but now javascript is capable to build and train data modules as well.

There are a number of machine learning javascript libraries available such as TensorFlow, ml5, WebGL, and brain.js but the most essential and used to build advanced systems is the brain.js which we are going to explore in this guide.

Why Brain.js?

After a basic introduction to brain.js, many developers may have questioned whether is javascript good for machine learning and why we need to know about brain.js the simple answer is yes javascript is a good fit for machine learning, and many developers use and explore javascript space. There are many applications of brain.js already available where we can work on modules and develop an advanced system that predicts future results.

Brain js is the fastest processing library because of GPU computations and even If you don't have GPU. Beginners can directly jump into brain js because you don't need neural network knowledge to start with it just basic javascript fundamentals are enough to start with brian js.

How to get started with Brain.js

To get started with brain js you need a basic understanding of javascript and an introduction to the neural networks. The brain.js is considered the second most widely used javascript library after TensorFlow. It's open source javascript library you can join the community and start contributing to it, the following ways help you to get started with brain.js.

  • Official Documentations
  • Tutorials
  • Community resources
  • Contributing to it
  • Articles and Manual resources

There are multiple resources available to learn about brain.js join their social community and will share the best resources with you, find articles and instruction manuals developed by other developers, and always start with official documentation. Building projects are the best way to learn any library or framework start building small and individual projects which help you to understand it properly.

Applications and Examples of Brain.js

Brain.js help you to create machine learning modules and neural network applications, you can build advanced security applications using brain.js.

Emotion detection application
Future Results predicting application
Cloudinary image recognition add-on system
Neural network application
Input-output Guessing system

These are some basic applications of brain.js but you can explore & learn more about them and now we are building a small application using brian.js.

Prerequisites

  • Node.js
  • NPM installed on your system
  • Basic understanding of Javascript

Installed Brain. js or use CDN

Javascript Basics

npm install brain.js
Enter fullscreen mode Exit fullscreen mode
<script src="//unpkg.com/brain.js"></script> // cdn
Enter fullscreen mode Exit fullscreen mode

Create index.html and app.js

index.html


<html>
  <head>
    <script src="//unpkg.com/brain.js"></script>
    <script src = " index.js"> </script>
  </head>
</html>
Enter fullscreen mode Exit fullscreen mode

app.js


const net = new.brain.NeuralNetwork({hiddenLayers :[3]});
Const _Data = [
    {input : [0,0], output: [0]},
    {input : [0,1], output: [1]}
  ];
net.train(_Data);
console.log(net.run([0,0]));
console.log(net.run([0,1]));
Enter fullscreen mode Exit fullscreen mode
Output

Input [0,0] = 0.038714755326509476
Input [0,1] = 0.9301425814628601
Enter fullscreen mode Exit fullscreen mode

Explanation

The above code will predict the possibilities from 0 and 1 which is more executable, you can take it as 0 means nothing and 1 means single point value. This is a small application you can develop a cricket match-winning prediction system with this idea here we take two inputs you can take more as per requirements. The input [0,0] shows low possibilities than input [0,1] values. You can train data using brain.js and develop several applications using it. There are multiple methods available to train and develop an application.

Wrap-up

In this article, we discussed a couple of concepts about neural networks and machine learning that are used in javascript using brain.js and how it uses these concepts sequentially. The brain is the fastest javascript library to build a neural network you can explore and build more stuff with it.

==========================================
➡️ Connect with me

Twitter

Youtube
Linkedin

Github

Top comments (9)

Collapse
 
bobdotjs profile image
Bob Bass

For anyone who is interested, Web Dev Simplified on YT has a tutorial on how to build an application that determines whether the text should be light or dark against a background color using brain.js.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I'm bookmarking this right now, finally some boost to ML in JS!

Thank you for sharing! 😁

Collapse
 
devgancode profile image
Ganesh Patil

Glad it help!

Collapse
 
jochemstoel profile image
Jochem Stoel

In this article, we discussed a couple of concepts about neural networks and machine learning that are used in javascript using brain.js and how it uses these concepts sequentially.

I'm sorry I'm a n00b and I have a few questions.

What concepts about neural networks do you mean you talked about? Can you list them please? I also feel a little confused or maybe I don't fully understand where exactly in the article you talk about how brain.js uses 'these concepts sequentially'? What concepts? I searched this page for the word sequentially and it is not even in the post. So weird. It does sound very interesting if you put it like that. Using concepts sequentially, woa that almost makes my head explode. I have one more question about that. On the brain.js Github page and on their website it says that it runs on multiple threads, wouldn't that make it parallel and not sequential?

Man, hard to wrap my head around these things. I wish there were more examples available on the internet on how to use brain.js, it seems like everybody who writes about this stuff is simply copying examples from the readme file from the package repository verbatim and then add a little filler text and their social media links.

Anyway thanks so much for this article. I'm sure a lot of people can learn from this.

Collapse
 
anasnmu profile image
Anas Nabil

Thanks for sharing this solid article, keep going!

Collapse
 
devgancode profile image
Ganesh Patil

Thanks @anasnmu 🙏🏻

Collapse
 
othimar profile image
Pélé Oussoumanou

Thanks! The code has some typos but it easy to fix.
It's working fine in the browser and Deno. But I have some issues with node.js (NPM).

Collapse
 
devgancode profile image
Ganesh Patil

Looking forward to fix it!
Thanks for addressing!

Collapse
 
tr11 profile image
Tiago Rangel

hi, quick warning that the code has some typos, like capitalization of Cons, new. instead of new , js comments in HTML, etc