005.torch.dtype

每個torch.Tensor都有torch.dtype, torch.device,和torch.layout

torch.dtype

torch.dtype是表示torch.Tensor的數據類型的對象。PyTorch有八種不同的數據類型:

Data type dtype Tensor types
32-bit floating point torch.float32 or torch.float torch.*.FloatTensor
64-bit floating point torch.float64 or torch.double torch.*.DoubleTensor
16-bit floating point torch.float16 or torch.half torch.*.HalfTensor
8-bit integer (unsigned) torch.uint8 torch.*.ByteTensor
8-bit integer (signed) torch.int8 torch.*.CharTensor
16-bit integer (signed) torch.int16 or torch.short torch.*.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.*.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.*.LongTensor

使用方法:

>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> print x.type()
torch.FloatTensor

torch.layout

torch.layout表示torch.Tensor內存佈局的對象。目前,我們支持torch.strided(dense Tensors)併爲torch.sparse_coo(sparse COO Tensors)提供實驗支持。

torch.strided代表密集張量,是最常用的內存佈局。每個strided張量都會關聯 一個torch.Storage,它保存着它的數據。這些張力提供了多維度, 存儲的strided視圖。Strides是一個整數型列表:k-th stride表示在張量的第k維從一個元素跳轉到下一個元素所需的內存。這個概念使得可以有效地執行多張量。

例:

>>> 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張量的更多信息,請參閱torch.sparse

《轉:pytorch中文學習文檔

原創文章,轉載請註明 :pytorch使用torch.dtype、torch.device和torch.layout管理數據類型屬性 - pytorch中文網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章