捷徑

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 的完整開發者文件

檢視文件

教學課程

取得適用於初學者和進階開發者的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得問題解答

檢視資源