Model families ๐พ

Model architectures ๐

Metrics ๐

Solvers / Optimizers ๐งฎ

Training parameters

Augmentations

Deployment

SGD

The classic, but by now somewhat out-dated, optimizer

Deep learning optimizer literature starts with **Gradient Descent **and the** Stochastic Gradient Descent** **(SGD)** is one very widely used version of it. The gradients are not calculated for the loss functions over all data points but over a randomly selected sub-sample. This is why it is also called mini-batch gradient descent sometimes.

Hyperparameter tuning yields 1-3% marginal gains in performance. Fixing your data is usually more effective.

Intuition

Gradient Descent

The goal of each solver is to find the loss function's minimum. However, this cannot be done by just setting the derivative to 0 (as you learned to do in calculus I) because there is no closed-form solution. This is because the loss landscape of neural networks is highly non-convex and riddled with saddle points.

Have you met **Gradient Descent**? Gradient Descent is an algorithm that finds local minima. It calculates the gradient of a given point on a loss function. If the gradient is negative, it updates the weights moving to a point in the direction of the gradient; if it's positive to a point in the opposite direction. This is repeated until the algorithm converges. Then, we have found a local minimumโor are at least are very close to it.

Gradient Descent visualized. Image source in the bottom of the page

Stochastic Gradient Descent (SGD)

SGD is a more computationally efficient form of Gradient Descent.

SGD only estimates the gradient for the loss from a small sub sample of data point only, enabling it to run much faster through the iterations. Theoretically speaking, the loss function is not as well minimized as with BGD. However, in practice, the close approximation that you get in SGD for the parameter values can be close enough in many cases. Also, the stochasticity is a form of regularization, so the networks usually generalize better.

Code implementation

PyTorch

TensorFlow

1

# importing the library

2

import torch

3

import torch.nn as nn

4

โ

5

x = torch.randn(10, 3)

6

y = torch.randn(10, 2)

7

โ

8

# Build a fully connected layer.

9

linear = nn.Linear(3, 2)

10

โ

11

# Build MSE loss function and optimizer.

12

criterion = nn.MSELoss()

13

โ

14

# Optimization method uses random gradient descent, the learning rate is 0.01 and the momentum 0.9

15

optimizer = torch.optim.SGD(linear.parameters(), lr=0.01, momentum=0.9)

16

โ

17

# Forward pass.

18

pred = linear(x)

19

โ

20

# Compute loss.

21

loss = criterion(pred, y)

22

print('loss:', loss.item())

23

โ

24

optimizer.step()

Copied!

1

# importing the library

2

import tensorflow as tf

3

โ

4

opt = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9)

5

var = tf.Variable(1.0)

6

val0 = var.value()

7

loss = lambda: (var ** 2)/2.0 # d(loss)/d(var1) = var1

8

โ

9

# First step is `- learning_rate * grad`

10

step_count = opt.minimize(loss, [var]).numpy()

11

val1 = var.value()

12

print((val0 - val1).numpy())

13

โ

14

# On later steps, step-size increases because of momentum

15

step_count = opt.minimize(loss, [var]).numpy()

16

val2 = var.value()

17

print((val1 - val2).numpy())

Copied!

Further resources

โ

Last modified 1yr ago