Skip to content

Instantly share code, notes, and snippets.

@ziwenhan
Created November 14, 2018 06:26
Show Gist options
  • Save ziwenhan/89b76b00417eff428a6bc9a635487b52 to your computer and use it in GitHub Desktop.
Save ziwenhan/89b76b00417eff428a6bc9a635487b52 to your computer and use it in GitHub Desktop.
Deep-Learning Nan loss reasons
https://stackoverflow.com/questions/40050397/deep-learning-nan-loss-reasons
There are lots of things I have seen make a model diverge.
Too high of a learning rate. You can often tell if this is the case if the loss begins to increase and then diverges to infinity.
I am not to familiar with the DNNClassifier but I am guessing it uses the categorical cross entropy cost function. This involves taking the log of the prediction which diverges as the prediction approaches zero. That is why people usually add a small epsilon value to the prediction to prevent this divergence. I am guessing the DNNClassifier probably does this or uses the tensorflow opp for it. Probably not the issue.
Other numerical stability issues can exist such as division by zero where adding the epsilon can help. Another less obvious one if the square root who's derivative can diverge if not properly simplified when dealing with finite precision numbers. Yet again I doubt this is the issue in the case of the DNNClassifier.
You may have an issue with the input data. Try calling assert not np.any(np.isnan(x)) on the input data to make sure you are not introducing the nan. Also make sure all of the target values are valid. Finally, make sure the data is properly normalized. You probably want to have the pixels in the range [-1, 1] and not [0, 255].
The labels must be in the domain of the loss function, so if using a logarithmic-based loss function all labels must be non-negative (as noted by evan pu and the comments below).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment