# Initializes parameters "a" and "b" randomly np.random.seed(42) a = np.random.randn(1) b = np.random.randn(1) print(a, b) # Sets learning rate lr = 1e-1 # Defines number of epochs n_epochs = 1000 for epoch in range(n_epochs): # Computes our model's predicted output yhat = a + b * x_train # How wrong is our model? That's the error! error = (y_train - yhat) # It is a regression, so it computes mean squared error (MSE) loss = (error ** 2).mean() # Computes gradients for both "a" and "b" parameters a_grad = -2 * error.mean() b_grad = -2 * (x_train * error).mean() # Updates parameters using gradients and the learning rate a = a - lr * a_grad b = b - lr * b_grad print(a, b) # Sanity Check: do we get the same results as our gradient descent? from sklearn.linear_model import LinearRegression linr = LinearRegression() linr.fit(x_train, y_train) print(linr.intercept_, linr.coef_[0])