PyTorch中关于tensor.repeat()的使用
作者:tomeasure 发布时间:2023-06-26 07:13:35
标签:PyTorch,tensor,repeat
关于tensor.repeat()的使用
考虑到很多人在学习这个函数,我想在这里提 一个建议:
强烈推荐 使用 einops 模块中的 repeat() 函数 替代 tensor.repeat()!
它可以摆脱 tensor.repeat() 参数的神秘主义。
einops 模块文档地址:https://nbviewer.jupyter.org/github/arogozhnikov/einops/blob/master/docs/1-einops-basics.ipynb
学习 tensor.repeat() 这个函数的功能的时候,最好还是要观察所得到的 结果的维度。
不多说,看代码:
>>> import torch
>>>
>>> # 定义一个 33x55 张量
>>> a = torch.randn(33, 55)
>>> a.size()
torch.Size([33, 55])
>>>
>>> # 下面开始尝试 repeat 函数在不同参数情况下的效果
>>> a.repeat(1,1).size() # 原始值:torch.Size([33, 55])
torch.Size([33, 55])
>>>
>>> a.repeat(2,1).size() # 原始值:torch.Size([33, 55])
torch.Size([66, 55])
>>>
>>> a.repeat(1,2).size() # 原始值:torch.Size([33, 55])
torch.Size([33, 110])
>>>
>>> a.repeat(1,1,1).size() # 原始值:torch.Size([33, 55])
torch.Size([1, 33, 55])
>>>
>>> a.repeat(2,1,1).size() # 原始值:torch.Size([33, 55])
torch.Size([2, 33, 55])
>>>
>>> a.repeat(1,2,1).size() # 原始值:torch.Size([33, 55])
torch.Size([1, 66, 55])
>>>
>>> a.repeat(1,1,2).size() # 原始值:torch.Size([33, 55])
torch.Size([1, 33, 110])
>>>
>>> a.repeat(1,1,1,1).size() # 原始值:torch.Size([33, 55])
torch.Size([1, 1, 33, 55])
>>>
>>> # ------------------ 割割 ------------------
>>> # repeat()的参数的个数,不能少于 * 作的张量的维度的个数,
>>> # 下面是一些错误示例
>>> a.repeat(2).size() # 1D < 2D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> # 定义一个3维的张量,然后展示前面提到的那个错误
>>> b = torch.randn(5,6,7)
>>> b.size() # 3D
torch.Size([5, 6, 7])
>>>
>>> b.repeat(2).size() # 1D < 3D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> b.repeat(2,1).size() # 2D < 3D, error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
>>>
>>> b.repeat(2,1,1).size() # 3D = 3D, okay
torch.Size([10, 6, 7])
>>>
Tensor.repeat()的简单用法
相当于手动实现广播机制,即沿着给定的维度对tensor进行重复:
比如说对下面x的第1个通道复制三次,其余通道保持不变:
import torch
x = torch.randn(1, 3, 224, 224)
y = x.repeat(3, 1, 1, 1)
print(x.shape)
print(y.shape)
结果为:
torch.Size([1, 3, 224, 224])
torch.Size([3, 3, 224, 224])
这个在复制batch的时候用的比较多,上面的情况就相当于batch为1的3×224×224特征图复制成了batch为3
来源:https://blog.csdn.net/qq_29695701/article/details/89763168
0
投稿
猜你喜欢
- Windows Server 2003系统是现在很流行的服务器操作系统,许多网站都用它来做。但是如何保证服务器的相对安全,这个只要进行一些简
- 一般一个网站的首页访问量是最大的,如果您的网站的首页打开的非常缓慢,您的客户将会陆续离开你的网站.通常我们把需要经过复杂运算或者查询数据库得
- 我是这样来做DIV布局代码的.不知道说的清楚不清楚,凑和看吧我把class分为2种,布局class,风格class,布局class是骨架,风
- js也是可以做出狂炫的图形的,恭请超级高手分析。给大家看个例子吧。http://www.p01.org/releases/DHTML_con
- 要做好一个HTML邮件,说简单,还真不怎么复杂,说它复杂,其实也不难。-_-!!以前写过类似的[ 关于HTML邮件的总结 ],最近又犯愁,就
- 一、事件捕捉(Event Capture)的实现问题首先在说这件事前,先感谢一下Realazy。 W3C DOM Level2的事
- 假设现有一张人员表(表名:Person),若想将姓名、身份证号、住址这三个字段完全相同的记录查找出来,使用1: SELECT p
- 当你准备全面进军web标准时,有时候你是不是被表格的弄得焦头烂额呢?比如,原来使用“非法”的nobr现在要用什么来代替呢?今天,就让我来一个
- 1、终极方法:条件注释<!--[if lte IE 6]> 这段文字仅显示在 IE6及IE6以下版本。 <![endif]
- 网络上关于各种语言和应用软件的速查手册和快速参考指南有很多很多,不幸的是当我们需要的时候,总是很难找到,所以我决定花点时间尽可能的收集更多的
- Go语言的二进制(gob)格式是一个自描述的二进制序列。从其内部表示来看,Go语言的二进制格式由一个 0 块或者更多块的序列组成,其中的每一
- 在mysql安装目录的bin下, 运行mysql --install MYSQL5查看服务中, 会多一个MYSQL5的服务XAMPP的控制面
- COM接口VC实现,接口: [id(1), helpstring("method Test"
- 引言最常见的闭包 (Closure) 范式大家都很熟悉了:(function() {// ...})(); 很
- 众所周知IE6不支持position:fixed,这个bug与IE6的双倍margin和不支持PNG透明等bug一样臭名昭著。前些天我做自己
- 今天同事 明城 在项目中碰到一个 BUG,代码具体如下:<!DOCTYPE html PUBLIC "-//W3C//DTD
- <% Response.Buffer = True Server.ScriptTimeOut=9999999&
- Asp(Active Server Pages)是Web服务器端脚本编写环境,可以使用Vbscript/Jscript两种脚本来编写.作为我
- 一、 在数据库排序查询优化上的差异。在讲解这个内容之前,为了读者能够清楚我讲的内容,我要先谈一个概念。命中率,它是指从内存中取得数据而不从磁
- Web,全称为 World Wide Web,是 Internet 上最重要和最为人们所熟知的应用之一。Web 是指 Internet 上所