Machine Learning in JavaScript

2019 04 30

Author: Nedas Šimkus

Nowadays we hear the term Machine Learning on every corner. Machine Learning is gaining popularity as a way to infuse intelligence into applications. By making complex analysis of data it helps us to take the right decisions or avoid unexpected problems. So what is machine learning and how does it work?

Machine Learning is a method of data analysis. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make complex decisions with minimal human intervention. Two of the most widely adopted machine learning methods are supervised learning and unsupervised learning:

  • Supervised learning: algorithms are trained using labeled examples, such as an input where the desired output is known. The learning algorithm receives a set of inputs along with the correct outputs, and the algorithm learns by comparing its actual output with correct outputs to find errors. It then modifies the model accordingly.
  • Unsupervised learning: used with data that has no historical labels. The system is not told the “right answer”. The algorithm must figure out what is being shown. The goal is to explore the data and find some structure within.

Here are a few widely publicized examples of machine learning applications you may be familiar with:

  • Facebook face recognition. You might not know the person in your photo, but be sure that Facebook does.
  • Online recommendation offers from Amazon and Netflix.
  • Fraud detection.
  • Our virtual helpers Siri and Alexa.

JavaScript

Python has always been the number one programming language for machine learning, because it is a mature programming language and has great ecosystem for machine learning. However, recent developments in the JavaScript world are making it more attractive for machine learning projects.

JavaScript, like any other tool, has it’s advantages and disadvantages. Here are some of them. Let’s start from the disadvantages:

  • Disadvantage #1: Immature ecosystem for machine learning development.

Despite the recent improvements, JavaScript is still very new in the machine learning field, so there is a lack of information on the subject. Fortunately, JavaScript is one of the most popular programming language in the world, it just needs time to grow in new area.

Advantages:

  • Advantage #1: JavaScript is a general purpose, cross-platform programming language. Probably the biggest advantage of JavaScript is the universality of the language. Modern web browsers allows you to run your code on nearly any device. Also, tools like electron allow developers to create desktop applications, while React Native let’s you create native mobile applications. So, JavaScript is now a general-purpose, cross-platform programming language.
  • Advantage #2: JavaScript is the most popular web development language with a mature npm ecosystem. Probably the biggest advantage of using JavaScript with machine learning is the language’s popularity. So as machine learning demand grows it will be easier for everyone to develop them with the programming language we know. Also, the Node Package Manager (npm) ecosystem is also large and growing, and you can find some already pre-trained machine learning models or packages that helps with machine learning problems.
  • Advantage #3: TensorFlow.js

TensorFlow.js – an open-source library you can use to define, train and run machine learning models in the browser, using JavaScript and a high-level layers API. Running machine learning programs in the browser unlocks new opportunities, like interactive machine learning. The main advantages this library gives us while creating machine learning applications are these:

  • Running machine learning programs in the browser means that as a user I do not need to install any libraries or drivers, just open a webpage and your program is ready to run. In addition it is ready to run GPU acceleration since TensorflowJS automatically supports WebGL, and will accelerate your code behind the scenes when a GPU is available.
  • All data stays on the client, making TensorFlow.js useful for low-latency inference, as well as for privacy preserving applications.

Main workflows with TensorFlow.js are as listed:

  • Import existing, pre-trained model. Just find a trained model, load it to your application and use it as you please.
  • Re-train imported model. You can also re-train existing model with small amount of data, so it would please your needs.
  • Train model. You can also use TensorFlow.js to defined, train, and run models entirely in the browser using JavaScript and a high-level layers API.

Summary

As we can see, the stars have aligned perfectly so we could use Machine Learning with JavaScript, and the time is perfect to do this now.
To summarize, I would like to share the insights from Accenture Technology Vision 2017 about AI:

“Accenture research reveals that by 2035, AI can double economic growth rates in 12 developed countries, and boost labor productivity by up to 40 percent”

“Up to 85% of business and IT executives anticipate making extensive investments in one or more AI-related technologies over the next three years.”

It shows that topics like machine learning are relevant for our future. So it is really important that we talk about it today.