You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/11/01 15:08:41 UTC

[GitHub] [incubator-mxnet] haidark opened a new issue #16701: Hybridize, conditional operator, and loop gradient/trainer bug

haidark opened a new issue #16701: Hybridize, conditional operator, and loop gradient/trainer bug
URL: https://github.com/apache/incubator-mxnet/issues/16701
 
 
   ## Description
   This bug occurs when training hybrid models with conditional operators and loops. The bug manifests itself as loss that becomes NaN after a few iterations probably due to some error in gradient computation. This loss behavior is not because of (machine) learning related issues, as the EXACT same code will run correctly when `model.hybridize()` is not called.
   I have created a simple model that illustrates the problem. The toy model has two dense blocks `dense1` and `dense2`. It takes some input `x` and flips a coin to decide whether to pass `x` through `dense1` or `dense2`. This is repeated for some number of iterations `iters`.
   
   What I have noticed so far:
   - Everything works when `model.hybridize()` is not called. (even when `iters > 1`
   - With  `iters==1` and `model.hybridize()`, the loss does not go to NaN
   
   ### Error Message
   N/A. Loss goes to NaN when `model.hybridize()` is called but behaves normally when it is not.
   
   ## To Reproduce
   ```
   import mxnet as mx
   import mxnet.autograd as ag
   import mxnet.gluon as gl
   
   d = 5
   # model
   class SimpleCondModel(gl.nn.HybridBlock):
       def __init__(self, **kwargs):
           super(SimpleCondModel, self).__init__(**kwargs)
   
           with self.name_scope():
               self.dense1 = gl.nn.Dense(d, in_units=d)
               self.dense2 = gl.nn.Dense(d, in_units=d)
   
       def hybrid_forward(self, F, x, *args, **kwargs):
           output = x
           iters = 4 # ~ setting this to 1 also makes the problem disappear!
           # ~ contrib.while_loop implementation
           i = F.zeros(shape=1)
           cond = lambda i, o: i < iters
           def func(i, o):
               flip = F.random.uniform(low=0.0, high=1.0, shape=1) < 0.5
               outp = F.contrib.cond(flip, lambda: self.dense1(o), lambda: self.dense2(o))
               return outp, [i+1, outp]
           loop_vars = (i, output)
           outputs, _ = F.contrib.while_loop(cond, func, loop_vars, max_iterations=iters)
           output = outputs.slice_axis(axis=0, begin=-1, end=None)
           # ~ regular for loop implementation
           # for _ in range(iters):
           #     flip = F.random.uniform(low=0.0, high=1.0, shape=1) < 0.5
           #     output = F.contrib.cond(flip, lambda: self.dense1(x), lambda: self.dense2(x))
           #     x = output
           return output.squeeze()
   bS = 1
   model = SimpleCondModel()
   model.initialize()
   model.hybridize()  # ~ comment this line to make the problem disappear!
   
   loss = gl.loss.L2Loss()
   trainer = gl.Trainer(model.collect_params(), 'sgd', {'learning_rate': 0.001})
   
   # make random data and labels and train for a bit, I verified this is normal without hybridize
   for _ in range(10):
       data = mx.nd.random_normal(3, 1, shape=(bS, d))
       labels = mx.nd.random_normal(10, 1, shape=(bS, d))
       with ag.record():
           out = model(data)
           l = loss(out, labels)
       l.backward()
       trainer.step(bS, ignore_stale_grad=True)
       print(l.mean().asscalar())
   ```
   
   ### Steps to reproduce
   python "the script above"
   
   ## What have you tried to solve it?
   Symbolic loops
   
   ## Environment
   mxnet version: '1.5.0' (I need to use this version)
   
   We recommend using our script for collecting the diagnositc information. Run the following command and paste the outputs below:
   ```
   (py37) 38f9d3cc3799:scratches khhaida$ curl --retry 10 -s https://raw.githubusercontent.com/dmlc/gluon-nlp/master/tools/diagnose.py | python reproduce.py
   53.559364
   4.1593175e+18
   nan
   nan
   nan
   nan
   nan
   nan
   nan
   nan
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services