'What is the difference between Tensor.size and Tensor.shape in PyTorch?
What is the difference between Tensor.size and Tensor.shape in Pytorch?
I want to get the number of elements and the dimensions of Tensor. For example for a tensor with the dimensions of 2 by 3 by 4 I expect 24 for number of elements and (2,3,4) for dimension.
Thanks.
Solution 1:[1]
.shape is an alias for .size(), and was added to more closely match numpy, see this discussion here.
Solution 2:[2]
.shape is an attribute of the tensor whereas size() is a function. They both return the same value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mughees |
| Solution 2 | former_Epsilon |
