Single Neuron Linear Regression Trainer

Chapter 1: Introduction to Linear Regression

Linear regression is a simple yet powerful statistical technique used to model the relationship between two variables by fitting a straight line, known as the regression line, through a set of data points. The equation of the line is typically y = wx + b, where w is the weight (slope), and b is the bias (intercept). In machine learning, training a single neuron for linear regression involves adjusting w and b to minimize the difference between the predicted and actual values.

Example: Predicting House Price

Imagine a simple scenario: the price of a house depends mainly on its size.
Let's say:

The model calculates: y = w * x + b
For a house of 2,000 sq ft (x = 2):
y = 50,000 * 2 + 100,000 = 200,000
So, the predicted price is $200,000.

Chapter 2: Train a Single Neuron for Linear Regression

Try training a single neuron below! The model will learn to fit a line to the sample data points.

Chapter 3: Discussion about TensorFlow.js

TensorFlow.js is an open-source library that brings machine learning to JavaScript. It enables you to define, train, and run models entirely in the browser or in Node.js, using the same core APIs as TensorFlow in Python. This allows for direct deployment of machine learning models on the client-side, enabling interactive and privacy-preserving ML applications without server dependencies. TensorFlow.js supports a range of hardware acceleration, making it possible to train neural networks efficiently even in the browser.

In this demo, TensorFlow.js is used to define and train a very simple neural network (a single neuron) to perform linear regression. The weights and bias are updated using gradient descent, and the training process is visualized for you interactively.

Chapter 4: Libraries for Modeling and Visualization

Early web-based machine learning and visualization relied on several foundational JavaScript libraries:

In this demonstration, TensorFlow.js (for model definition and training) and the browser’s built-in <canvas> API (for simple visualization) are used. Early machine learning demos often combined these or similar libraries to provide interactive web experiences and educational tools.

Chapter 4: Libraries for Modeling and Visualization

Early web-based machine learning and visualization relied on several foundational JavaScript libraries:

In this demonstration, TensorFlow.js (for model definition and training) and Chart.js (for visualization) will be used. Click the button below to load these libraries. They will be used in later chapters or examples.

Chapter 5: Discussion of the Cars Dataset

The dataset used in this tutorial is provided by Google TensorFlow.js and contains information about various cars, their miles per gallon (MPG), and horsepower. Each entry is an object with properties such as Miles_per_Gallon and Horsepower, along with other vehicle characteristics.

This data is commonly used for regression tasks, like predicting MPG based on horsepower. However, the raw data may include missing or non-numeric values. To ensure data quality, we filter out any entries with missing Miles_per_Gallon or Horsepower values and convert these fields to numbers.

Click the button below to load and clean the dataset. After loading, the cleaned dataset will be accessible as cleanedData for use in future examples.


Chapter 6: Visualization of the Cleaned Dataset

After loading, you can visualize the relationship between Miles_per_Gallon and Horsepower using a scatter plot. These two columns, Miles_per_Gallon (used as mpg) and Horsepower (used as horsepower), are selected as input and output for model training.


Chapter 7: Splitting the Dataset

Splitting a dataset into training and testing parts is an important step in machine learning. The training set is used to fit the model, while the testing set is used to evaluate its performance. A common split is 80% for training and 20% for testing. The split should be random to ensure that both sets represent the overall dataset fairly.


Chapter 8: Defining and Understanding the Neural Network Model

In this chapter, you will define an artificial neural network (ANN) model using TensorFlow.js. The model will be used to predict Miles Per Gallon (MPG) from Horsepower, using the training set created earlier.

What is this model?

Input Horsepower H1 H2 Output MPG

This neural network is a simple regression model. It receives the horsepower of a car as input and aims to predict its miles per gallon (MPG) value. The network consists of:

During training, the model learns the best way to map horsepower to MPG using the training set. After training, it can predict MPG for cars it has never seen before.



Chapter 9: Training the Neural Network Model

Now that you have defined the model, the next step is to train it so that it can predict MPG from horsepower. When you click the button below, the model will be trained using all the entries in cleanedData. You will see the training progress displayed live, including the epoch number, loss value, and a chart showing the loss decreasing over epochs.



Chapter 10: Evaluating the Trained Model

To understand how well the trained model generalizes to new data, we evaluate it on the reserved test set. This chapter demonstrates the evaluation process and displays a chart comparing the model's predictions with actual values from the test set.


Chapter 11: Understanding the Confusion Matrix

A confusion matrix is a summary table used to evaluate the performance of a classification model. For regression tasks (like predicting MPG), we can convert the predictions into categories (for example: Low, Medium, High efficiency) to see how well the model classifies data into these ranges.

The confusion matrix shows how many predictions fall into the correct or incorrect categories. Each row represents the actual class, and each column represents the predicted class.