Getting Started with TensorFlow.js | Deep Learning for JavaScript Hackers (Part 0)
Based on Venelin Valkov's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
TensorFlow.js enables TensorFlow tensor operations and model training in JavaScript, including in-browser workflows and Node.js usage.
Briefing
TensorFlow.js is positioned as a way to run machine-learning workflows directly in JavaScript—either in the browser or in Node.js—by bridging TensorFlow’s core C/C++ implementation to JS. That matters because it lets developers load pre-trained models created in Python/Keras and use them inside apps built with frameworks like React or Angular, without standing up a separate ML service. The setup starts with using TensorFlow.js documentation and demos, then moving into a “sandbox” project built with vanilla JavaScript.
The tutorial walks through installing TensorFlow.js and related dev dependencies, creating a basic HTML page, and wiring a script that waits for the DOM before running an async function. From there, it introduces tensors as TensorFlow.js’s core data structure: N-dimensional containers that hold the inputs and outputs used during model training. A first tensor example turns a JavaScript array into a 1D tensor, then checks its rank (dimensionality) and shape (size along each dimension). It also highlights a common gotcha: logging a tensor directly won’t show its values; calling methods like print (or an equivalent) is needed to reveal the underlying data.
Next comes building intuition for tensor manipulation and math. The tutorial creates 2D tensors (matrices) from 2D arrays, including an example using strings to show tensors can technically hold non-numeric values even though ML workflows typically use numbers. It demonstrates utility operations such as reshape (turning a vector into a matrix with specified rows and columns) and basic element-wise arithmetic like addition between tensors. For linear algebra, it uses dot product to multiply matrices/vectors and explains the row-by-column multiplication behavior. It also covers transpose, showing how flipping rows and columns changes the resulting matrix—an operation that generalizes to higher dimensions even if it’s harder to visualize.
To make results visible in a web app, the tutorial adds TFJS Vis, a visualization library that renders charts in the browser. It creates a bar chart by supplying an array of objects with index and value fields, then renders it into a custom div container in the HTML. It briefly notes styling options like axis labels and chart height.
The core “getting started” payoff is a minimal supervised-learning model: converting kilograms to pounds. The model is built with tf.sequential and a single dense layer with one unit, meaning it learns one parameter (the conversion factor). Training data is generated synthetically: 2,000 examples of random kilograms with corresponding pounds labels computed using the known relationship (~2.2). The model is compiled with mean squared error as the loss function and Adam as the optimizer, then trained for many epochs with shuffling enabled to avoid learning spurious ordering.
After training, the model predicts pounds for an input like 10 kg and produces a value much closer to the expected 22.2 than an untrained model. The contrast—untrained predictions being wildly off, trained predictions improving substantially—serves as the practical proof that TensorFlow.js can learn in-browser and that the tensor/math pipeline is working end-to-end. The next planned step is applying the same workflow to a real dataset for diabetes prediction and evaluating training performance.
Cornell Notes
TensorFlow.js brings TensorFlow’s tensor operations and model training to JavaScript, enabling ML in the browser or in Node.js and reuse of pre-trained models. The tutorial treats tensors as the central data structure: rank and shape describe dimensionality, and tensor values require explicit printing rather than direct console logging. It demonstrates key tensor operations—reshape, element-wise add, dot product, and transpose—then uses TFJS Vis to render a bar chart from simple data objects. Finally, it builds a tiny sequential model to learn a kilograms-to-pounds conversion factor using mean squared error loss and the Adam optimizer, showing that training dramatically improves predictions compared with an untrained model.
Why are tensors the “core data structure” in TensorFlow.js, and how do rank and shape help describe them?
What’s the practical difference between logging a tensor and printing its values?
How do reshape, dot product, and transpose change tensor data, and when are they useful?
How does TFJS Vis render a chart, and what data format does it expect for a bar chart?
What does the kilograms-to-pounds model learn, and why does training improve predictions?
Why shuffle training data, and what does “epochs” mean in this setup?
Review Questions
- How do rank and shape differ, and how would you verify them for a 2D tensor created from a 2×3 array?
- What conditions must hold for dot product to work between two tensors, and how does transpose affect those conditions?
- In the kilograms-to-pounds example, what roles do mean squared error and Adam play during training?
Key Points
- 1
TensorFlow.js enables TensorFlow tensor operations and model training in JavaScript, including in-browser workflows and Node.js usage.
- 2
Tensors are N-dimensional data containers; rank indicates dimensionality and shape indicates the size along each dimension.
- 3
Tensor values often require explicit printing to inspect contents, while rank/shape can be checked via tensor metadata.
- 4
Core tensor operations demonstrated include reshape, element-wise arithmetic, dot product, and transpose.
- 5
TFJS Vis can render charts directly into a chosen div container, using simple data objects (index/value) for bar charts.
- 6
A minimal supervised model for unit conversion can be built with tf.sequential and a single dense layer, trained using mean squared error loss and the Adam optimizer.
- 7
Training quality is validated by comparing predictions from an untrained model versus a trained model on the same input.