question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.take_along_axis gives wrong gradient

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mattjjcommented, Oct 17, 2019

My excuse is that I’m rusty because I’ve been on paternity leave for almost 3 weeks 😃

0reactions
mattjjcommented, Oct 17, 2019

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 against indices in our implementation of take_along_axis. Will attempt that now…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found