What is the difference between Tensor.size and Tensor.shape in PyTorch?

Viewed 8706

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.

2 Answers

.shape is an alias for .size(), and was added to more closely match numpy, see this discussion here.

.shape is an attribute of the tensor whereas size() is a function. They both return the same value.

Related