20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

184 CHAPTER 7 Telling birds from airplanes: Learning from images

of the gradient on a single sample. However, what is a good direction for reducing the

loss based on one sample might not be a good direction for others. By shuffling samples

at each epoch and estimating the gradient on one or (preferably, for stability) a few

samples at a time, we are effectively introducing randomness in our gradient descent.

Remember SGD? It stands for stochastic gradient descent, and this is what the S is about:

working on small batches (aka minibatches) of shuffled data. It turns out that following

gradients estimated over minibatches, which are poorer approximations of gradients

estimated across the whole dataset, helps convergence and prevents the optimization

process from getting stuck in local minima it encounters along the way. As depicted in

figure 7.13, gradients from minibatches are randomly off the ideal trajectory, which is

part of the reason why we want to use a reasonably small learning rate. Shuffling the

dataset at each epoch helps ensure that the sequence of gradients estimated over minibatches

is representative of the gradients computed across the full dataset.

Typically, minibatches are a constant size that we need to set prior to training, just

like the learning rate. These are called hyperparameters, to distinguish them from the

parameters of a model.

GRADIENT

OVER

MINIBATCH

GRADIENT

OVER ALl DATA

UPDATE OVER

MINIBATCH

LOSs

Figure 7.13 Gradient descent averaged over the whole dataset (light path) versus stochastic

gradient descent, where the gradient is estimated on randomly picked minibatches

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!