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

.size() vs .shape, which one should be used? #5544

Closed
freud14 opened this issue Mar 3, 2018 · 13 comments
Closed

.size() vs .shape, which one should be used? #5544

freud14 opened this issue Mar 3, 2018 · 13 comments

Comments

@freud14
Copy link

freud14 commented Mar 3, 2018

Hi,

This is not an issue per se, but more of a question of best practices. I notice that there a shape attribute and a size function that both return the size/shape of a tensor. I also notice that .shape is not documented, but according to various release notes, Pytorch tries to be as close as possible to numpy. So, knowing this, is .shape really supported or just an undocumented functionality that could disappear in the next version?

Thank you.

@fmassa
Copy link
Member

fmassa commented Mar 3, 2018

Hi,
.shape is an alias for .size(), and was added to more closely match numpy, see #1983

@apaszke
Copy link
Contributor

apaszke commented Mar 3, 2018

Yes, .shape is going to remain supported. Both ways are fine

@apaszke apaszke closed this as completed Mar 3, 2018
@freud14
Copy link
Author

freud14 commented Mar 3, 2018

Thank you.

@samuela
Copy link
Contributor

samuela commented Sep 4, 2018

FWIW this redundancy has always bugged me. For some reason x.shape feels more internal than x.size() but using x.size() also just doesn't look right when doing things like x.size()[-2:]. Still I don't want to be inconsistent and use both.

@nyck33
Copy link

nyck33 commented May 18, 2019

what about .size with no ( ) following it like here:
image
Found here: http://nlp.seas.harvard.edu/2018/04/03/attention.html

@munael
Copy link

munael commented Oct 29, 2019

what about .size with no ( ) following it like here:
image
Found here: http://nlp.seas.harvard.edu/2018/04/03/attention.html

That size attribute is custom to the custom layers, from what I can see. tensor.size() is a tensor method.

@Risingabhi
Copy link

.size() method returns total elements in a dataframe , for eg shape of a tensor might be (10,3) , here total elements in tensor would be returned by .size() = 10X3 = 30 elements!!

@freud14
Copy link
Author

freud14 commented Nov 18, 2020

.size() method returns total elements in a dataframe , for eg shape of a tensor might be (10,3) , here total elements in tensor would be returned by .size() = 10X3 = 30 elements!!

@Risingabhi Nope, that's not how it works in PyTorch:
image

@Risingabhi
Copy link

.size() method returns total elements in a dataframe , for eg shape of a tensor might be (10,3) , here total elements in tensor would be returned by .size() = 10X3 = 30 elements!!

@Risingabhi Nope, that's not how it works in PyTorch:
image

yes, that's the case in pytorch

@flpgrz
Copy link

flpgrz commented Feb 26, 2021

.size() method returns total elements in a dataframe

That's what torch.numel(x) does.

@davidgilbertson
Copy link

If you're like me and looking to pick one or the other, some common use cases compared:

t = torch.arange(60).view(3, 4, 5)

t.shape == t.size()          # just get the size/shape
t.shape[1] == t.size(1)      # get one element from the size/shape
t.shape[1:] == t.size()[1:]  # get several elements from the size/shape

To my eyes, shape is the better all-rounder. It returns a different type of object to NumPy (Size) which is a bit odd, but Size extends tuple so I can't think of a scenario where the behaviour would be any different.

@Vagish-Kumar
Copy link

Hi, .shape is an alias for .size(), and was added to more closely match NumPy, see #1983

@tornikeo
Copy link

Is there any difference in behavior for torch.compile or onnx export? I'm pretty sure I've seen some weird stuff happen with .shape, while .size seemed to work well @apaszke

isdanni added a commit to isdanni/pytorch that referenced this issue Jul 6, 2023
Summary:
Add `Tensor.Shape` doc.

Ref:

- pytorch#5544
- pytorch#1980

Differential Revision: D47278630

fbshipit-source-id: 823ee4776a8ef1086c3b37c3d1af3b5bb9355a1c
isdanni added a commit to isdanni/pytorch that referenced this issue Jul 7, 2023
Summary:
Pull Request resolved: pytorch#104750

Add `Tensor.Shape` doc.

Ref:

- pytorch#5544
- pytorch#1980

Differential Revision: D47278630

fbshipit-source-id: b549a68eb632f46db921fd5ae89ad58767a880a4
isdanni added a commit to isdanni/pytorch that referenced this issue Jul 25, 2023
Summary:
Pull Request resolved: pytorch#104750

Add `Tensor.Shape` doc.

Ref:

- pytorch#5544
- pytorch#1980

Differential Revision: D47278630

fbshipit-source-id: aa2466cba60b21860338e1ea6b8850f4395b6255
pytorchmergebot pushed a commit that referenced this issue Jul 26, 2023
Summary:
Add `Tensor.Shape` doc.

Fix: #104038

Ref:

- #5544
- #1980

Differential Revision: D47278630

CC: @svekars @carljparker

Pull Request resolved: #104750
Approved by: https://github.com/mikaylagawarecki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests