torch.Size#
Created On: Apr 19, 2024 | Last Updated On: Jun 18, 2025
torch.Size is the result type of a call totorch.Tensor.size(). It describes the size of all dimensionsof the original tensor. As a subclass oftuple, it supports common sequence operations like indexing andlength.
Example:
>>>x=torch.ones(10,20,30)>>>s=x.size()>>>storch.Size([10,20,30])>>>s[1]20>>>len(s)3
- classtorch.Size(iterable=(),/)#
- count(value,/)#
Return number of occurrences of value.
- index(value,start=0,stop=9223372036854775807,/)#
Return first index of value.
Raises ValueError if the value is not present.
- numel()→int#
Returns the number of elements a
torch.Tensorwith the given size would contain.More formally, for a tensor
x=tensor.ones(10,10)with sizes=torch.Size([10,10]),x.numel()==x.size().numel()==s.numel()==100holds true.Example:
>>>x=torch.ones(10,10)>>>s=x.size()>>>storch.Size([10, 10])>>>s.numel()100>>>x.numel()==s.numel()True
Warning
This function does not return the number of dimensions described by
torch.Size, but instead the numberof elements atorch.Tensorwith that size would contain.