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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Creating a Web Project: Caching for Performance Optimization
  • The End of “Good Enough Agile”
  • SaaS in an Enterprise - An Implementation Roadmap
  • How to Merge HTML Documents in Java
  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.2K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!