TensorFlow.js : Machine Learning in Javascript

What is TensorFlow.js?

TensorFlow.js is an open source WebGL-accelerated JavaScript library for machine intelligence. It brings highly performant machine learning building blocks to your fingertips, allowing you to train neural networks in a browser or run pre-trained models in inference mode.

Why choose TensorFlow.js?

  • No drivers/No installs

The reason TensorFlow.js is successful because it works inside a browser, and browser is a unique platform where we can just share a link to anybody and use the link to access your app. No need to install any drivers, any software.

  • Interactive

Browser is highly interactive, so the user will engage more.

  • Sensors

Browsers have access to sensors like microphone, camera and accelerometer and all of these sensors are behind standardized API’s that work on all browsers.

  • Data stays on the client

Data storage at the client side. This means that data coming from these sensors does not need to leave the client.

Where you can use TensorFlow.js

  • You can write models directly into the browser.
  • Use a pre-existing/pre-trained model in python. use a script and you can import it into the browser to do inference.
  • Re-train the imported model with the private data that comes from those sensors of the browser, in the browser itself.

HowTensorFlow.js works?

TensorFlow.js

We have the browser that utilizes WebGL to do fast, linear algebra. 

On top of it, TensorFlow.js has two sets of API’s:

  • Ops API: which used to be deeplearn.js and it is aligned with the API’s of TensorFlow python. It is powered by automatic differentiation library that is built analogous to eager mode
  • Layers API: High layer API that allows you to use best practices and high-level building blocks to write models.

TensorFlow.js also supports Keras model and TensorFlow saved model and import it automatically for execution in the browser.

Building RNN that learns to sum two numbers

TensorFlow.js

We are going to build a recurrent neural network that learns to sum two numbers. But those numbers are being ed character by character and then the neural network has to maintain an internal state with an LSTM cell. That state then passes into a decoder. That decoder has to output 100, character by character. As a result, it’s a sequence to sequence model.

let’s write it with Layers API:

  • We first import out TensorFlow.js. then,
  • We create our sequential model i.e. stack of layers.
  • First two layers are encoder and last three layers are decoder.

TensorFlow.js

  • We then compile it with a loss, optimizer and metric we want to monitor( like accuracy)
  • We call model.fit, with our data.

TensorFlow.js

Await keyword is used for executing model.fit an asynchronous call. model.fit can take time to execute the command, during that time we don’t want the main UI thread of the browser to be locked.

 

Read also: Importance of exploratory data analysis

Tagged with:

Manish Prasad

An experienced data scientists with huge passion for working on new challenges.