Getting started with TensorFlow.js

Machine learning is one of the fastest-growing and most exciting fields currently and most companies hire talents in the field to develop products/services based. Google is one of the major companies that uses machine learning extensively.

TensorFlow is an open-source software library that was originally developed by Google Brain team for their internal use. Today, it’s one of the most popular packages used in the machine learning fields. This article will give you a basic understanding of how you can build your first project in TensorFlow.js.

What is a Tensor?

The central unit of data in TensorFlow is the tensor. A tensor consists of a set of primitive values shaped into an array of any number of dimensions and a tensor’s rank is its number of dimensions.

Note that tensors and multidimensional arrays are different types of object; the first is a type of function, the second is a data structure suitable for representing a tensor in a coordinate system. In short, Tensor is “something” which can be represented as a multidimensional array.

To get started with TensorFlow, head towards the TensorFlow website that has a bit of documentation and tutorials.

Machine Learning in Javascript

TensorFlow provides an open-source Javascript library called TensorFlow.js that can be used to define, train, and run machine learning models entirely in the browser. This means that you can use ML in your apps to make it smarter.

Running machine learning on client-side means that you can build lighter apps and won’t need to write a lot of codes. From a users perspective, it’s easy to use as the program is ready to run when the webpage is opened.  Running an ML program in your browser means there is need to install any libraries or drivers.  TensorFlow.js automatically supports WebGL, and will accelerate your code behind the scenes when a GPU is available.

So what can TensorFlow.js help you? If you have existing TensorFlow you’ve previously trained, you can convert into the TensorFlow.js format. It also has the ability to retrain a model. Using a technique called Image Retraining you can use transfer learning to augment an existing model trained offline using a small amount of data collected in the browser. You can also author models directly in the browser.

Sample Code

But first, there are two main ways to get TensorFlow.js in your JavaScript project: via script tags or by installing it from NPM and using a build tool like Parcel, WebPack, or Rollup.

To run TensorFlow.js via script, add the code to your HTML File

https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.0

To run TensorFlow via yarn or npm, using the following code

yarn add @tensorflow/tfjs
npm install @tensorflow/tfjs

Here is a sample project that you can try out which is based on Neural Network. In your main js file, you can insert the following codes

1. Used to import TensorFlow. In case of script tage, ‘tf’ is available on the index-page

import * as tf from ‘@tensorflow/tfjs’;

2. A sequential model is a container which you can layer to

const model = tf.sequential();

3. Add a dense layer with 1 output unit.

model.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'softmax'}));

4. Prepare the model for training: Specify the loss and the optimizer

model.compile({loss: 'meanSquaredError', optimizer: 'adam'});

5. Generate some synthetic data for training.

const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

6. Train the model using the data.

model.fit(xs, ys, {epochs: 1000});

7. After training, make predictions:

model.predict(tf.tensor2d([8], [1, 1])).print();

Final Remarks

The ease of implementing Machine Learning algorithms and deploying them has been changing. With TensorFlow.js adding to the list of library, we can expect smarter web applications that will incorporate ML. The code is simple to write making it easy for beginners to learn.

Check out the tutorials, examples, and documentation for more details on TensorFlow.js.

TensorFlow.js is basically the successor of deeplearn.js. The major difference between the two is the TensorFlow.js includes a layers API and imports pre-trained models and can also re-train them. You can also work on almost any GPU but it will not be close to the speed you’ll get on CUDA.

I would recommend everyone to try out this web framework for yourself and take it out for a test drive.

 

Leave a Reply

Ask Bharat