Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thomasnield/5447a085064f5483697cf305573f7acc to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/5447a085064f5483697cf305573f7acc to your computer and use it in GitHub Desktop.
deep_learning_from_scratch_exercise_3.py
import pandas as pd
import torch
"""
Complete the code below to perform stochastic gradient descent
on a linear regression.
Try to find a sufficient learning rate and number of iterations.
"""
# Input data
url = r"https://raw.githubusercontent.com/thomasnield/machine-learning-demo-data/master/regression/linear_normal.csv"
data = pd.read_csv(url, header=0)
# Convert data to PyTorch Tensors
X = torch.tensor(data.iloc[:, 0].values, dtype=torch.float32)
Y = torch.tensor(data.iloc[:, 1].values, dtype=torch.float32)
n = data.shape[0] # rows
# Building the model from scratch
# No requires_grad=True needed here because we are doing the math ourselves!
m = torch.tensor(0.0)
b = torch.tensor(0.0)
sample_size = 1 # sample size
L = ? # The learning Rate
epochs = ? # The number of iterations to perform gradient descent
# Performing Stochastic Gradient Descent
for i in range(epochs):
# PyTorch equivalent of np.random.choice
idx = torch.randint(0, n, (sample_size,))
x_sample = X[idx]
y_sample = Y[idx]
# The current predicted value of Y
Y_pred = m * x_sample + b
# d/dm derivative of loss function (Calculated manually)
# We use torch.sum() instead of Python's sum() for tensor speed
D_m = (-2 / sample_size) * torch.sum(x_sample * (y_sample - Y_pred))
# d/db derivative of loss function (Calculated manually)
D_b = (-2 / sample_size) * torch.sum(y_sample - Y_pred)
# Update m and b using basic arithmetic
m = m - L * D_m
b = b - L * D_b
# print progress
if i % 10000 == 0:
print(f"Iteration {i}: m = {m.item():.4f}, b = {b.item():.4f}")
print(f"\nFinal Equation: y = {m.item():.4f}x + {b.item():.4f}")
# --- MATPLOTLIB VISUALIZATION ---
import matplotlib.pyplot as plt
# 1. Convert tensors back to numpy arrays for matplotlib compatibility
x_data = X.numpy()
y_data = Y.numpy()
# 2. Calculate the predicted Y values across the whole dataset to draw the line
y_line = m.item() * x_data + b.item()
# 3. Create the plot
plt.figure(figsize=(8, 6))
# Plot the original dataset as a scatter plot
plt.scatter(x_data, y_data, color='blue', alpha=0.6, label='Original Data')
# Plot the regression line
plt.plot(x_data, y_line, color='red', linewidth=2, label=f'Regression Line\ny = {m.item():.4f}x + {b.item():.4f}')
# 4. Add styling and labels
plt.title('Linear Regression using Manual PyTorch SGD', fontsize=14)
plt.xlabel('X', fontsize=12)
plt.ylabel('Y', fontsize=12)
plt.legend(loc='upper left')
plt.grid(True, linestyle='--', alpha=0.7)
# 5. Display the chart
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment