ML Models From Scratch ยท Medium
Linear Regression Trainer
Train linear regression end to end with explicit gradients and parameter updates.
Task
Implement class LinearRegression(initial_weights: np.ndarray, initial_bias: float, learning_rate: float). Implement an end-to-end linear regression trainer with explicit forward, loss, backward, step, fit, and predict methods.
Requirements
- Copy the initial weights so training does not mutate the caller's array.
- forward(x) and predict(x) return x @ weights + bias for a batch of rows.
- loss(predictions, targets) returns mean squared error.
- backward(x, predictions, targets) stores weight_grad and bias_grad for mean squared error.
- step() applies one gradient-descent update using learning_rate.
- fit(x, y, epochs) records the loss before each update in loss_history.
- Do not call an autograd backward API in the core implementation.
Example
model = LinearRegression(
np.array([0.0]),
initial_bias=0.0,
learning_rate=0.1,
)
model.fit(
np.array([[1.0], [2.0]]),
np.array([3.0, 5.0]),
epochs=2,
)
model.predict(np.array([[1.0], [2.0]]))
# array([2.76, 4.47])