TensorFlow.js: A Browser-based Machine Learning Library

Table of Contents ( Press the ← key in browser search bar to return TOC)

Overview

TensorFlow.js is an open-source JavaScript library that brings the power of the popular Python library TensorFlow in JavaScript. It allows developers to define, train, and deploy machine learning models without the need for any external server or complex setup. TensorFlow.js supports both frontend and backend development, giving you the flexibility to build complete end-to-end machine learning applications entirely in JavaScript.

TensorFlow.js enables a whole new range of possibilities for browser-based applications. In this article, we’ll dive into the basics of TensorFlow.js and explore its capabilities.

Features of TensorFlow.js

  1. Browser Compatibility: TensorFlow.js is designed to work seamlessly across different browsers, including Chrome, Firefox, Safari, and others. This compatibility ensures that your machine-learning models can reach a broader audience without any compatibility issues.
  2. Execution on the Client-side: The most significant advantage of TensorFlow.js is that it performs computations directly on the client-side, utilizing the user’s device’s GPU and CPU. This not only reduces the server workload but also provides real-time inference, making your applications faster and more responsive.
  3. Model Conversion: TensorFlow.js supports model conversion from TensorFlow Python models, allowing you to reuse pre-trained models and even import popular models such as MobileNet and Coco SSD directly into your web application.
  4. Mobile Support: TensorFlow.js can be integrated into mobile applications using React Native or any other hybrid mobile development framework, enabling on-device machine learning capabilities for mobile users.
  5. Transfer Learning: You can employ transfer learning techniques to fine-tune pre-trained models for specific tasks. This significantly reduces training time and computational resources required for building custom models.
  6. Layers API: TensorFlow.js provides a high-level Layers API that simplifies the process of building neural network architectures, similar to Keras in Python.
  7. Flexible Backend: TensorFlow.js can use WebGL, Web Assembly, or CPU as its computing engine also known as simply backend, and allows us to switch between them. We will talk about this more below.

TensorFlow.js Backends

In TensorFlow.js a backend refers to the underlying computational engine that performs mathematical operations and computations required to run a machine learning model. A backend is a hardware-specific choice where Tensors are stored and operated on.

// Get the active backend.
console.log(tf.getBackend());

A backend is an important topic in TensorFlow.js since it is directly responsible for the performance of the model while training or inferencing.

TensorFlow.js will choose the best available backend for the given platform while giving us the opportunity to switch to the preferred one. Only one backend will be active at a time. The possible backends in TFJS are:

WebGL

Web Graphics Library abbreviated as WebGL is the most powerful backend available for browser-based machine learning tasks. It’s the default backend that TensorFlow.js will use in the browser.

WebGL is a JavaScript API that allows high-performance 2D and 3D graphics rendering directly within the browser. TensorFlow.js leverages the parallel processing capabilities of the GPU through WebGL to accelerate computations, making it particularly well-suited for real-time inference and model execution in web-based applications.

For heavy training and large models, WebGL is the recommended backend.

Web Assembly

Web Assembly (WASM) is another fast and performant backend available in TensorFlow.js. While it’s not faster than the WebGL backend it can slightly outperform the WebGL backend when used for small models. That’s why this backend can be suitable for tiny models or when WebGL is not available.

CPU

Central Processing Unit (CPU) is the simplest but least performant backend yet. In this backend, Tensors are stored and operated within JavaScript which may give us poor performance. This backend is suitable for testing or when WebGL and WASM are not available.

Original Post>

Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.