# Chapter 2: Stochastic Gradient DescentÂ¶

**By Tomas Beuzen đźš€**

## Chapter OutlineÂ¶

## Chapter Learning ObjectivesÂ¶

Explain and implement the stochastic gradient descent algorithm.

Explain the advantages and disadvantages of stochastic gradient descent as compared to gradient descent.

Explain what are epochs, batch sizes, iterations, and computations in the context of gradient descent and stochastic gradient descent.

## ImportsÂ¶

```
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error
from utils.plotting import *
```

## 1. Motivation for Stochastic Gradient DescentÂ¶

Last chapter we looked at â€śvanillaâ€ť gradient descent. Almost all loss functions youâ€™ll use in ML involve a sum over all the (training) data, e.g., mean squared error:

Here \(f(w)\) is the value of the loss function, \(h_w(x)\) is the model we wish to fit, e.g., for linear regression: \(h_w(x)=w^Tx\). The goal is to find the weights \(\boldsymbol{w}\) that minimize the loss function. With gradient descent we:

Start with some arbitrary \(\boldsymbol{w}\)

Calculate the gradient using all training examples

Use the gradient to adjust \(\boldsymbol{w}\)

Repeat for \(I\) iterations or until the step-size is sufficiently small

But if we have large \(n\) and/or \(\boldsymbol{w}\) vanilla gradient descent becomes very computationally expensive (when we get to deep learning, weâ€™ll have models where the number of weights to optimize is in the millions!). Say `n`

= 1,000,000, we have 1000 parameters to optimize, and we do 1000 iterations - thatâ€™s \(10^{12}\) computations!

We can reduce this workload by using just a fraction of our dataset to update our parameters each iteration (rather than using the whole data set). This is called â€ś**stochastic gradient descent**â€ť.

## 2. Stochastic Gradient DescentÂ¶

Gradient Descent:

Stochastic Gradient Descent:

Pretty simple! What exactly is the difference in the above equations? Well one of them includes a subscript \(i\). That means, instead of updating our parameters based on a gradient calculated using all training data, we simply use **one of our data points** (the \(i\)th one). This is best seen by example, letâ€™s use the Pokemon dataset from last chapter:

```
df = (pd.read_csv("data/pokemon.csv", usecols=['name', 'defense', 'attack'], index_col=0)
.reset_index()
)
x = StandardScaler().fit_transform(df[['defense']]).flatten()
y = df['attack'].to_numpy()
plot_pokemon(x, y, x_range=[0, 1], y_range=[0, 200], dx=0.2, dy=50)
```