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 use-cases of using tensorflow.js

Use cases of tensor flow JS library
Use cases of tensor flow JS library

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 pre-trained 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 pre-trained 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 customise with new personalised 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 personalised to recognise the voice of the user at the user's device. So in this scenario, you can use the pre-trained model of the voice recognition, remove the final layer and train a new (often fairly shallow) model containing 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 tensor-flow concepts of Tensors, Layers, Optimisers 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.