fierceX opened a new pull request #14673: [BUGFIX] fix ELU function will appear 
nan when calculating the gradient
URL: https://github.com/apache/incubator-mxnet/pull/14673
 
 
   ## Description ##
   fix ELU function will appear `nan` when calculating the gradient
   
   e.g:
   ```python
   elu = nn.ELU()
   elu.initialize()
   x = nd.ones((2, 3))
   x.attach_grad()
   x[0] = 100
   x[1] = -1
   with autograd.record():
       y = elu(x)
   
   y.backward()
   print(y)
   print(x.grad)
   ```
   output is 
   ```
   [[100.         100.         100.        ]
    [ -0.63212055  -0.63212055  -0.63212055]]
   <NDArray 2x3 @cpu(0)>
   
   [[       nan        nan        nan]
    [0.36787945 0.36787945 0.36787945]]
   <NDArray 2x3 @cpu(0)>
   ```
   After experiments, it was found that the `where` and the `exp` were used 
together and that this bug would occur
   
   e.g:
   
   ```python
   x = nd.ones((2, 3))
   x.attach_grad()
   x[0] = 100
   x[1] = 5
   
   with autograd.record():
       y = nd.where(x > 0, x, nd.exp(x))
   y.backward()
   print(y)
   print(x.grad)
   ```
   output:
   ```
   [[100. 100. 100.]
    [  5.   5.   5.]]
   <NDArray 2x3 @cpu(0)>
   
   [[nan nan nan]
    [ 1.  1.  1.]]
   <NDArray 2x3 @cpu(0)>
   ```
   When the `exp` calculation appears `inf`, even if `where` does not select 
the value, but the gradient will still be `nan`,so this PR does not completely 
fix the problem, but based on this, modified the ELU calculation method so that 
it does not appear `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:
[email protected]


With regards,
Apache Git Services

Reply via email to