Tensor 屬性¶
每個 torch.Tensor
都有一個 torch.dtype
、torch.device
和 torch.layout
。
torch.dtype¶
- class torch.dtype¶
一個 torch.dtype
是一個物件,代表 torch.Tensor
的資料類型。 PyTorch 有十二種不同的資料類型。
資料類型 |
dtype |
舊版建構子 |
---|---|---|
32 位元浮點數 |
|
|
64 位元浮點數 |
|
|
64 位元複數 |
|
|
128 位元複數 |
|
|
16 位元浮點數 1 |
|
|
16 位元浮點數 2 |
|
|
8 位元整數 (無符號) |
|
|
8 位元整數 (有符號) |
|
|
16 位元整數 (有符號) |
|
|
32 位元整數 (有符號) |
|
|
64 位元整數 (有符號) |
|
|
布林值 |
|
|
- 1
有時稱為 binary16:使用 1 個符號位元、5 個指數位元和 10 個有效數位元。當精度很重要時很有用。
- 2
有時稱為 Brain Floating Point:使用 1 個符號位元、8 個指數位元和 7 個有效數位元。當範圍很重要時很有用,因為它具有與
float32
相同的指數位元數。
要找出 torch.dtype
是否為浮點資料類型,可以使用屬性 is_floating_point
,如果資料類型為浮點資料類型,則傳回 True
。
要找出 torch.dtype
是否為複數資料類型,可以使用屬性 is_complex
,如果資料類型為複數資料類型,則傳回 True
。
當算術運算(add、sub、div、mul)的輸入的 dtypes 不同時,我們會透過尋找滿足以下規則的最小 dtype 來提升類型:
如果純量運算元的類型高於張量運算元的類別(其中複數 > 浮點數 > 整數 > 布林值),我們會提升到具有足夠大小的類型,以容納該類別的所有純量運算元。
如果零維張量運算元的類別高於有維度運算元,我們會提升到具有足夠大小和類別的類型,以容納該類別的所有零維張量運算元。
如果沒有更高類別的零維運算元,我們會提升到具有足夠大小和類別的類型,以容納所有有維度運算元。
浮點純量運算元具有 dtype torch.get_default_dtype(),而整數非布林純量運算元具有 dtype torch.int64。 與 numpy 不同,在確定運算元的最小 dtypes 時,我們不會檢查值。 尚不支援量化和複數類型。
提升範例
>>> float_tensor = torch.ones(1, dtype=torch.float)
>>> double_tensor = torch.ones(1, dtype=torch.double)
>>> complex_float_tensor = torch.ones(1, dtype=torch.complex64)
>>> complex_double_tensor = torch.ones(1, dtype=torch.complex128)
>>> int_tensor = torch.ones(1, dtype=torch.int)
>>> long_tensor = torch.ones(1, dtype=torch.long)
>>> uint_tensor = torch.ones(1, dtype=torch.uint8)
>>> bool_tensor = torch.ones(1, dtype=torch.bool)
# zero-dim tensors
>>> long_zerodim = torch.tensor(1, dtype=torch.long)
>>> int_zerodim = torch.tensor(1, dtype=torch.int)
>>> torch.add(5, 5).dtype
torch.int64
# 5 is an int64, but does not have higher category than int_tensor so is not considered.
>>> (int_tensor + 5).dtype
torch.int32
>>> (int_tensor + long_zerodim).dtype
torch.int32
>>> (long_tensor + int_tensor).dtype
torch.int64
>>> (bool_tensor + long_tensor).dtype
torch.int64
>>> (bool_tensor + uint_tensor).dtype
torch.uint8
>>> (float_tensor + double_tensor).dtype
torch.float64
>>> (complex_float_tensor + complex_double_tensor).dtype
torch.complex128
>>> (bool_tensor + int_tensor).dtype
torch.int32
# Since long is a different kind than float, result dtype only needs to be large enough
# to hold the float.
>>> torch.add(long_tensor, float_tensor).dtype
torch.float32
- 當指定算術運算的輸出張量時,我們允許轉換為其 dtype,但以下情況除外:
整數輸出張量不能接受浮點張量。
布林輸出張量不能接受非布林張量。
非複數輸出張量不能接受複數張量
轉換範例
# allowed:
>>> float_tensor *= float_tensor
>>> float_tensor *= int_tensor
>>> float_tensor *= uint_tensor
>>> float_tensor *= bool_tensor
>>> float_tensor *= double_tensor
>>> int_tensor *= long_tensor
>>> int_tensor *= uint_tensor
>>> uint_tensor *= int_tensor
# disallowed (RuntimeError: result type can't be cast to the desired output type):
>>> int_tensor *= float_tensor
>>> bool_tensor *= int_tensor
>>> bool_tensor *= uint_tensor
>>> float_tensor *= complex_float_tensor
torch.device¶
- class torch.device¶
一個 torch.device
是一個物件,代表 torch.Tensor
所在的或將被分配到的裝置。
torch.device
包含一個裝置類型(最常見的是“cpu”或“cuda”,但也可能是 “mps”、 “xpu”、 “xla” 或 “meta”)以及裝置類型的可選裝置序號。 如果裝置序號不存在,則此物件將始終代表裝置類型的目前裝置,即使在呼叫 torch.cuda.set_device()
之後也是如此; 例如,使用裝置 'cuda'
建構的 torch.Tensor
等同於 'cuda:X'
,其中 X 是 torch.cuda.current_device()
的結果。
可以通過 Tensor.device
屬性存取 torch.Tensor
的裝置。
可以透過字串或字串與裝置序號來建構 torch.device
。
透過字串
>>> torch.device('cuda:0')
device(type='cuda', index=0)
>>> torch.device('cpu')
device(type='cpu')
>>> torch.device('mps')
device(type='mps')
>>> torch.device('cuda') # current cuda device
device(type='cuda')
透過字串與裝置序號
>>> torch.device('cuda', 0)
device(type='cuda', index=0)
>>> torch.device('mps', 0)
device(type='mps', index=0)
>>> torch.device('cpu', 0)
device(type='cpu', index=0)
裝置物件也可以用作 context manager,以變更配置 tensors 的預設裝置。
>>> with torch.device('cuda:1'):
... r = torch.randn(2, 3)
>>> r.device
device(type='cuda', index=1)
如果工廠函數傳入明確的、非 None 的裝置引數,則此 context manager 不起作用。若要全域變更預設裝置,另請參閱 torch.set_default_device()
。
警告
此函數會對每次從 Python 呼叫 torch API (不僅僅是工廠函數) 產生輕微的效能成本。 如果這對您造成問題,請在 https://github.com/pytorch/pytorch/issues/92701 上發表評論
注意
函數中的 torch.device
引數通常可以用字串代替。 這允許快速建立程式碼原型。
>>> # Example of a function that takes in a torch.device
>>> cuda1 = torch.device('cuda:1')
>>> torch.randn((2,3), device=cuda1)
>>> # You can substitute the torch.device with a string
>>> torch.randn((2,3), device='cuda:1')
注意
由於歷史原因,可以透過單個裝置序號來建構裝置,該序號被視為目前的 加速器 類型。 這與 Tensor.get_device()
相符,它會針對裝置 tensors 傳回序號,且不支援 cpu tensors。
>>> torch.device(1)
device(type='cuda', index=1)
注意
接受裝置的方法通常會接受(格式正確的)字串或(舊版的)整數裝置序號,即以下所有方法都是等效的
>>> torch.randn((2,3), device=torch.device('cuda:1'))
>>> torch.randn((2,3), device='cuda:1')
>>> torch.randn((2,3), device=1) # legacy
注意
Tensors 永遠不會在裝置之間自動移動,並且需要使用者明確呼叫。 純量 Tensors (tensor.dim()==0) 是此規則的唯一例外,它們會在需要時自動從 CPU 傳輸到 GPU,因為此操作可以「免費」完成。 範例
>>> # two scalars
>>> torch.ones(()) + torch.ones(()).cuda() # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(()).cuda() + torch.ones(()) # OK, scalar auto-transferred from CPU to GPU
>>> # one scalar (CPU), one vector (GPU)
>>> torch.ones(()) + torch.ones(1).cuda() # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(1).cuda() + torch.ones(()) # OK, scalar auto-transferred from CPU to GPU
>>> # one scalar (GPU), one vector (CPU)
>>> torch.ones(()).cuda() + torch.ones(1) # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU
>>> torch.ones(1) + torch.ones(()).cuda() # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU
torch.layout¶
- class torch.layout¶
警告
torch.layout
類別處於 beta 階段,可能會發生變更。
torch.layout
是一個物件,表示 torch.Tensor
的記憶體佈局。 目前,我們支援 torch.strided
(密集 Tensors),並且 beta 支援 torch.sparse_coo
(稀疏 COO Tensors)。
torch.strided
表示密集 Tensors,並且是最常使用的記憶體佈局。 每個 strided tensor 都有一個關聯的 torch.Storage
,它保存其資料。 這些 tensors 提供多維的 strided storage 檢視。 Strides 是整數列表:第 k 個 stride 表示從 Tensor 的第 k 個維度中的一個元素跳到下一個元素所需的記憶體跳躍。 這個概念使高效地執行許多 tensor 運算成為可能。
範例
>>> x = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)
>>> x.t().stride()
(1, 5)
有關 torch.sparse_coo
tensors 的更多資訊,請參閱 torch.sparse。
torch.memory_format¶
- class torch.memory_format¶
torch.memory_format
是一個物件,表示 torch.Tensor
是或將被分配到的記憶體格式。
可能的值為
torch.contiguous_format
: Tensor 是或將被分配到密集、非重疊的記憶體中。 Strides 由遞減順序的值表示。torch.channels_last
: Tensor 是或將被分配到密集、非重疊的記憶體中。 Strides 由strides[0] > strides[2] > strides[3] > strides[1] == 1
(又名 NHWC 順序)中的值表示。torch.channels_last_3d
: Tensor 是或將被分配到密集、非重疊的記憶體中。 Strides 由strides[0] > strides[2] > strides[3] > strides[4] > strides[1] == 1
(又名 NDHWC 順序)中的值表示。torch.preserve_format
: 用於 clone 等函數中,以保留輸入 tensor 的記憶體格式。 如果輸入 tensor 分配在密集、非重疊的記憶體中,則輸出 tensor strides 將從輸入複製。 否則,輸出 strides 將遵循torch.contiguous_format