Debugging ML Models

From David's Wiki
Revision as of 15:05, 10 August 2020 by David (talk | contribs)
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)

Notes on debugging ML models, primarilly CNNs.
Most of this is advice I've found online or gotten through mentors.

Debugging

  • Train on a single example and see if it overfits.
    • If it doesn't overfit, there may be an issue with your code.
    • You can try increasing the capacity (e.g number of filters or number of nodes in FC) 2-4x.
      If the input is 3 channels, then the first conv layer should have more than 3 channels.
    • Check that your loss is implemented correctly and taken against the correct ground truth image.
  • Dump all inputs and outputs into TensorBoard. You may have an unexpected input or output somewhere.
  • Make sure there is no activation on the final layer.

Underfitting

If it looks like it is underfitting (e.g. if the training output and validation output are both blurry), then you can try the following.

  • Train for 4x as long until the training loss and validation loss both flatten.
  • Increase or decrease the learning rate one magnitude.
  • Make sure the batch size is a multiple of 2. Try increasing it to get more stable gradient updates or decreasing it to get faster iterations.
  • Try disabling any tricks you have like dropout.

Overfitting

Overfitting occurs when your training loss is below your validation loss.
Historically this was a big concern for ML models and people relied heavily on regularization to address overfitting.
Recently though, overfitting has become less of a concern with larger ML models.

  • Increase the capacity and depth of the model.
  • Add more training data if you can.
  • Depending on your task, you can also try data augmentation (e.g. random crops, random rotations, random hue).

NaNs and Infs

You can get NaNs and Infs in either the forward pass or the backward pass.
To determine where, check the outputs of each layer and check the gradients.

In PyTorch, you can do:

assert torch.isfinite(my_tensor).all(), "my_tensor has NaNs or Infs"

In Tensorflow, you can do:

def all_finite(tensor):
  is_finite = tf.math.is_finite(tensor)
  all_finite = tf.math.reduce_all(is_finite)
  return all_finite.numpy()

assert all_finite(my_tensor), "my_tensor has NaNs or Infs"

Typically, you can Infs and NaNs when there is an division by 0 in the forward or backward pass.
However it is also possible that the learning rate is too high or your model is broken.
I typically debug by:

  • Dropping the learning rate to something super small 1e-10
  • Determining whether the NaN/Inf is in the forward or backward pass.
  • Checking that the training data has no NaNs or Infs.
  • Checking that there are no divides anywhere in the code or that all divides are safe.
  • Checking the gradients of trig functions in the code.

\( \DeclareMathOperator{\arcsec}{arcsec} \DeclareMathOperator{\arccot}{arccot} \DeclareMathOperator{\arccsc}{arccsc} \) The following functions have unstable (e.g. Inf) gradients at certain points:

  • Derivative of \(\log(x)\) is \(1/x\) which is Inf near 0
  • Derivative of \(\arcsin(x)\) and \(\arccos(x)\) are \(\frac{\pm 1}{\sqrt{1-x^2}}\) which are Inf near 1.
  • The derivatives of \(\arccsc(x)\) and \(\arcsec(x)\) are Inf near 0 and 1.

If you must use one of these functions near the unstable points, I suggest performing a linear approximation near 0 and 1.

Other ways to mitigate NaNs and Infs are: