np.take_along_axis gives wrong gradient
See original GitHub issueThe following repro script shows the issue:
import jax.numpy as np
from jax import value_and_grad
idx = np.repeat(np.arange(3), 10).reshape((30, 1))
def f(x):
y = x * np.arange(3.).reshape((1, 3))
return np.take_along_axis(y, idx, -1).sum()
def g(x):
y = x * np.arange(3.).reshape((1, 3))
y = np.broadcast_to(y, (30, 3))
return np.take_along_axis(y, idx, -1).sum()
print(value_and_grad(f)(1.)) # get 30, 0
print(value_and_grad(g)(1.)) # get 30, 30
I think (30, 30)
is the correct answer.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Weird behavior of np.gradient with increased resolution
It's because of the default spacing assumed between two consecutive values, which is 1. See this answer for details.
Read more >numpy.gradient — NumPy v1.24 Manual
Return the gradient of an N-dimensional array. The gradient is computed using second order accurate central differences in the interior points and either ......
Read more >np.gradient() — A Simple Illustrated Guide - Finxter
gradient () function approximates the gradient of an N-dimensional array. It uses the second-order accurate central differences in the interior points and either ......
Read more >2.6. Image manipulation and processing using Numpy and Scipy
In particular, the submodule scipy.ndimage provides functions operating on ... Here, image == Numpy array np.array ... array([[False, True, False],.
Read more >Numpy Gradient Examples using numpy.gradient() method.
... mathematical calculations on arrays. Know how to calculate numpy gradient with examples. ... numpy_array = np.array([1, 2, 4, 7, 11, 16], dtype=float)....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My excuse is that I’m rusty because I’ve been on paternity leave for almost 3 weeks 😃
Hah, okay, I read @fehiepsi’s OP again and I realized he basically figured out the issue for us. I was just too sloppy to read it!
I think we just need to broadcast
arr
againstindices
in our implementation oftake_along_axis
. Will attempt that now…