Example of debugging gradient of op in HybridBlock
I have a custom LSTMCell and I get nan gradients after a certain number of gradient updates. I used pdb to step through the forward pass and check for nans. Tensorflow has an operation that adds nan checks to all ops in the graph, which would be nice here.
How can I debug to find out which op is causing the nans?
After googling, here is an approach. Is this the recommended approach? Or is there a simpler way?
-
Export the HybridBlock to json using: https://mxnet.incubator.apache.org/api/python/gluon/gluon.html?highlight=export#mxnet.gluon.HybridBlock.export
-
Add a monitor to a symbol executor, which will print gradients of all ops: https://github.com/apache/incubator-mxnet/blob/master/example/python-howto/monitor_weights.py
However, I'm not sure if hybird recurrent architectures can be exported.
Thanks in advance!
automatic nan check is a good feature we can add.
For now you can use
for p in net.collect_params():
for g in p.list_grads():
assert not np.any(np.isnan(g.asnumpy()))
Thanks @piiswrong -- I've done that to see which parameters have nan gradients, but this doesn't tell me which op is leading to the nans.
Is it best to do binary search by commenting out parts of the computational graph to find the nans then? Or is there another way (e.g. using a monitor somehow to see the gradients in every node of the graph)
proposed labels: "Feature request", "Bug", "Exception Handling", "Question"