-
Notifications
You must be signed in to change notification settings - Fork 53
Make axis keyword to squeeze() positional #100
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
Conversation
As suggested by @shoyer in data-apis#97 (comment) This makes it possible to predict resulting rank of output array, which is otherwise undetermined (see discussion in data-apisgh-97). Using squeeze without specifying the axis in library code often results in unintuitive behaviour. For the common use case of extracting a scalar from a size-1 2-D array, this gets a little more verbose (e.g. `np.squeeze(np.array([[1]]), axis=(0, 1))`), but that's probably a price worth paying for the extra clarity. Also changes specified behaviour for a given axis not having size 1 to raising a `ValueError`, which is what NumPy does. This wasn't considered before, and the current description seems simply incorrect. Finally, this makes `squeeze` the exact inverse of `expand_dims`, which is probably a good thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me, thanks!
I did a bit of searching, NumPy has done this for a very long time, the The PyTorch docs have a warning: "If the tensor has a batch dimension of size 1, then squeeze(input) will also remove the batch dimension, which can lead to unexpected errors." So there's two things to decide:
The PR in its current state (which answers with "No, Raise") makes There's not much data on (2) - I haven't seen any complaints from numpy users, but that's because they mostly use the default |
0607525
to
138e963
Compare
Given the divergence in semantics, would it make sense to do a bit of innovating here and actually split the use cases as separate APIs? |
I don't think we need "clean up size-1 dimensions if they exist" in the API standard. Squeezing without an axis really only makes sense in an interactive context, but our primary audience here is library developers. |
I agree. Although the proof will be in the pudding. There's quite a bit of How about we merge this as is, and then see how it works in practice? We can always add something later, but once we put a function in we can't remove it anymore. |
Agreed about both!
Sounds good to me. To be honest, it is somewhat questionable whether we need squeeze() in either form. The canonical implementation of either version is ~5 lines of code. The most annoying part is probably formatting the error message :) |
Agreed, I'd say it's borderline. The symmetry with Okay, this has been open for a long time, let's push the button on it:) |
…l-only See data-apis/array-api#100. Original NumPy Commit: 1ae808401951bf8c4cbff97a30505f08741d811f
…l-only See data-apis/array-api#100. Original NumPy Commit: 1ae808401951bf8c4cbff97a30505f08741d811f
As suggested by @shoyer in #97 (comment). This makes it possible to predict resulting rank of output array, which is otherwise undetermined (see discussion in gh-97).
Using squeeze without specifying the axis in library code often results in unintuitive behaviour. For the common use case of turning a size-1 2-D array into a 0-D, this gets a little more verbose (e.g.
np.squeeze(np.array([[1]]), axis=(0, 1))
), but that's probably a price worth paying for the extra clarity.Also changes specified behaviour for a given axis not having size 1 to raising a
ValueError
, which is what NumPy does. This wasn't considered before, and the current description seems simply incorrect.Finally, this makes
squeeze
the exact inverse ofexpand_dims
, which is probably a good thing.The caveats:
squeeze
(e.g. in SciPy) do not use theaxis
keyword. The counter-argument for that is that many of those instances are hard to understand and have no comment for why squeeze is used.