mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Example of debugging gradient of op in HybridBlock

Open altosaar opened this issue 9 years ago • 3 comments

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?

  1. Export the HybridBlock to json using: https://mxnet.incubator.apache.org/api/python/gluon/gluon.html?highlight=export#mxnet.gluon.HybridBlock.export

  2. 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!

altosaar avatar Nov 25 '17 17:11 altosaar

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()))

piiswrong avatar Nov 26 '17 00:11 piiswrong

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)

altosaar avatar Nov 26 '17 14:11 altosaar

proposed labels: "Feature request", "Bug", "Exception Handling", "Question"

zhiz21 avatar Mar 10 '18 00:03 zhiz21