clab / clab/dynet

RuntimeError: Magnitude of gradient is bad: nan

Open
#1,456 14 comments 0 reactions 0 assignees View on GitHub
moderate bug need more info
Dominant language
C++
Stars
3.4k
Forks
701
PR merge metrics
No merged PRs in 30d

Description

Hi,

The output of my softmax contains a few zeros due to key masking. I apply a query mask after the softmax using cmult. The problem is that I get the error that magnitude of gradient is bad: nan. I have narrowed it down to be due to the cmult (if commented the training works fine).

The explanation that I have come up with is that since there are a few values in the output of softmax which are zeros, there are also a few values in the query mask which are zero and occur at corresponding positions. Thus resulting in 0*0. The code works fine if not training, but while training I get the run-time error. Does this seem an appropriate enough reason for the gradients becoming nan?

Also then what to replace the 0's in the query mask with to avoid this problem?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the training-time failure with softmax key masking, a query mask, and cmult enabled, then compare it with the working case where cmult is removed. Trace the masked softmax and cmult inputs through gradient computation; done means the cause of the NaN gradient is established and a validated masking behavior is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.