Hire the author: Lakshay G
Introduction
In this tutorial, I’ll cover how to build a color contrast picker using brain.js. To build this I will be using Reactjs, redux, brain.js MongoDB, and node.js. It includes building a neural network and then training it to predict accurate results.Brain.js is a great library to start with machine learning or neural networks. The unique thing about this project that differentiates it from other articles is that:
- It is solely based on the request/ response model
- The training data gets updated and does not wipe out even if the browser window is closed
- It can be used to simultaneously train the neural network and predict results
NOTE: I will be using a standard neural network, that will be approximating the XOR function over the input parameters to predict results.
Motivation
Google says “Machine Learning is the future,” and the future of Machine Learning is going to be very bright. I wanted to start with machine learning from a very basic level and javascript is the scripting language I prefer to code in. And as a result, I came across this amazing library brain.js. The best part is you don’t need to know the advanced fundamentals of machine learning to use this. Brain.js is the best library for beginners.
Goal
My goal with this project is to make the readers successfully implement it with the greatest ease and then deploy it online for free.
Glossary
neural network: A neural network is a series of algorithms that endeavors to recognize underlying relationships in a set of data through a process that mimics the way the human brain operates
AI: It stands for artificial intelligence (intelligence demonstrated by machines).
NaN: In computing, NaN, standing for Not a Number, is a member of a numeric data type that can be interpreted as a value that is undefined or unrepresentable, especially in floating-point arithmetic.
Project Requirements
- Basic programming fundamentals are a must
- Must have a little bit idea about JavaScript, React.js, Node.js and npm packages(brain.js)
Step by Step Tutorial
Backend
- The first task is to create the MongoDB schema for the training data
Now in this remember all the input and the output parameters should be Numbers - The second task is to create a function to connect to the Database.
- The third task is to create the server file and the API’s
There are 5 main routes that will be used in our client application :
1. To train our neural network (/train)
2. To predict the output (/predict/:r/:g/:b and /result/:r/:g/:b but both have different use-cases)
3. To instruct the neural network (/white/:r/:g/:b and /black/:r/:g/:b)
Note: Before running this file keep in mind to separately run the statements commented from line 28 to 35 (because we need to store some starting data for our neural network) and then comment them back - After all these steps are successfully executed, run the server (it is going to show some warnings related to brain.js but those will not affect our application in any way)
- Now the server is ready for deployment, follow this in order to deploy it for free on Heroku.
Frontend
- The first step is to create a react-app (use npx create-react-app appname)
- Now set up redux in the app (follow this short video for help)
- Now after everything is set, let’s start with the frontend designing and setting up the actions and reducers.
- First Lets code the App.js file first for the design and this is how it looks
Don’t worry if you didn’t get meaning of the functions used, it will be way easier to understand once the actions and reducers are set - Let’s write the actions corresponding to each of the 5 routes at the backend
- After writing the actions, now our last task is to write the reducer
- After this is done, import all the actions and the redux state variables in the App.js file (step 4)
- Now, the client application is ready for deployment (follow this for help) and it will finally look like
Note: The Black/ White buttons are used to train the neural network whereas the “Set Background and Predict” button is used to predict the results
Learning Tools and Strategies
- Don’t write messy code, everything should be well understood. Doing this will automatically help you when it comes to debugging.
- Do a console.log() at each major step you feel is important, or that you feel may throw some kind of error. This is so that you can get the result of each major code-snippet.
- The last most important thing is to make sure that the training data consists of only numbers and not strings, otherwise, brain.js neural network will not train it and throw an error NaN which means Not a Number.
Search Terms
color contrast picker, brain.js, neural network, machine learning
Reflective Analysis
This is my first hands-on on any machine learning related project. After implementing this project my theoretical knowledge related to neural networks has increased because I was visually able to see what was happening. Explore more of this brain.js documentation here. The most common mistake people are expected to make is to insert strings in training data rather than numbers and as a result, the neural network is not able to train the data and throws NaN error.
Conclusions and Future Directions
It is a basic and very easy implementation of color contrast picker using brain.js by training the neural network to predict accurate results and keeping the learnings of our neural network persistent even if the browser window is closed. The server application and the client application are deployed separately, so I would suggest trying deploying them both together as a single application.
Citations
I used https://blog.frankfurt-school.de/neural-networks-vs-random-forests-does-it-always-have-to-be-deep-learning/ for the featured image.
The project is available here and the deployed version is available here.

