快捷方式

torch.Size

torch.Size 是呼叫 torch.Tensor.size() 的結果型別。它描述了原始張量的所有維度的大小。作為 tuple 的子類,它支援索引和長度等常見序列操作。

示例

>>> x = torch.ones(10, 20, 30)
>>> s = x.size()
>>> s
torch.Size([10, 20, 30])
>>> s[1]
20
>>> len(s)
3
class torch.Size(iterable=(), /)
count(value, /)

返回值的出現次數。

index(value, start=0, stop=9223372036854775807, /)

返回值的第一個索引。

如果值不存在,則引發 ValueError。

numel() int

返回具有給定大小的 torch.Tensor 將包含的元素數量。

更正式地說,對於大小為 s = torch.Size([10, 10]) 的張量 x = tensor.ones(10, 10),有 x.numel() == x.size().numel() == s.numel() == 100 成立。

示例:
>>> x=torch.ones(10, 10)
>>> s=x.size()
>>> s
torch.Size([10, 10])
>>> s.numel()
100
>>> x.numel() == s.numel()
True

警告

此函式不返回 torch.Size 所描述的維度數量,而是返回具有該大小的 torch.Tensor 將包含的元素數量。

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取適合初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源