[ GRADIENT DESCENT & OPTIMIZATION COMMAND ]

Algorithms do not guess the solution; they find it by tracking the slope. Adjust the learning rate to see how an AI system navigates a cost landscape to find the global minimum.

CURRENT POSITION: [ --- ]
CURRENT COST: [ --- ]
EPOCHS: [ 0 ]
STATUS: [ READY ]
  Learning Rate (α) 0.20
  Starting Coordinates (θ0, θ1) 8.0

The Optimization Blueprint

Behind the scenes, gradient descent relies on formal calculus to navigate the landscape.

The Cost Function (MSE):
J(θ0, θ1) = (1 / 2m) Σ (hθ(x(i)) - y(i)

The Iterative Update Rule:
θj := θj - α * (∂ / ∂θj) J(θ)
  1. The Partial Derivative (∂ / ∂θj) J(θ) acts as the local slope indicating the direction of downhill descent.
Critical Prerequisite In Multiple Linear Regression, all parameters (θ0, θ1, etc.) MUST be updated SIMULTANEOUSLY during each epoch. You cannot use a newly updated θ0 to calculate the new θ1 in the same step.
Loading Python Environment...

[ APPLIED OPTIMIZATION (SGD) ]

Utilize Scikit-Learn's Stochastic Gradient Descent regression model to solve a dataset.

Incoming Transmission...

Import SGDRegressor from sklearn.linear_model.

from sklearn.linear_model import SGDRegressor
model = SGDRegressor()
model.fit(X, y)

Decryption Complete...

  • SGDRegressor uses an iterative approach.
  • Instantiate it with a constant learning rate to mimic our simulator:
    model = SGDRegressor(learning_rate='constant', eta0=0.01, max_iter=200)
  • eta0 is the starting Alpha (α) and max_iter defines the total allowed training loops (epochs).
Waiting for execution...

[ FROM SCRATCH OPTIMIZATION LOOP ]

Write a raw Python for loop that manually computes the gradients and updates weights over 100 epochs using matrix operations.

Incoming Transmission...

The gradient is the average prediction error scaled by the input feature. Compute the predictions, calculate the errors, and update both thetas simultaneously using alpha.

Decryption Complete...

  • Step 1 (Predictions): Inside the loop, calculate the current hypothesis: predictions = theta_0 + theta_1 * X
  • Step 2 (Errors): Compute the raw deviation from the truth: errors = predictions - y
  • Step 3 (The Derivatives): Translate the partial derivative math into vector operations:
    gradient_0 = (1 / len(X)) * np.sum(errors)
    gradient_1 = (1 / len(X)) * np.sum(errors * X)
  • Step 4 (The Simultaneous Update): Apply the update rule directly using the learning rate:
    theta_0 -= alpha * gradient_0
    theta_1 -= alpha * gradient_1
  • Print out the final parameters after the loop terminates.
Waiting for execution...