Getting stared with tensorflow.js library

Getting stared with tensorflow.js library


TensorFlow.js is a library for machine learning in JavaScript. It is hardware-accelerated JavaScript library, which is used for training and deploying machine learning models. Let us learn how to get started with tensorflow.js library.

You can develop machine learning models in JavaScript, and use machine learning directly in the browser or in Node.js environment.

There are three main usecases of using tensorflow.js

Run the existing models

If you have a model developed in Javascript, then you can use it to run in the browser. You can also convert the pretrained models developed in Python and use them in Javascript environments.

Keras models are usually created via the Python API and can be saved configuration + weights, weights only, configuration only. This format can be converted to TensorFlow.js Layers format, which can be loaded directly into TensorFlow.js for inference or for further training. Learn more about converting keras model to tensorflow.js.

If you have a SavedModel of tensorflow (SavedModel contains a complete TensorFlow program, including weights and computation and it does not require the original model building code to run, which makes it useful for sharing or deploying), then you can convert it into tensorflow.js using tfjs-converter,

Retrain existing models

If you have a pretrained model, and you want to retrain it with new data, then you can do so in tensorflow.js. This is particularly useful when you have an edge device, which you want to customize with new personalized data.

For example, you have a model which is trained by thousands of sample of voices of humans. But you want that the final model be personalized to recognize the voice of the user at the user’s device. So in this scenario, you can use the pretrained model of the voice recognition, remove the final layer and train a new (often fairly shallow) model containig the voice of user on top of the output of the truncated model.

This technique is called tranfer learning. Examples of transfer learning for the audio classifier and image classifier.

Develop and train model in Javascript

We saw how we can use tensorflow.js to run the existing models and retrain them. But we can also use tensorflow.js to completely train the model from scratch and use it. In order to train the model from scratch, you need to be familiar with tensorflow concepts of Tensors, Layers, Optimizers and Loss Functions.

Helper libraries

There are few helper libraries available which can be used with tensorflow.js.


tfjs-vis is used to visualize the Behaviour of TensorFlow.js Model. The main features of this library are

  • It provides a set of visualizations like bar chart, line chart etc. useful for visualizing model behaviour
  • A set of high level functions for visualizing objects specific to TensorFlow.js
  • A way to organize visualizations (the visor) of model behaviour that won’t interfere with the web application

The library is flexible enough that you can use external javascript based chart libraries for visualization like d3, Chart.js or plotly.js.


tfjs-examples contains the code and usecases developed using tensorflow.js. There are many examples available. You can find examples of Binary classification – classifying website urls as normal or phishy, reinforcement learning – using deep Q learning to solve the snake game, date conversion using LSTM attention model

Online browser Demos

One of the advantage of tensorflow.js is that it can run in browser environments. So we can easily show demos in browser, which can use text, webcam, microphone, or other things for input. Tensorflow.js contains many such interesting usecases which you can check in browser itself.

Leave a Reply

%d bloggers like this: