Chapter 25: TensorFlow Example 1

What is “TensorFlow.js Example 1”?

In the official TensorFlow.js documentation, most beginner YouTube videos, Udemy courses, and blog posts, Example 1 is always the same:

Training a single-neuron linear regression model in the browser to learn the relationship y ≈ 2x (with a little noise)

Why this specific example?

  • It shows the complete end-to-end ML cycle in ~30 lines of code
  • Proves that real gradient descent / backpropagation happens 100% in the browser (no Python, no server)
  • Uses the simplest possible model: one input → one output (basically a perceptron without activation)
  • Lets you watch the loss drop live in console → you feel the learning happening
  • After this, everything else (CNNs, LSTMs, transfer learning, image classification) builds on the same pattern

So let’s do it properly — full working code + explanation.

Full Working Code for TensorFlow.js Example 1

Create a file called tfjs-example1.html and paste this entire code:

HTML

How to Run It Right Now (30 Seconds)

  1. Copy the entire code above
  2. Paste into a new file → save as tfjs-example1.html
  3. Double-click the file (opens in Chrome/Edge/Firefox)
  4. Press F12 → go to Console tab
  5. Click the big blue button
  6. Watch:
    • Loss starts high (maybe 50–200)
    • Every 30 epochs it prints lower loss
    • Final prediction for x=15 should be very close to 30 (usually 29.8–30.2 after 300 epochs)

Line-by-Line Breakdown (Teacher Mode)

  • xs & ys: Our tiny training dataset (10 examples). Real data would be much larger.
  • tf.sequential(): Creates a linear stack of layers (like a simple pipeline).
  • tf.layers.dense({units: 1, inputShape: [1]}): One neuron that takes 1 number and outputs 1 number. Learns slope (weight) and intercept (bias).
  • model.compile(…): Tells TensorFlow: “Use SGD optimizer to minimize mean squared error”.
  • model.fit(xs, ys, {epochs: 300, callbacks: …}): The training loop. Each epoch = full pass over 10 examples.
  • onEpochEnd callback: Prints loss so we see progress (without flooding console).
  • model.predict(testInput): After training, use the learned weights to guess for new x=15.
  • dispose(): Frees GPU/browser memory (very important — tf.js tensors don’t auto-free like Python).

What You Should See (Typical Console Output)

text

If loss doesn’t drop much → common mistake: too few epochs, wrong optimizer, or data not scaled.

Why This Example is Called “Example 1” Everywhere

  • It is literally Example 1 in the official TensorFlow.js “Get Started” guide
  • It appears first in almost every tutorial video/blog/course
  • It teaches the core cycle: data → model → compile → fit → predict
  • It proves TensorFlow.js can do real training (not just inference) in browser

In 2026 this tiny example is still the #1 starting point — because once you understand this, adding layers, CNNs, transfer learning, or tfjs-vis graphs becomes easy.

Got it completely now? 🔥

Want next?

  • Add live loss graph with tfjs-vis to this example?
  • Version with multiple features (e.g. flat price predictor)?
  • How to save/load this model in browser?

Just tell me — next class is ready! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *