Created
July 13, 2023 22:19
-
-
Save mitzen/6be6d82cdd08654a641fd941107fae31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment