当前位置:网站首页>Pytorch中torch.repeat()函数解析
Pytorch中torch.repeat()函数解析
2022-07-15 17:31:00 【cv_lhp】
一. torch.repeat()函数解析
1. 说明
官网:torch.tensor.repeat(),函数说明如下图所示:

2. 函数功能
torch.tensor.repeat()函数可以对张量进行重复扩充
1) 当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。
2) 当参数有三个时:(通道数的重复倍数,行的重复倍数,列的重复倍数),1表示不重复。
3. 代码例子如下:
3.1 输入一维张量,参数为一个,即表示在列上面进行重复n次
a = torch.randn(3)
a,a.repeat(4)
结果如下所示:
(tensor([ 0.81, -0.57, 0.10]),
tensor([ 0.81, -0.57, 0.10, 0.81, -0.57, 0.10, 0.81, -0.57, 0.10, 0.81,
-0.57, 0.10]))
3.2 输入一维张量,参数为两个(m,n),即表示先在列上面进行重复n次,再在行上面重复m次,输出张量为二维
a = torch.randn(3)
a,a.repeat(4,2)
(tensor([ 0.06, -0.76, -0.59]),
tensor([[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59]]))
3.3 输入一维张量,参数为三个(b,m,n),即表示先在列上面进行重复n次,再在行上面重复m次,最后在通道上面重复b次,输出张量为三维
a = torch.randn(3)
a,a.repeat(3,4,2)
输出结果如下:
(tensor([2.25, 0.49, 1.47]),
tensor([[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]],
[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]],
[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]]]))
3.4 输入二维张量,参数为两个(m,n),即表示先在列上面进行重复n次,再在行上面重复m次,输出张量为两维(注意参数个数必须大于输入张量维度个数)
a = torch.randn(3,2)
a,a.repeat(4,2)
输出结果如下:
(tensor([[-0.58, -1.21],
[-0.35, 0.68],
[ 0.33, 0.70]]),
tensor([[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70]]))
3.5 输入二维张量,参数为三个(b,m,n),即表示先在列上面进行重复n次,再在行上面重复m次,最后在通道上面重复b次,输出张量为三维。(注意输出张量维度个数为参数个数)
a = torch.randn(3,2)
a,a.repeat(3,4,2)
输出结果如下:
(tensor([[-0.75, 1.20],
[-1.50, 1.75],
[-0.05, 0.40]]),
tensor([[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]],
[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]],
[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]]]))
边栏推荐
- 云上数字创新,如何塑造一座城市的幸福感?
- Unit MySQL appears in MySQL Solution of service could not be found
- Pytorch中torch.nonzero()函数解析
- How to make Sitemaps website map
- Eight guidelines for modbus-rs485 wiring
- AES encryption learning of openharmony security module
- 2022中国移动创客马拉松大赛物联网专题赛开赛啦
- Things about the client (2)
- Simple understanding of CAS and AQS
- Fosai biology interprets the changes in the atmospheric environment in the first half of 2022, and VOCs control is still the key to breaking the situation
猜你喜欢

分库分表真的适合你的系统吗?聊聊分库分表和NewSQL如何选择

Pytorch中的广播机制(Broadcast)

抽丝剥茧C语言(高阶)静态通讯录

Pytorch中torch.repeat_interleave()函数解析

局域网攻击与网络设备安全配置

Record the use of yolov5 (1)

Intel releases open source AI Reference Suite

AES encryption learning of openharmony security module

实现浏览器 - Servlet - 数据库交互操作

Illegal profits exceed one million, and new outlets in the industry are being cracked and eroded
随机推荐
Pytorch中torch.numel(),torch.shape,torch.size()和torch.reshape()函数解析
概率沉思录:1.Plausible reasoning
[basic use of oscilloscope] and [introduction to the meaning of each key on the oscilloscope key panel]
Illegal profits exceed one million, and new outlets in the industry are being cracked and eroded
OpenHarmony模块二初分析(2)
DevSecOps研发安全实践——设计篇
Markdown in CSDN sets the width of table columns
(open source project) abattoir unity game
LINQ implements query string splicing: and and or
Using chardet to detect web page coding
如何制作sitemaps网站地图
根据经纬度计算两点之间的距离
In mysql, the decimal (10,2) format is written to Kafka through stream and becomes stri
C language (high level) static address book
记录Yolov5的使用(1)
来自JRockit的礼物:JMC虚拟机诊断工具
Procédure d'essai de pénétration
ASP.NET里的Session详细解释
局域网攻击与网络设备安全配置
The function of ifndef /define/endif in the header file