1
$\begingroup$

I have been reading the Deep Learning book by Ian Goodfellow and on pg. 277, they mention:

It is also crucial that the minibatches be selected randomly. Computing an unbiased estimate of the expected gradient from a set of samples requires that those samples be independent. We also wish for two subsequent gradient estimates to be independent from each other, so two subsequent minibatches of examples should also be independent from each other

I understand that for any unbiased estimation we require the samples to be i.i.d. so that we ideally end up with a true representation of the underlying data and so the above statement makes sense. However in practice, the samples that SGD sees for the subsequent gradient update (the next epoch) are the same, and it still performs well in the sense that the training error decreases. The authors later mention :

...but of course, the additional epochs usually provide enough benefit due to decreased training error to offset the harm they cause by increasing the gap between training error and test error

I know this happens but if someone could explain to me why and how it happens perhaps from a statistical perspective, it would be great! Another way to put it: why does training error in SGD decrease in subsequent epochs even though the samples are not i.i.d anymore?

$\endgroup$

1 Answer 1

1
$\begingroup$

The samples might be the same in each epoch, but, before starting the new epoch, the training dataset might be randomly shuffled before splitting it into mini-batches (this is the default behaviour in Keras), which helps to de-correlate the samples.

For example, suppose that your training data consists of $D =\{4, 2, 3, 1, 5, 0 \}$. Now, you decide to perform mini-batch GD with batch size $m = 2$. You also shuffle the training data before each epoch, so before splitting the training data into mini-batches. In the first epoch, you could have batches $B_1 = \{3, 0 \}$, $B_2 = \{4, 5 \}$ and $B_3 = \{2, 1 \}$. In the second epoch, we could have $B_1 = \{4, 1 \}$, $B_2 = \{3, 0 \}$ and $B_3 = \{5, 2 \}$, and so on. So, if there was any spurious/noisy information associated with some specific order of the samples, this would unlikely bias the computation of the gradient.

This should give you some intuition about why shuffling might help to avoid the introduction of any bias due to some specific order of the samples.

$\endgroup$
5
  • $\begingroup$ Got it. I did not know that Keras randomly shuffles after each epoch. As far as I know in Pytorch, the shuffling only takes place once at the start of the training, which is why I assumed that the samples for subsequent epochs are not iid $\endgroup$
    – Kunj Mehta
    Commented Apr 12, 2022 at 21:57
  • $\begingroup$ Hm, it's strange that this is the default behaviour in PyTorch. I would like to know why. $\endgroup$
    – nbro
    Commented Apr 12, 2022 at 22:02
  • $\begingroup$ my bad, Pytorch also shuffles every epoch. $\endgroup$
    – Kunj Mehta
    Commented Apr 12, 2022 at 22:11
  • $\begingroup$ Are you sure? The PyTorch documentation says that the DataLoader has a default parameter shuffle=False. It's been a long time since I used PyTorch. $\endgroup$
    – nbro
    Commented Apr 12, 2022 at 22:16
  • $\begingroup$ Yes, by default it is false. But when set to true, it shuffles data at every epoch $\endgroup$
    – Kunj Mehta
    Commented Apr 12, 2022 at 22:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .