参考链接: torch.layout
参考链接: PyTorch中张量的shape和stride的关系
原文及翻译:
torch.layout
torch.layout 章节
class torch.layout
torch.layout类型
A torch.layout is an object that represents the memory layout of a torch.Tensor.
Currently, we support torch.strided (dense Tensors) and have experimental
support for torch.sparse_coo (sparse COO Tensors).
torch.layout的一个对象,该对象代表一个torch.Tensor类型的内存布局.当前我们支持
torch.strided (dense Tensors 密集张量),并且实验性支持
torch.sparse_coo (sparse COO Tensors 稀疏 COO 张量).
torch.strided represents dense Tensors and is the memory layout that is most commonly used.
Each strided tensor has an associated torch.Storage, which holds its data. These tensors
provide multi-dimensional, strided view of a storage. Strides are a list of integers:
the k-th stride represents the jump in the memory necessary to go from one element to the
next one in the k-th dimension of the Tensor. This concept makes it possible to perform
many tensor operations efficiently.
torch.strided 表示密集张量(dense Tensors),并且是通常使用的内存布局.
每个带步长的张量都关联一个torch.Storage对象,该torch.Storage对象持有该张量的数据.这些张量提供
这个存储空间上数据的多维带步长的视图.步长是整数构成的列表.第k个步长的大小表示的是在这个张量上的
第k维度上移动一个位置对应到内存空间上所需要移动的长度.这个概念非常有利于许多张量操作执行得更高效.
Example: 例子:
>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)
>>> x.t().stride()
(1, 5)
For more information on torch.sparse_coo tensors, see torch.sparse.
关于torch.sparse_coo张量地更多信息请查阅torch.sparse的部分内容.
实验代码展示:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002396070D330>
>>>
>>> x = torch.randn(3,4,5,6,7,8)
>>> x.shape
torch.Size([3, 4, 5, 6, 7, 8])
>>> x.stride()
(6720, 1680, 336, 56, 8, 1)
>>> y = x.permute(5,3,4,1,0,2)
>>> y.shape
torch.Size([8, 6, 7, 4, 3, 5])
>>> y.stride()
(1, 56, 8, 1680, 6720, 336)
>>>
>>>