<jupyter_start><jupyter_text>Regression, Gradient DescentThis notebook shows how to solve regression problems with gradient descent through [Autograd](https://github.com/HIPS/autograd). In some machine learning algorithms, a [loss function](https://en.wikipedia.org/wiki/Loss_function) must be defined to gauge at how much learning the algorithm has accomplished. After the loss function is defined, you will then need to express the gradient of such loss function. The shape of the gradient of the loss function is typically concave, and you may use [gradient descent](https://en.wikipedia.org/wiki/Gradient_descent) to optimize it (e.g. find the minimum of the gradient of the loss function). However, expressing the gradient of the loss function is usually non-trivial to express in code form. With Autograd, you simply define the loss function and this package will give you the gradient (first derivative). For linear regression, $y = b + w'x$, the loss function is defined as follows.$L(\hat{Y}, Y) = \frac{1}{N} \sum{(y - \hat{y})^2} = \frac{1}{N} \sum{(y - (b + w'x))^2}$where* $\hat{Y}$ is the predicted value,* $Y$ is the real value,* $b$ is the y-intercept,* $w$ is the vector of weights (coefficients), and* $x$ is the vector of observations.The gradients of the loss function, $\nabla L(\hat{Y}, Y)$, is simply the partial derivatives with respect to the variables $b$ and $w$.* $\nabla_b = \frac{\partial L}{\partial b} = \frac{2}{N} \sum{-(y - (b + wx))}$* $\nabla_{w_{1}} = \frac{\partial L}{\partial w_1} = \frac{2}{N} \sum{-x_1 (y - (b + w'x))}$* $\ldots$* $\nabla_{w_{n}} = \frac{\partial L}{\partial w_n} = \frac{2}{N} \sum{-x_n (y - (b + w'x))}$With Autograd, you do not have to write out all these gradients, you only write out the loss function. It's really amazing to see how your code becomes more concise and easy to understand.  Examples ImportsNotice that you don't import `numpy` but instead `autograd.numpy`?<jupyter_code>import autograd.numpy as np
from autograd.numpy.random import normal
import pandas as pd
import matplotlib.pyplot as plt
from autograd import grad

np.random.seed(37)<jupyter_output><empty_output><jupyter_text>Define the loss function and its gradient using Autograd<jupyter_code>def loss(w, X, y_true):
    """
    The loss function is 1/n * (y_predicted - y_true)^2
    """
    y_pred = np.dot(X, w)
    loss = ((y_pred - y_true) ** 2.0)
    return loss.mean(axis=None)

#the magic line that gives you the gradient of the loss function
loss_grad = grad(loss) 

# here we simulate 5,000 samples for 2 examples
n = 5000<jupyter_output><empty_output><jupyter_text>Example 1Here, we specify a simple linear model $y = 5 + 2.0x_0$.* $X_0 \sim \mathcal{N}(2, 1)$* $Y \sim \mathcal{N}(5 + 2.0x_0, 1)$<jupyter_code>X = np.hstack([
        np.ones(n).reshape(n, 1), 
        normal(2.0, 1.0, n).reshape(n, 1)
    ])
y = normal(5.0 + 2.0 * X[:, 1], 1, n)
w = np.array([np.random.randn() for _ in range(X.shape[1])])

for i in range(1000):
    w = w - loss_grad(w, X, y) * 0.01
    
print('intercept + weights: {}'.format(w))<jupyter_output>intercept + weights: [4.92702295 2.027472  ]<jupyter_text>Example 2Here, we specify a more complicated linear model with $y = 5 + 2x_0 + 1x_1 + 3x_2 + 0.5x_3 + 1.5x_4$. Note, we can reuse the same gradient loss function as before for both simple and multiple linear regression problems.* $X_0 \sim \mathcal{N}(2, 1)$* $X_1 \sim \mathcal{N}(1, 1)$* $X_2 \sim \mathcal{N}(-1, 1)$* $X_3 \sim \mathcal{N}(-2, 1)$* $X_4 \sim \mathcal{N}(0.5, 1)$* $Y \sim \mathcal{N}(5 + 2x_0 + 1x_1 + 3x_2 + 0.5x_3 + 1.5x_4, 1)$<jupyter_code>X = np.hstack([
        np.ones(n).reshape(n, 1), 
        normal(2.0, 1.0, n).reshape(n, 1),
        normal(1.0, 1.0, n).reshape(n, 1),
        normal(-1.0, 1.0, n).reshape(n, 1),
        normal(-2.0, 1.0, n).reshape(n, 1),
        normal(0.5, 1.0, n).reshape(n, 1)
    ])
y = normal(5.0 + 2.0 * X[:,1] + 1.0 * X[:,2] + 3.0 * X[:,3] + 0.5 * X[:,4] + 1.5 * X[:, 5], 1.0, n)
w = np.array([np.random.randn() for _ in range(X.shape[1])])

for i in range(1000):
    w = w - loss_grad(w, X, y) * 0.01
    
print('intercept + weights: {}'.format(w))<jupyter_output>intercept + weights: [4.36847588 2.10524944 1.03287861 2.92075188 0.39290605 1.5431474 ]
{'fraction_non_alphanumeric': 0.16086547507055504, 'fraction_numerical': 0.05268109125117592, 'mean_word_length': 3.8329545454545455, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '272496', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1551, 'n_words': 513, 'path': 'sphinx/datascience/source/autograd-regression-gradient-descent.ipynb', 'repo_name': 'oneoffcoder/books', 'size': '4252'}