TensorFlow.js: An intro and analysis with use cases
Tags: tensorflow, machinelearning, ai, webdev
Published: 2019-07-08 19:20:20

---
title: TensorFlow.js: An intro and analysis with use cases
published: true
tags: tensorflow,machine-learning,ai,web-development
canonical_url: https://blog.logrocket.com/tensorflow-js-an-intro-and-analysis-with-use-cases-8e1f9a973183
---

![](https://cdn-images-1.medium.com/max/1024/1*4SGH3kOTAiiK0FT2BMat_w.jpeg)

### What is TensorFlow.js?

[TensorFlow.js](https://js.tensorflow.org) is a JavaScript library developed by Google for training and using machine learning (ML) models in the browser. It’s a companion library to [TensorFlow](https://www.tensorflow.org/), a popular ML library for Python. Read on to learn about its features, its future, and how it can help you.

#### What’s machine learning?

Machine learning (often abbreviated to ML) is a field of artificial intelligence loosely defined as the study of programs and algorithms that allow computers to perform tasks without specific instructions.

A fairly typical “supervised learning” ML program works by creating a “model,” similar to a mathematical model, with inputs and outputs. It then accepts a batch of training data with inputs and actual outputs, and “trains” itself by tweaking parameters of the model in order to minimize the amount of error of the model. In other words, the program will change the model to try to make its output match the desired, “actual” output.

If you would like to learn more about machine learning or artificial intelligence, read [this article](https://medium.com/machine-learning-for-humans/why-machine-learning-matters-6164faf1df12) or [the machine learning Wikipedia entry](https://en.wikipedia.org/wiki/Machine_learning).

### The cool stuff about TensorFlow.js

Here’s a quick analysis of TensorFlow.js:

#### Speed

TensorFlow.js is hardware-accelerated because it uses WebGL (a JavaScript graphics API), so it has surprisingly good performance.

A Node.js version of Tensorflow, [tfjs-node](https://github.com/tensorflow/tfjs-node), also exists and offers improved performance over the browser version.

#### Load existing models

One of my favorite features of TensorFlow.js is that it allows you to load pretrained models. That means you can use libraries like [this one](https://github.com/tensorflow/tfjs-models) and include image classification and pose detection on your website without the need to train the model yourself.

TensorFlow.js also allows you to load models you’ve trained in the Python version of TensorFlow. That means you can write a model and train it using Python, then save it to a location available on the web and load it in your JS. This technique can significantly improve performance because you don’t have to train the model in the browser.

[![](https://cdn-images-1.medium.com/max/1024/1*wV7zU6J05BL3bphzMlB2rA.png)](https://logrocket.com/signup/)

### Use cases

More and more, businesses are using machine learning to improve interactions with users. AI programs handle everything from self-driving cars to matchmaking in video games, chatbots like Siri and Alexa, and suggesting content for users. In the past, however, machine learning has been handled on back-end servers.

The creation of TensorFlow.js means that _you can create and run AI models in a static HTML document._ Yes, you heard that right: you can use AI without setting up a server or even a database. As long as the user’s browser supports JavaScript (and preferably WebGL) you can train and use ML models, all client-side.

Here are some uses of ML (not all examples use TensorFlow.js) to fill your mind with possibilities:

- **Create abstract art:** Though this example is less “useful” for the real world (unless you want to become an art dealer), this is one of my favorite examples. Check out [this page](http://blog.otoro.net/2015/06/19/neural-network-generative-art/) and [this page](https://janhuenermann.com/blog/abstract-art-with-ml) for some beautiful and mesmerizing examples.
- **Generate realistic images:** [thispersondoesnotexist.com](https://thispersondoesnotexist.com/) recently made the news for using a generative adversarial network to generate images of completely new people. [This website](https://ai.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html) explains how a neural network developed by Google “finds” objects in unrelated images.
- **Play games:** Having AI players in video games isn’t a new idea, and there are already examples in TensorFlow.js. [This project](https://github.com/MagicCube/tensorflow-rex-run) uses TensorFlow.js to automate the Chrome Dinosaur game.
- **Recommend content** : Content recommendation through AI is fairly popular and used by most media platforms. With TensorFlow.js, content recommendation can be handled on the client side!

### Get started with TensorFlow.js

A complete tutorial for TensorFlow.js is a little outside the scope of this article, but here are some really helpful resources:

#### Tutorials

- [A Gentle Introduction to TensorFlow.js](https://medium.com/tensorflow/a-gentle-introduction-to-tensorflow-js-dba2e5257702): Helpful article that takes you through the basics of TensorFlow.js and has great examples.
- [Official TensorFlow.js Tutorials](https://www.tensorflow.org/js/tutorials): These official tutorials on the TensorFlow.js homepage include recognizing handwritten digits and creating a model that estimates the mpg of a car based on its horsepower.

#### Resources

- [ml5.js](https://ml5js.org/): ml5 is a high-level JS library that uses TensorFlow.js behind the scenes and aims to make machine learning easier for beginners. The website has tutorials, lessons, and examples including image classification, text-generation, and a drawings generator.
- [Emoji Scavenger Hunt](https://emojiscavengerhunt.withgoogle.com/): This is an interactive, AR-like game built with TensorFlow.js where you try to find emojis using your phone’s webcam.
- [Tic-Tac-Too](https://github.com/nebrelbug/tictactoo): This is an AI tic-tac-toe game that I made with TensorFlow.js using image classification techniques.
- [tfjs-vis](https://github.com/tensorflow/tfjs-vis): A self-described “set of utilities for in-browser visualization with TensorFlow.js”

### The future of TensorFlow.js

#### TensorFlow.js with progressive web apps

As PWAs become more popular, we can expect to see more and more integrations with TensorFlow.js and on-device storage. Since TensorFlow.js allows you to save models, you could create a model that trains itself on each user to provide a personalized experience, and even works offline.

#### TensorFlow.js development

With the use of machine learning constantly increasing — and with JavaScript development becoming ever more popular — TensorFlow.js seems like it will only increase in popularity in the near future, so it will probably get new features and updates often.

[According to TensorFlow](https://github.com/tensorflow/tfjs/issues/1497), a Web Assembly backend is in development as well, which should further improve performance.

### Conclusion

Now that you’ve learned a little about TensorFlow.js, go make something with it! You’ll be surprised at what you can create after only a little while. If you’re stuck, look at the documentation and the many examples on GitHub.

Happy creating!

### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

[Try it for free](https://logrocket.com/signup/).

* * *