Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 809
Peyman habibi Avatar asked Aug 05 '20 10:08

Peyman habibi


People also ask

What is tensor size?

A tensor with one dimension can be thought of as a vector, a tensor with two dimensions as a matrix and a tensor with three dimensions can be thought of as a cuboid. The number of dimensions a tensor has is called its rank and the length in each dimension describes its shape . For example, a 2 by 3 matrix: 1 2 3 4 5 6.

How do you size a tensor in PyTorch?

We access the size (or shape) of a tensor and the number of elements in the tensor as the metadata of the tensor. To access the size of a tensor, we use the . size() method and the shape of a tensor is accessed using .

What is the shape of a tensor?

A tensor is a vector or matrix of n-dimensions that represents all types of data. All values in a tensor hold identical data type with a known (or partially known) shape. The shape of the data is the dimensionality of the matrix or array. A tensor can be originated from the input data or the result of a computation.

What is shape in PyTorch?

shape gives a tuple of ints of dimensions of V. In tensorflow V. get_shape(). as_list() gives a list of integers of the dimensions of V. In pytorch, V.


1 Answers

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

like image 120
Tasnuva Avatar answered Sep 20 '22 10:09

Tasnuva