A simple machine learning small instance with javascript

Source: Internet
Author: User
Tags machine learning project machine learning machine learning algorithm machine learning application training data

While it may not be the development language of traditional choices for machine learning, JavaScript is proving to be able to do this—even though it currently cannot compete with the main machine learning language Python. Before we go any further, let's take a look at machine learning.

According to Skyhoshi, a developer with more than 13 years of experience, machine learning gives computers the ability to learn based on the data they provide, and to identify patterns and then draw conclusions without explicit human intervention.

Traditionally, JavaScript is not commonly used in machine learning for two main reasons, including limited library support and implementation complexity.

Nonetheless, JavaScript has recently gained popularity, mainly because of the wide range of opportunities for deploying machine learning applications on the client side.

In this article, I will demonstrate a simple machine learning tutorial project that demonstrates that JavaScript can also power machine learning applications.

For this relatively simple project, I will use the Neal.js program file, a simple open source JavaScript library that provides a preprocessed neural network for machine learning.

The goal of the project is to train a neural network with a set of training data so that it can predict whether the contrast of colors is bright or dark.

The first step: install the library

Let's use the NoDE.js file and install the library through the NPM package manager. To do this, you need to pre-install the No.js file on your computer.

Here is the code you can use to run on the terminal. (Remember to install it on the folder you want to process)

Npm install brain.js

Step 2: Import the library

Use the following code to import the library on your file:

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

Step 3: Create a neural network

Here is the code:

Const network = new brain.NeuralNetwork();

Step 4: Training data

In machine learning, the training data is as good as the results received. Higher quality data may predict better results than lower quality data.

This data is a set of examples that is provided to the algorithm and "guides" what the algorithm is searching for.

In this example, since we want this machine learning algorithm to learn to recognize the difference in color contrast between light and dark, we will give these examples to train the learning model.

After that, the algorithm will use the provided examples to identify the essential features between the two groups.

If unknown data is provided in the future, the algorithm will classify based on the features identified from the original training model.

For this project, we will use the train() function in the built-in brain.js program file to train the neural network using an array of instance data.

The training data in each instance will have an input object and an output object, both of which should be an array from 0 to 1.

Because we are using color for research, we are going to find their RGB values. Since the RGB colors are between 0 and 255, we can convert them to values between 0 and 1 divided by 255.

For example, if the RGB value of a color is (158, 183, 224), we convert each value by dividing by 255, which becomes (0.62, 0.72, 0.88).

After that, we need to provide some sample datasets of RGB values and specify whether the output will be bright 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 the results

After training the algorithm with some instance data, it is time to look at the prediction results.

We simply call the Run() function and provide a color tonal parameter in order to know if it is bright or dark.

Here is the code:

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

Console.log(result);

If we execute the above code on a Windows terminal, the output will look like this:


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

If we want the output to be bright or dark, then add the following code:

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

Console.log(result);

Display results on the following terminals:

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 article, I demonstrated a simple machine learning project on JavaScript. In order to improve your machine learning skills, you of course need to complete similar projects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.