#native_company# #native_desc#
#native_cta#

Why do JavaScript developers need to learn TensorFlow.js?

Let's get physical

Sometimes, nothing beats holding a copy of a book in your hands. Writing in the margins, highlighting sentences, folding corners. So this book is also available from Amazon as a paperback.

Buy now on Amazon

People sometimes laugh at me when I tell them I focus on Machine Learning with JavaScript.

The initial reaction is something along the lines of “but…​ but…​ JavaScript isn’t very fast?”.

What’s annoying is that they are right, but also completely wrong, and then right again, but for the wrong reasons, let me explain.

Ok, so how does TensorFlow work? You have probably heard a lot about people using Python write with TensorFlow. That’s right, well how about this: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python.html

Python vs. NodeJS performance
Figure 1. Python vs. NodeJS performance

Note

Yes, JavaScript is much much faster than Python!

What’s going on then, why is Python the choice for Machine Learning?

Here is where it gets interesting, whether you are writing some TensorFlow with JavaScript, or Python all your code is doing is creating a Data Flow Graph. Once you’ve created the graph, you don’t need JavaScript or Python anymore. A Data Flow Graph executes as fast and optimized C code. You pump some numbers into the graph, lots of computation happens (in code that was written in C and compiled into machine code), and some numbers get popped out. That’s it.

So they are completely wrong, whether you are writing in JavaScript or Python, the resulting Data Flow Graph will be the same and will be just as fast when executed. But, annoyingly, they are also entirely right.

Important

When using TensorFlow in the browser, you are using a version that has been re-written from the ground up in JavaScript! So in the browser, calculations are run using JavaScript, and it’s a lot slower. However, when running as JavaScript in NodeJS, it’s using the C++ version of TensorFlow, so it runs at the same speed as Python.

Why use TensorFlow.js at all?

  • So we never expect someone to do intensive number-crunching in the browser.

  • We will crunch those numbers and train machine learning models on something much more powerful than a single browser tab.

  • Once that model has been trained, we can save it to a JSON file

  • And then we can use the model in the browser!

  • To use that model in the browser, we need a version of TensorFlow that works all by itself without installing some C++ application. That’s why TensorFlow.js was re-written from the ground up as JavaScript, so we could quickly load and run a pre-trained model in the browser.

Summary

Although you could train models using JavaScript and have the same performance as Python (if you used NodeJS), I don’t think we’ll be doing that with JavaScript.

I believe in the future, we’ll work in teams where some of the team will be creating machine learning models using Python. When they are ready, it’s up to us, JavaScript developers, to take those models and build an app around it.

That’s why I believe we need to learn TensorFlow.js. Machine Learning is the future, and I want to be there before anyone else.



Advanced JavaScript

This unique course teaches you advanced JavaScript knowledge through a series of interview questions. Bring your JavaScript to the 2021's today.

Level up your JavaScript now!