Machine Learning on the Web

Chapter 1

Machine Learning in the web browser offers a lot of benefits to practitioners and web developers, without having to write any server-side solutions, and this chapter delves into some of those benefits.

First things first; even though this book is titled Machine Learning with TensorFlow.js, TensorFlow is a deep learning library, and hence all discussion in this book centers around deep learning and artificial neural networks. So why did I choose to title this book the way I did? The answer lies in knowing who this book is meant for: individuals who have experience in building web applications, particularly using JavaScript, yet have little or no expertise and experience in machine and deep learning.

We will touch upon machine learning and deep learning in the next chapter. For now, it is important to understand why we need to run machine learning solutions in the web browser in the first place.

Server-side versus Client-side Processing

Machine learning is mostly done on server-side machines and is typically done closer to where the data resides. This is done for the following reasons.

Compute Intensity

Machine learning is a deeply compute-intensive process requiring a lot of CPU, GPU, or TPU processing to learn from data. For this reason, most of the ML engineering tasks are performed server-side, or on a machine with enough compute resources to process large amounts of data for extended periods of time. The server-side processing of data is usually carried out in Python, R, and/or Scala, and to a lesser extent, in Java and C++.

Data Volume

The second reason for server-side processing pertains to data volume. Large amounts of data make it necessary that the enterprise data do not have to move on the wire before ML models can be trained. Even if the data comes from a public source, it should ideally be copied locally before it is used to generate a machine learning model.

Data Privacy and Security

Not every set of eyes in the organization need to have access to every bit of data there is within the organizational landscape. Data privacy and security dictate that access be restricted to individuals who must absolutely have the ability to perform operations on the data in question.

Why TensorFlow.js?

Just like any JavaScript framework, TensorFlow.js was engineered from the ground-up to execute in Microsoft Edge or Google Chrome. The following section provides a glimpse of why a new library is needed to develop machine learning solutions in a web browser.

Browser-based Workloads

JavaScript is commonly used to write client-side logic for web applications, regardless of the language or framework used on the server. With TensorFlow.js, machine learning solutions can be developed using JavaScript that run in the browser like Microsoft Edge or Google Chrome. This ensures that the application makes use of a library that is used on the web without the need to install anything.

Application Distribution

Application development typically moves from environment to environment based on some condition or approval, and the move to the production environment is no exception. For any application to be developed for the web browser, it is extremely easy to distribute to the systems it needs to run on, since it does not require any local installation to execute. Also, any change to the application is quite easy to make as the deployment is required in a single location i.e. the web server, as opposed to all systems it runs on.

Wide Audience

Web developers make up a largest group of the individuals writing code to create web applications. Irrespective of the server-side framework used, web developers are used to writing JavaScript code to run client-side logic, which makes TensorFlow.js the ideal library to run machine learning solutions on the client.

Transfer Learning

Machine Learning requires a lot of compute resources to learn from data, whereas web browsers are severely resource constrained, both in terms of bandwidth required to load the data from the server, and the memory to process and learn from that data. This is where Transfer Learning can be used to train existing models with new data that is only a subset of the total data required. In other words, Transfer Learning adapts a pre-trained model to learn from new data without ever needing the entire dataset (old as well as new data) to teach the model. Transfer Learning is covered in greater detail in Chapter 4.

JavaScript for Machine Learning

In order to fully understand how machine learning solutions are developed using JavaScript and executed in a web browser, let’s look at some of the other client-side libraries or frameworks for developing machine and deep learning solutions. While TensorFlow is not a pre-requisite for development using TensorFlow.js, it is imperative to know the concepts behind machine and deep learning. Using the knowledge imparted in Chapter 2, along with the understanding of JavaScript libraries for developing machine learning solutions below, look at the data provided to you in your organization and figure out the best way forward keeping in mind the problem you are trying to solve. The following section is neither an exhaustive list of machine/deep learning libraries nor a comparison of the approaches available to web developers for building machine learning solutions.

Brain.js

Web site: https://brain.js.org

Approach: Deep Learning/Neural Networks

Brain.js simplifies the process of creating and training neural networks and executing them in the web browser by relying on a small number of API calls. A knowledge of neural networks is essential to construct the neural network and train it in-line with custom data.

ML5.js

Web site: https://ml5js.org/

Approach: Machine Learning

ML5.js is based on the p5.js JavaScript library, and is commonly used for sentiment analysis, image classification, k-means clustering, and neural networks among other machine learning algorithms.

ConvNetJS

Web site: http://cs.stanford.edu/people/karpathy/convnetjs/

Approach: Neural Networks for Graphics Processing

Developed by a student at Stanford University, ConvNetJS is a client-side library for image processing as well as common neural network modules and an experimental reinforcement learning module.

WebDNN

Web site: https://mil-tokyo.github.io/webdnn/

Approach: Deep Learning/Neural Networks

Originally developed by Japan, WebDNN executes deep neural networks in the web browser using its JavaScript API.

DeepLearnJS

Web site: https://deeplearnjs.org/

Approach: Deep Learning/Neural Networks

I should not even be mentioning this library as it has been discontinued and merged into Google TensorFlow (https://js.tensorflow.com) in March 2018. DeepLearn.js allowed neural networks to be trained in the web browser and used the WebGL API for GPU processing of graphics. It is also important to mention that DeepLearn.js (and by extension TensorFlow.js) had nothing to do with TensorFlow, a popular deep learning library from Google; the renaming was simply done for branding reasons.

Don’t worry if are unable some of the underling concepts of machine learning and neural networks as these are covered in greater detail in Chapter 2. Also keep in mind the use cases for each type of data available to you as this would help you in picking the right algorithm and library.

Key Takeaways

An introductory chapter on JavaScript development for machine learning solutions, the discussion focuses on the following:

  • Why machine learning is predominantly a server-side task

  • Why TensorFlow.js is a good contender for machine learning on the client

  • Other JavaScript frameworks for web browser machine learning development

Last updated