TensorFlow.js tutorial: Get started with the ML library
Tags: datascience, machinelearning, tutorial, beginners
Published: 2021-07-30 19:15:13

TensorFlow is one of the most popular tools for machine learning and deep learning. It's used by many big tech companies such as Twitter, Uber, and Google. TensorFlow.js is a JavaScript library used for training and deploying machine learning models in the browser. TensorFlow.js was designed to provide the same features and functionalities as traditional TensorFlow, but for the JavaScript ecosystem. 

Today, we're going to dive deeper into TensorFlow and discuss its benefits, features, models, and more. We'll finish the article with a walkthrough of the Layers API, which is one of the two ways to create a machine learning model with TensorFlow.js. Let's get started!

**We’ll cover**:

* [What is TensorFlow?](#tensorflow)

* [What is TensorFlow.js?](#tfjs)

* [Installing TensorFlow.js](#install)

* [Creating a ML model with the Layers API](#createmodel)

* [Wrapping up and next steps](#nextsteps)

<br>

<b><h4>Get hands-on with machine learning</b>

This learning path will help you master important ML skills and techniques.

<b>[Become a Machine Learning Engineer](https://www.educative.io/path/become-a-machine-learning-engineer)</b></h4>

<br>

<a name="tensorflow"></a>

## What is TensorFlow?

TensorFlow is an open-source [**machine learning**](https://www.educative.io/blog/deep-vs-machine-learning) platform. It was created in 2015 by the [deep learning](https://www.educative.io/blog/deep-learning-beginner-tutorial) artificial intelligence research team at Google. TensorFlow has an extensive environment that includes many different tools, libraries, and user resources. 

TensorFlow uses machine learning and deep learning algorithms. It uses Python for an accessible front-end API for building apps within the framework and then executes those apps in [C++](https://www.educative.io/blog/top-cpp-coding-interview-questions). With TensorFlow, we can train and run **deep neural networks** for many different purposes, such as image classification, [natural language processing](https://www.educative.io/blog/what-is-natural-language-processing), and more. 

TensorFlow is known to have an intuitive and easy-to-learn set of [APIs](https://www.educative.io/blog/what-is-an-api). Their goal is to make it easy for you to learn and implement machine learning, deep learning, and scientific computing. In 2019, they released TensorFlow 2.0, which enhanced the platform and made it easier to work with and more efficient. One of their big changes was using the **Keras API** for model training.

With TensorFlow, we can create dataflow graphs. Each node we see in the graph represents a mathematical operation, and each edge between the nodes is called a **tensor**. A tensor is a multi-dimensional array.

TensorFlow has many benefits, such as:

* **Portability**: TensorFlow allows us to train and deploy our models easily, no matter what language or platform we use.

* **Powerful**: With TensorFlow, we can create complex topologies using features like the Keras Functional API and the Model Subclassing API.

* **Debugging**: We can use eager execution for quick and intuitive debugging.

* **Community**: TensorFlow has a large and active community.

* **Abstraction**: TensorFlow has multiple levels of abstraction so we can pick the right one for our needs.

<br>

<a name="tfjs"></a>

## What is TensorFlow.js?

TensorFlow.js (TFJS) is an open-source **machine learning library** that we can run anywhere that we run JavaScript. It supports WebGL, which is the browser interface to OpenGL. OpenGL is an API used for rendering 2D and 3D vector graphics. The goal of TensorFlow.js was to recreate the experience of traditional TensorFlow, but for the JavaScript ecosystem. 

TFJS allows JavaScript code to be executed on a GPU. It allows us to build deep neural networks that we can easily integrate into new or existing web applications and is heavily based on the Keras API. 

We can use TensorFlow.js in the following platforms: 

* Client-side in the web browser using JavaScript

* Server-side and with IoT tools using [Node.js](https://www.educative.io/blog/what-is-nodejs)

* Desktop and web apps using Electron

* Native mobile apps using [React Native](https://www.educative.io/blog/top-react-native-interview-questions)

<br>

### TensorFlow.js models 

TensorFlow.js provides **pre-trained, out-of-the-box machine learning models** that we can use in our projects. Some of these trained models and use cases include:

* Image classification

* Object detection

* Body segmentation

* Speech command recognition

* Simple face detection

* Transfer learning

* And much more

With these models, JavaScript developers have built a lot of really cool demos, such as:

* **LipSync by YouTube**: This demo uses the Facemesh model to score your lip-syncing accuracy.

* **Emoji Scavenger Hunt**: In this demo, you use your phone's camera to find emojis in the real world. 

* **Webcam Controller**: In this demo, you play Pac-Man using images trained in your browser.

If you want to check out the code for any of these existing models, you can visit the `tensorflow/tfjs-models` GitHub repository to dive deeper! To check out the code for the demos, you can visit `tensorflow/tfjs-examples` on GitHub.

<br>

<div style="background-color:#F1F1FF;padding-left: 10px;padding-top:1px;padding-bottom:1px;">

#### Keep the learning going. 

Learn fundamental machine learning techniques without scrubbing through videos or documentation. Educative’s text-based learning paths are easy to skim and feature live coding environments, making learning quick and efficient.

<b>[Become a Machine Learning Engineer](https://www.educative.io/path/become-a-machine-learning-engineer)</b>

</div>

<br>

<a name="install"></a>

## Installing TensorFlow.js

The two most common ways to install TensorFlow.js in your browser include:

* Installation with npm

* Installation with script tags

If we want to install using npm, we can use npm cli or yarn.

**npm cli installation**:

```js
npm install @tensorflow/tfjs
```

**yarn installation**:

```js
yarn add @tensorflow/tfjs
```

If we want to install using script tags, we can add the following tag to our main HTML file:

```js
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
```

<br>


<a name="createmodel"></a>

## Creating a ML model with the Layers API

In TensorFlow.js, there are **two ways** to create a machine learning model. We can either use the higher-level Layers API to build a model using layers, or we can use the Core API with lower-level operations. Today, we're going to take a closer look at the Layers API.

With the Layers API, we can either create a sequential model or a functional model. The sequential model is the most common type of model. We can create one by passing a list of layers to the `sequential()` function using `const`. Let's take a look:


```js
const model = tf.sequential({

indent layers: [

double indent tf.layers.dense({inputShape: [692], units: 35, activation: 'relu'}),

double indent tf.layers.dense({units: 15, activation: 'softmax'}),

indent ]

});
```

> **Note**: When providing the `inputShape`, leave out the batch size. As an example, let's say we plan to give our model tensors of shape `[X, 692]`, where `X` is any batch size. We would specify our `inputShape` as `[692]` when making our model.

<br>

<a name="nextsteps"></a>

## Wrapping up and next steps

Congrats on taking your first steps with TensorFlow.js! It's a great tool to add to your machine learning workflow. With TensorFlow, it's easy to train and deploy machine learning and [deep learning models in the browser. Machine learning is a vast field, and there's still so much more to learn. Some recommended concepts to cover next include:

* Image processing with datasets

* Building advanced data pipelines with TensorFlow

* Analyzing and manipulating data in [Pandas](https://www.educative.io/blog/python-pandas-tutorial) and Numpy

To get started learning these concepts and much more, check out Educative's learning path [**Become a Machine Learning Engineer**](https://www.educative.io/path/become-a-machine-learning-engineer). In this hands-on path, you'll learn the essential ML techniques to stand out from the competition. By the end, you'll have job-ready skills in data pipeline creation, model deployment, and interference. The path even has a chapter dedicated to deep learning with TensorFlow.

*Happy learning!*

<br>


### Continue reading about machine learning

* [The top 10 ML algorithms for data science in 5 minutes](https://www.educative.io/blog/top-10-ml-algorithms-for-data-science-in-5-minutes)
* [Cracking the machine learning interview: System design approaches](https://www.educative.io/blog/cracking-machine-learning-interview-system-design)
* [Crack the top 40 machine learning interview questions](https://www.educative.io/blog/top-machine-learning-interview-questions)