Lesson 4 of 6 Evaluating ML Models

Debugging Training Convergence with SageMaker Debugger

This lesson explains the ways training can fail to converge and how SageMaker Debugger detects many of them automatically while a job runs.

Learning outcomes

After completing this lesson, you will be able to:

  • Relate convergence symptoms to causes.
  • Use SageMaker Debugger built-in rules.
  • Apply the typical fix for each convergence problem.

Symptoms and causes

Divergence, where loss grows or becomes NaN, usually comes from a learning rate that is too high, exploding gradients or unscaled inputs. A flat loss from the start suggests a learning rate that is too low, vanishing gradients in deep networks, saturated activations, dead ReLU units or a data problem such as shuffled labels. Oscillation suggests a learning rate or batch size problem. A plateau followed by no improvement may need a learning-rate schedule. Validation loss rising while training loss falls is overfitting rather than a convergence failure.

The following diagram compares gradient size by layer in a 30-layer network before and after the fix for vanishing gradients.

Gradient size by layer in the 30-layer network. With sigmoid activations, gradients shrink layer by layer on the way back, so the early layers receive almost none and training loss stays flat; Debugger's vanishing_gradient rule fires. With ReLU activations, batch normalisation and He initialisation, gradients stay usable through every layer and loss falls.
Gradient sizes are schematic; every third layer is shown.

SageMaker Debugger

SageMaker Debugger captures tensors (weights, gradients, losses) during training and evaluates built-in rules in parallel: vanishing_gradient, exploding_tensor, loss_not_decreasing, overfit, overtraining, poor_weight_initialization, saturated_activation, dead_relu and class_imbalance, among others. Rules publish status to CloudWatch and can trigger actions such as stopping the training job or sending a notification, which saves cost when a run is clearly failing. Custom rules can be written too.

Match the pairs

Debugger rules

Exam skills 2.3.5

Match each SageMaker Debugger built-in rule with the problem it detects.

Log in to try this activity Interactive activities are free with an account.

Fixes

Each convergence problem points to a typical cause. A loss that does not decrease often means the learning rate is too low, the data is not shuffled or the labels are wrong. Exploding values usually mean the learning rate is too high or the inputs are not scaled. Vanishing gradients in deep networks call for different activation functions, better initialisation or normalisation layers, and overfitting calls for regularisation or more data. The following table maps convergence symptoms to the usual fixes.

Symptom (Debugger rule)Fix
Exploding tensor / NaN lossLower learning rate, gradient clipping, normalise inputs
Vanishing gradientReLU-family activations, batch normalisation, residual connections, better initialisation
Loss not decreasingRaise learning rate, check labels and data pipeline, check initialisation
Saturated activation / dead ReLUChange activation (Leaky ReLU), lower learning rate, normalise
Overfit / overtrainingEarly stopping, regularisation, more data
Troubleshooting scenario

A network that will not learn

Exam skills 2.3.5 · 2.2.7

Fix a deep network's convergence problems one at a time.

Log in to try this activity Interactive activities are free with an account.
Worked example

A failing deep network

A 30-layer network's training loss stays flat at its initial value for hours. Debugger flags vanishing_gradient on early layers, which use sigmoid activations.

  • Replace sigmoid with ReLU activations and add batch normalisation between layers.
  • Use He initialisation suited to ReLU.
  • Configure the Debugger rule action to stop the job automatically if the rule fires again.

Loss now falls steadily. Raising the learning rate alone would not have helped, because gradients were close to zero by the time they reached the early layers.

Check yourself

What causes a flat loss in a deep network, and what helps?

Show answer

Vanishing gradients. ReLU activations and batch normalisation help.

Exam Tips

Remember for the exam

  • NaN or growing loss: exploding gradients or learning rate too high; clip gradients.
  • Flat loss in deep networks: vanishing gradients; ReLU, batch normalisation.
  • Automatic detection during training: SageMaker Debugger built-in rules.
  • Debugger rule actions can stop a failing job to save cost.
  • Rising validation loss with falling training loss is overfitting, not divergence.