Approximately periodic jumps in TensorFlow model loss

Viewed 591

I am using tensorflow.keras to train a CNN in an image recognition problem, using the Adam minimiser to minimise a custom loss (some code is at the bottom of the question). I am experimenting with how much data I need to use in my training set, and thought I should look into whether each of my models have properly converged. However, when plotting loss vs number of epochs of training for different training set fractions, I noticed approximately periodic spikes in the loss function, as in the plot below. Here, the different lines show different training set sizes as a fraction of my total dataset.

As I decrease the size of the training set (blue -> orange -> green), the frequency of these spikes appears to decrease, though the amplitude appears to increase. Intuitively, I would associate this kind of behaviour with a minimiser jumping out of a local minimum, but I am not experienced enough with TensorFlow/CNNs to know if that is the correct way to interpret this behaviour. Equally, I can't quite understand the variation with training set size.

Can anyone help me to understand this behaviour? And should I be concerned by these features?

enter image description here

from quasarnet.models import QuasarNET, custom_loss
from tensorflow.keras.optimizers import Adam

...

model = QuasarNET(
        X[0,:,None].shape, 
        nlines=len(args.lines)+len(args.lines_bal)
        )

loss = []
for i in args.lines:
    loss.append(custom_loss)

for i in args.lines_bal:
    loss.append(custom_loss)

adam = Adam(decay=0.)
model.compile(optimizer=adam, loss=loss, metrics=[])

box, sample_weight = io.objective(z,Y,bal,lines=args.lines, 
        lines_bal=args.lines_bal)

print( "starting fit")
history = model.fit(X[:,:,None], box,
        epochs = args.epochs,
        batch_size = 256,
        sample_weight = sample_weight)
1 Answers

Following some discussion from a colleague, I believe that we have solved this problem. As a default, the Adam minimiser uses an adaptive learning rate that is inversely proportional to the variance of the gradient in its recent history. When the loss starts to flatten out, the variance of the gradient decreases, and so the minimiser increases the learning rate. This can happen quite drastically, causing the minimiser to "jump" to a higher loss point in parameter space.

You can avoid this by setting amsgrad=True when initialising the minimiser (http://www.satyenkale.com/papers/amsgrad.pdf). This prevents the learning rate from increasing in this way, and thus results in better convergence. The (somewhat basic) plot below shows loss vs number of training epochs for the normal setup, as in the original question (norm loss) compared to the loss when setting amsgrad=True in the minimiser (amsgrad loss).

Clearly, the loss function is much better behaved with amsgrad=True, and, with more epochs of training, should result in a stable convergence. enter image description here

Related