栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

pytorch - storage(), stride()

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch - storage(), stride()

1 storage()

pytorch 中的 一个tensor分为头信息区(Tensor)和存储区(Storage)

        头信息区保存着tensor的形状size,步长stride,数据类型type等信息;

        真正的数据保存成连续的数组,存储在存储区;

存在多个tensor都对应着相同一个存储区的情况,只是这几个tensor头信息区不同;

>>> import torch
>>> a = torch.arange(6)
>>> a
tensor([0, 1, 2, 3, 4, 5])
>>> a.storage()
 0
 1
 2
 3
 4
 5
[torch.LongStorage of size 6]
>>> b = a.view(2,3)
>>> b
tensor([[0, 1, 2],
        [3, 4, 5]])
>>> b.storage()
 0
 1
 2
 3
 4
 5
[torch.LongStorage of size 6]

a、b tensor的Storage都是一样的,而a、b  tensor只是因为它们的头信息区不同;

        一旦修改其中一个tensor的值。其它tensor也会跟着改变;

>>> a[3] = 10
>>> a
tensor([ 0,  1,  2, 10,  4,  5])
>>> b
tensor([[ 0,  1,  2],
        [10,  4,  5]])
2 stride()

        stride是在指定维度中从一个元素跳到下一个元素所必需的步长;

        当没有参数传入时,返回所有步长的元组;

        否则,将返回一个整数值作为特定维度dim中的步长;

>>> b = torch.tensor([[0, 1, 2],[3,10,2],[10,2,1]])
>>> b
tensor([[ 0,  1,  2],
        [ 3, 10,  2],
        [10,  2,  1]])
>>> b.stride()
(3, 1)
>>> b = torch.tensor([[0, 1, 2,4],[3,10,2,3],[10,2,1,1]])
>>> b.stride()
(4, 1)
>>> b.stride(0)
4
>>> b.stride(1)
1

        上面的4指的是第0个维度中的一个元素[0, 1, 2, 4]到下一个元素[3, 10, 2, 3]所需要的步长为4;

        也可以理解从第一个的第一个索引到下一个元素第一个索引跨度是4;

        1指的是第1个维度[0, 1, 2, 4]中的一个元素0到下一个元素1所需要的步长为1;

参考:pytorch笔记(一)——tensor的storage()、stride()、storage_offset()_Zoran_卓的博客-CSDN博客_tensor的stride

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037791.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号