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

Add support for expanding dimensions using None #408

Merged
merged 5 commits into from
Apr 18, 2022
Merged

Conversation

kgryte
Copy link
Contributor

@kgryte kgryte commented Mar 24, 2022

This PR

  • resolves Allowing None for indexing to add an axis #360 by adding support for expanding dimensions via None. The functional equivalent of the proposed changes is repeated invocation of expand_dims. Using None in a selection tuple is more convenient when needing to insert multiple size 1 dimensions.
  • as per comment, using None in selection tuples enjoys broad support across array libraries.

@kgryte kgryte added API change Changes to existing functions or objects in the API. topic: Indexing Array indexing. labels Mar 24, 2022
@kgryte kgryte added API extension Adds new functions or objects to the API. and removed API change Changes to existing functions or objects in the API. labels Mar 24, 2022
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding None makes sense. My main question is whether it's okay to combine it with other indexing semantics. E.g,:

>>> import torch
>>> t = torch.arange(5)
>>> t[None, :3, None]
tensor([[[0],
         [1],
         [2]]])
>>> t2 = torch.arange(6).reshape((3, 2))
>>> t2[:2, None, ...]
tensor([[[0, 1]],

        [[2, 3]]])

That probably needs testing and specifying.

@asmeurer
Copy link
Member

I think adding None makes sense. My main question is whether it's okay to combine it with other indexing semantics. E.g,:

There shouldn't be any issues with this I would imagine. The text from the NumPy docs seems fine, although it could perhaps be more precise:

Each newaxis object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension. The added dimension is the position of the newaxis object in the selection tuple. newaxis is an alias for None, and None can be used in place of this with the same result.

This should probably not apply to boolean indices where we should keep the "only index" rule.

@kgryte
Copy link
Contributor Author

kgryte commented Apr 7, 2022

I've added an explicit note that the use of None in combination with boolean array indexing is not allowed. This should address @asmeurer's comment above.

@kgryte
Copy link
Contributor Author

kgryte commented Apr 7, 2022

Re: combining with other indexing semantics. As @asmeurer mentions, this should not be an issue, given that the use of None is syntactic sugar for first extracting a selection and then applying expand_dims. Meaning, an implementation could filter a selection tuple for non-None values, compute the selection, and then apply expand_dims to the result. As such, there should be no conflict with using None in conjunction with other single-axis indexing expressions.

@asmeurer
Copy link
Member

asmeurer commented Apr 8, 2022

This looks good. Were we going to add newaxis?

@kgryte kgryte added this to the v2021 milestone Apr 18, 2022
@kgryte
Copy link
Contributor Author

kgryte commented Apr 18, 2022

As this PR has been open for 3 weeks, has received approvals here, and no objections were raised during consortium meetings, will go ahead and merge. Any changes can be addressed in follow-up pull requests.

This was referenced Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API extension Adds new functions or objects to the API. topic: Indexing Array indexing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allowing None for indexing to add an axis
3 participants