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 2017/12/21 23:38:08 UTC
[GitHub] sxjscience opened a new issue #9172: Wrong gradient of gather_nd when the indices have duplicates
sxjscience opened a new issue #9172: Wrong gradient of gather_nd when the indices have duplicates
URL: https://github.com/apache/incubator-mxnet/issues/9172
This issue is borrowed from https://discuss.gluon.ai/t/topic/3389.
I find the cause is a bug in the gradient computation of `gather_nd `. Currently, the gradient of `gather_nd ` is `scatter_nd`, which has not considered the case that the indices may be the same
https://github.com/apache/incubator-mxnet/blob/master/src/operator/tensor/indexing_op.h#L1156 . The correct way to implement it is to use the same backward logic as `take`.
```python
import mxnet as mx
import mxnet.ndarray as nd
import numpy as np
data = mx.nd.array([[0, 1], [2, 3]])
indices = mx.nd.array([[1, 1, 0], [1, 1, 0]])
data.attach_grad()
with mx.autograd.record():
ret = mx.nd.gather_nd(data, indices)
loss = mx.nd.sum(ret)
loss.backward()
print(data.grad)
```
```
[[ 1. 0.]
[ 0. 1.]]
<NDArray 2x2 @cpu(0)>
```
The correct result should be
```
[[ 1. 0.]
[ 0. 2.]]
<NDArray 2x2 @cpu(0)>
```
@piiswrong @szha
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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