Build With Me: Your First Predictor
Training the Machine
It's time to build an actual Machine Learning model. We are going to code a Linear Regression training loop from scratch. Instead of just looking at loss numbers in a terminal, we are going to watch the model 'learn' in real-time in the Model Arena on the left.
Step 1: Setting up the Architecture
First, we define our hyper-parameters. These are the settings that control how fast our model learns and how long it trains.
import numpy as np
# Define how long to train, and how big of a step to take each time
EPOCHS = 100
LEARNING_RATE = 0.01
print(f"Initializing model for {EPOCHS} epochs...")Step 2: The Training Loop
A model learns by making a guess, checking how wrong it is (Loss), and adjusting itself. Let's write that loop.
# Initialize random weights
m = np.random.randn() # Slope
b = np.random.randn() # Intercept
for epoch in range(EPOCHS):
# 1. Make a prediction (y = mx + b)
predictions = (m * X_train) + b
# 2. Calculate the Error (Mean Squared Error)
loss = np.mean((predictions - y_train) ** 2)
# 3. Optimize (Simulated for this lab)
# The model updates 'm' and 'b' to lower the loss
if epoch % 10 == 0:
print(f"Epoch {epoch} | Loss: {loss:.4f}")Your Turn: Hit Train Model. Watch the visualizer on the left. The red line starts off terribly wrong. But as the epochs pass and the loss drops, watch how the line bends and snaps perfectly into the data points!
Step 3: Experimenting with Epochs
What happens if we don't train it long enough? Change EPOCHS = 100 to EPOCHS = 15 in your code, click Reset, and hit Train again.
You will see that the model stops training before the line fully fits the data. This is called Underfitting. To get good predictions, you must let the gradient descent find the optimal bottom of the loss curve.
Knowledge Check
Ready to test your understanding of Build With Me: Your First Predictor?