pytorch中的storage指的是连续的内存块,而tensor则是映射到storage的视图,他把单条的内存区域映射成了n维的空间视图。
size是tensor的维度,storage offset是数据在storage中的索引,stride是storage中对应于tensor的相邻维度间第一个索引的跨度。示例如下:
![7120285e89549f75a8c776ece76d6d43.png](https://i-blog.csdnimg.cn/blog_migrate/4906ae9e953cc1746e2f281b41e6b74b.png)
上图是一个storage,与它对应的tensor([[3.0, 1.0, 2.0], [4.0, 1.0, F]])如下图所示:
![8e66cefce5ec57245c119b0a0faef99b.png](https://i-blog.csdnimg.cn/blog_migrate/1519cc150619d1ea275200c8339d1952.png)
那么这里的stride=(3,1),因为从第一行的第一个索引到第二行第一个索引跨度是3,从第一列到第二列的跨度是1,stride是个元组,stride[0]=3,stride[1]=1;这里的size应为torch.Size([2, 3]);根据tenser中的索引i,j查找storage中对应索引的公式是storage_offset + stride[0] * i + stride[1] * j ,因为是从storage的开头查找,所以storage_offset=0。