Train a Single Neuron: Linear Regression Demo

1. Introduction to Linear Regression

Linear regression is a foundational technique in machine learning used to model the relationship between a dependent variable (y) and one independent variable (x). The goal is to find the best-fitting straight line, which is mathematically expressed as y = wx + b, where w is the weight (slope) and b is the bias (intercept).

Neuron x y × w + b
Diagram: Single neuron for linear regression. Input x is multiplied by weight w, bias b is added, and the output is y.

Example: Suppose you want to predict the price of a slice of pizza (y) by its size in inches (x). If every extra inch adds $2, and a plain slice (0 inches) costs $1, the model is:

y = 2x + 1

So, a 4-inch slice (x=4) would cost y = 2×4 + 1 = 9 dollars.

2. Train a Single Neuron for Linear Regression

In this section, we use TensorFlow.js to train a single neuron (with just one input and one output) to learn the relationship between x and y as described above.

3. Discussion: TensorFlow.js for JavaScript

TensorFlow.js is an open-source library that enables you to define, train, and run machine learning models directly in your browser or in a JavaScript environment such as Node.js. It supports both training from scratch and running pre-trained models and leverages GPU acceleration for efficient computation.

In this demo, TensorFlow.js powers the training and prediction process right in your browser!