PyTorch torch.Tensor.contiguous() 用法与理解
中文文档:
contiguous() → Tensor
返回一个内存连续的有相同数据的 tensor,如果原 tensor 内存连续则返回原 tensor
英文文档:
contiguous() → Tensor
Returns a contiguous tensor containing the same data as self tensor. If self tensor is contiguous, this function returns the self tensor.
常见用法:
contiguous 一般用于 transpose/permute 后和 view 前,即使用 transpose 或 permute 进行维度变换后,调用 contiguous,然后方可使用 view 对维度进行变形(如:tensor_var.contiguous().view() ),示例如下:
x = torch.Tensor(2,3)
y = x.permute(1,0) # permute:二维tensor的维度变换,此处功能相当于转置transpose
y.view(-1) # 报错,view使用前需调用contiguous()函数
y = x.permute(1,0).contiguous()
y.view(-1) # OK
两个可能原因
- view 函数只能用于 contiguous 后的 tensor 上,也就是只能用于内存中连续存储的 tensor。如果对 tensor 调用过 transpose, permute 等操作的话会使该 tensor 在内存中变得不再连续,此时就不能再调用 view 函数。因此,需要先使用 contiguous 来返回一个 contiguous copy。
- 维度变换后的变量是之前变量的浅拷贝,指向同一区域,即 view 操作会连带原来的变量一同变形,这是不合法的,所以也会报错。(这个解释有部分道理,也即 contiguous 返回了 tensor 的深拷贝 contiguous copy 数据)
参考文章
- https://zhuanlan.zhihu.com/p/64376950
- https://blog.csdn.net/qq_37385726/article/details/81738518
PyTorch torch.Tensor.contiguous() 用法与理解相关推荐
- 【Pytorch】Tensor.contiguous()使用与理解
官方文档地址:https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html?highlight=torch%20cont ...
- pytorch中的torch.tensor.repeat以及torch.tensor.expand用法
文章目录 torch.tensor.expand torch.tensor.repeat torch.tensor.expand 先看招 import torch x = torch.tensor([ ...
- pytorch torch.Tensor.numpy()(从张量创建一个numpy数组,数组和张量共享相同内存)
https://pytorch.org/docs/1.1.0/tensors.html?highlight=numpy#torch.Tensor.numpy numpy() → numpy.ndarr ...
- pytorch torch.Tensor.clone()(返回张量自身的副本。 副本具有与自身相同的大小和数据类型。)
https://pytorch.org/docs/1.1.0/tensors.html?highlight=clone#torch.Tensor.clone clone() → Tensor Retu ...
- pytorch torch.Tensor.new_ones()(返回一个与size大小相同的用1填充的张量。 默认返回的Tensor具有与此张量相同的torch.dtype和torch.device)
from https://pytorch.org/docs/1.1.0/tensors.html?highlight=new_ones#torch.Tensor.new_ones new_ones(s ...
- PyTorch | torch.tensor使用方法,如何使用torch.tensor
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得PyTorch学习教程pdf版 通过torch.tensor创建张量 arr = np.ones((3,3)) print("由np ...
- pytorch torch.Tensor.view
应用 >>> x = torch.randn(4, 4) >>> x.size() torch.Size([4, 4]) >>> y = x.vi ...
- pytorch torch.tensor
应用 torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]]) ---------------------------------------------- ...
- Pytorch——torch.Tensor.data操作的含义
1 致谢 感谢陈助教的帮助! 2 前言 今天在学习FaceBoxes~ 看到一句代码不是很懂, prior_data = priors.data 因为data是torch.Tensor类的一个成员变量 ...
最新文章
- hbase RowFilter如何根据rowkey查询以及实例实现代码
- 回归架构本真:从规划、思维到设计,构建坚不可摧的架构根基
- Python学习笔记:装饰器
- 深入理解spark两种调度模式:FIFO,FAIR模式
- mysql6支持connect by_mysql 实现oracle start with connect by递归
- 我的Android进阶之旅------Android MediaPlayer播放网络音频的实例--网络mp3播放器
- 前端学习(2621):更新品牌
- 有4个节点可以构造出 二叉树_简单4个步骤就可以拍摄出美丽的城市夜景,赶紧试试吧...
- 流量管理系统产品选型常见问答(FAQ)
- PJzhang:经典子域名爆破工具subdomainsbrute
- 内容管理项目小结 成功案例-公司官网,游戏官网
- java服务器开发(一)
- 洛谷 P1911 L国的战斗之排兵布阵
- 测试属性4 –准确性
- 以太坊数字资产的发行和流通:以太坊上的数字资产定义、ERC 20代币合约标准、ERC 20标准接口、ERC 721代币合约标准、
- 《前端框架开发技术》HTML+CSS+JavaScript 制作个人简历简单个人主页
- Python入门(第一节):Python版本如何选和安装
- ora-00972标识符过长
- WinXP开机激活死循环
- 营收、净利双增,用友网络的“云转型”迎来了“龙抬头”?