Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numpy.mgrid #623

Open
99991 opened this issue Jul 25, 2024 · 8 comments
Open

Support numpy.mgrid #623

99991 opened this issue Jul 25, 2024 · 8 comments
Labels
PR welcome Contributions towards resolving this issue are welcome
Milestone

Comments

@99991
Copy link

99991 commented Jul 25, 2024

>>> import autograd.numpy as np
>>> np.mgrid[:3, :4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'autograd.numpy' has no attribute 'mgrid'

Support for numpy.mgrid would be nice.

@agriyakhetarpal agriyakhetarpal added the PR welcome Contributions towards resolving this issue are welcome label Aug 23, 2024
@Karthik-Dulam
Copy link

@agriyakhetarpal

I'm interested in contributing if someone hasn't already started working on it.
However since I am not familiar with the project (I'm actively trying to understand how it works) I might take some time.
If that is okay, please point me towards resources or files in the project that would be helpful.

As I understand (correct me if I am wrong), most of the work should lie in figuring out whether mgrid should be a primitive and if it is write a vjp for it, otherwise copy the numpy implementation.

@fjosw
Copy link
Collaborator

fjosw commented Sep 8, 2024

Hey @99991 and @Karthik-Dulam, autograd actually already supports numpy.msort, but the method was removed with numpy 2, see the release notes:

np.msort has been removed. For a replacement, np.sort(a, axis=0) should be used instead.

The error you reported only occured because autograd was not compatible with numpy 2. In the meantime, we released autograd 1.7 which fully supports numpy 2 and the AttributeError should not occur anymore. Please reopen the issue in case you still experience problems with the new version.

@fjosw fjosw closed this as completed Sep 8, 2024
@99991
Copy link
Author

99991 commented Sep 8, 2024

@fjosw I do not have permission to reopen this issue. Could you reopen?

This issue is about np.mgrid, not np.msort. The issue still occurs with the latest versions of NumPy and autograd on PyPI:

$ pip freeze | grep -P 'autograd|numpy'
autograd==1.7.0
numpy==2.1.1
$ python3
Python 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import autograd.numpy as np
>>> np.mgrid[:3, :4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'autograd.numpy' has no attribute 'mgrid'

NumPy 2 still supports np.mgrid:

>>> import numpy
>>> numpy.mgrid[:3, :4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])

@fjosw fjosw reopened this Sep 8, 2024
@fjosw
Copy link
Collaborator

fjosw commented Sep 8, 2024

Oh, I'm sorry for the confusion. i reopened the issue.

@99991
Copy link
Author

99991 commented Sep 8, 2024

No worries. Thanks for reopening.

@fjosw
Copy link
Collaborator

fjosw commented Sep 8, 2024

One more question: Do you really need np.mgrid to be differentiable? At first glance I would not know how to define the derivative. Would it work for your use-case to create an array via

import numpy as onp
onp.mgrid[:3, :4]

and then use this array as input for function you would like to derive with autograd.numpy?

@99991
Copy link
Author

99991 commented Sep 8, 2024

Personally, I do not need the derivative. I just do not want to change my code when switching between numpy and autograd.numpy.

I do not know whether there are applications where a derivative with mgrid is possible or useful.

@fjosw
Copy link
Collaborator

fjosw commented Sep 8, 2024

Thanks for clarifying. I create a draft PR for adding a few index tricks to autograd.numpy (#639).

@agriyakhetarpal agriyakhetarpal added this to the v1.8.0 milestone Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR welcome Contributions towards resolving this issue are welcome
Projects
None yet
Development

No branches or pull requests

4 participants