DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two-Tower Model for Fraud Detection: A Comprehensive Guide
  • Navigating the Complexities of Text Summarization With NLP
  • The Perils of AI Hallucination: Unraveling the Challenges and Implications
  • Understanding the Basics of Neural Networks and Deep Learning

Trending

  • Evaluating SOC Effectiveness Using Detection Coverage and Response Metrics
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. A Simple Machine Learning Project in JavaScript

A Simple Machine Learning Project in JavaScript

This tutorial teaches you how to use Javascript to define, train, and deploy machine learning algorithms completely in the browser.

By 
Dr. Michael Garbade user avatar
Dr. Michael Garbade
·
Updated May. 28, 20 · Tutorial
Likes (35)
Comment
Save
Tweet
Share
37.6K Views

Join the DZone community and get the full member experience.

Join For Free

Machine learning in the web browser?

Yes, it’s possible to use JavaScript to define, train, and deploy machine learning algorithms completely in the browser.

Although it may not be a conventional selection for machine learning, JavaScript is proving to be capable of completing such tasks — even if it cannot compete with the principal machine learning language: Python.

Before going further, let’s do a little introduction to machine learning.

According to Skyhoshi, who is an experienced developer with over 13 years of experience, machine learning endows computers with the ability to learn from the provided data, identify patterns, and then draw conclusions without requiring explicit human intervention.

Traditionally, JavaScript is not commonly used in machine learning for several reasons, including limited library support and implementation complexities.

Nonetheless, JavaScript has gained popularity recently mainly because of the extensive opportunities available in deploying machine learning applications entirely on the client-side.

In this article, I’m going to illustrate a simple machine learning tutorial project that demonstrates that JavaScript is also capable of powering machine learning applications.

Let’s Get Started

For this simple project, I’m going to use brain.js, which is a handy open source JavaScript library with pre-made neural networks for machine learning.

The objective of this project is to train a neural network with a set of training data such that it can predict whether a color contrast is light or dark.

Step 1: Install the library

Let’s use Node.js to install the library using the npm package manager. For this to work, you need to have Node.js pre-installed on your computer.

Here is the code you can run on your terminal. (Remember to install it on the folder you’ll be working on).

 npm install brain.js 

Step 2: Import the library

Import the library on your file using the following code:

 const brain = require("brain.js"); 

Step 3: Create a neural network

Here is the code:

 const network = new brain.NeuralNetwork(); 

Step 4: Train the data

In machine learning, the training data is as good as the outcome that will be received. A higher quality data is likely to predict better outcomes than a lower quality data.

This data is a set of examples that is provided to an algorithm to “instruct” it about what to search for.

In this case, since we want our machine learning algorithm to learn to identify the difference between light and dark color contrasts, we will give it those examples to train the model.

Thereafter, the algorithm will employ the provided examples to discern the essential characteristics in differentiating between the two groups.

If provided with unknown data in the future, the algorithm will classify according to the features it recognizes from the originally trained model.  

For this project, we are going to use the in-built brain.js train() function to train our neural network using an array of example data.

Every example training data will have an input object and an output object, of which both should be an array of numbers from 0 to 1.

Since we are working with colors, we are going to go for their RGB values. Since RGB colors are between 0 and 255, we can convert them to have their values between 0 and 1 by dividing by 255.

For example, if the RGB value of a color is (158,183,224). If we convert each of the values by dividing by 255, it will become (0.62, 0.72,0.88).

Thereafter, we need to provide some example dataset of RBG values and specify whether the output will be light or dark.

Here is the code:

network.train([

  {input: {r:0.62,g:0.72,b:0.88}, output:{light: 1}},

  {input: {r:0.1,g:0.84,b:0.72}, output:{light: 1}},

  {input: {r:0.33,g:0.24,b:0.29}, output:{dark: 1}},

  {input: {r:0.74,g:0.78,b:0.86}, output:{light: 1}},

  ]);

Step 5: View results

After training the algorithm with some example data, it’s now time to view the prediction results.

Are you excited?

We just call the run() function and provide an argument of the color hue we would like to know whether is light or dark.

Here is the code:

const result = network.run({r:0.0,g:1,b:0.65});

console.log(result);

If we execute it on the Windows terminal, the output will be something like this:

Image title

As you can see, our machine learning for beginners algorithm predicted that the color contrast is light with an accuracy of 0.97 (97%).

If we want the output to be either light or dark, we can add the following code:

const result = brain.likely({r:0.0,g:1,b:0.65}, network);

console.log(result);

Here is the result on the terminal.

Image title

Conclusion

Here is the code for the entire project:

const brain = require("brain.js");

const network = new brain.NeuralNetwork();

network.train([

  {input: {r:0.62,g:0.72,b:0.88}, output:{light: 1}},

  {input: {r:0.1,g:0.84,b:0.72}, output:{light: 1}},

  {input: {r:0.33,g:0.24,b:0.29}, output:{dark: 1}},

  {input: {r:0.74,g:0.78,b:0.86}, output:{light: 1}},

  ]);

//const result = network.run({r:0.0,g:1,b:0.65});

const result = brain.likely({r:0.0,g:1,b:0.65}, network);

console.log(result);

In this post, I demonstrated a simple machine learning project in JavaScript. To improve your machine learning skills, you need to complete such projects.

Happy learning machine learning!

Machine learning JavaScript library neural network Data (computing)

Published at DZone with permission of Dr. Michael Garbade. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Two-Tower Model for Fraud Detection: A Comprehensive Guide
  • Navigating the Complexities of Text Summarization With NLP
  • The Perils of AI Hallucination: Unraveling the Challenges and Implications
  • Understanding the Basics of Neural Networks and Deep Learning

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook