当前位置:网站首页>PyTorch中repeat、tile与repeat_interleave的区别
PyTorch中repeat、tile与repeat_interleave的区别
2022-07-02 09:41:00 【raelum】
torch.Tensor.repeat
repeat 可以形象地理解为将已有的张量多次重复以组成 “分块矩阵”。
import torch
""" Example 1 """
t = torch.arange(3)
print(t.repeat((2, )))
# tensor([0, 1, 2, 0, 1, 2])
print(t.repeat((2, 2)))
# tensor([[0, 1, 2, 0, 1, 2],
# [0, 1, 2, 0, 1, 2]])
""" Example 2 """
t = torch.arange(4).reshape(2, 2)
print(t.repeat((2, )))
# RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
print(t.repeat((2, 2)))
# tensor([[0, 1, 0, 1],
# [2, 3, 2, 3],
# [0, 1, 0, 1],
# [2, 3, 2, 3]])
print(t.repeat((2, 3, 4)))
# tensor([[[0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3]],
#
# [[0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3]]])
可以看出要 repeat 的维度不能低于张量本身的维度。
torch.Tensor.tile
大部分情况下,tile 与 repeat 的作用相同,如下:
""" Example 1 """
t = torch.arange(3)
print(t.tile((2, )))
# tensor([0, 1, 2, 0, 1, 2])
print(t.tile((2, 2)))
# tensor([[0, 1, 2, 0, 1, 2],
# [0, 1, 2, 0, 1, 2]])
""" Example 2 """
t = torch.arange(4).reshape(2, 2)
print(t.tile((2, )))
# tensor([[0, 1, 0, 1],
# [2, 3, 2, 3]])
print(t.tile((2, 2)))
# tensor([[0, 1, 0, 1],
# [2, 3, 2, 3],
# [0, 1, 0, 1],
# [2, 3, 2, 3]])
print(t.tile((2, 3, 4)))
# tensor([[[0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3]],
#
# [[0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3],
# [0, 1, 0, 1, 0, 1, 0, 1],
# [2, 3, 2, 3, 2, 3, 2, 3]]])
与 repeat 不同的是,当要重复的维度低于张量的维度时,tile 也能够处理,此时 tile 会使用前置 1 1 1 自动补齐维度。
torch.Tensor.repeat_interleave
之前提到的 repeat 和 tile 都是重复整个张量,而这次的 repeat_interleave 则是重复张量中的元素。
参数如下:
torch.Tensor.repeat_interleave(repeats, dim=None)
repeats:代表张量中每个元素将要重复的次数。可以为整数或张量;dim:决定了沿哪一个轴去重复数字。默认情况下会将输入展平再进行重复,最后输出展平的张量。
""" Example 1 """
t = torch.arange(3)
print(t.repeat_interleave(repeats=3))
# tensor([0, 0, 0, 1, 1, 1, 2, 2, 2])
""" Example 2 """
t = torch.arange(4).reshape(2, 2)
print(t.repeat_interleave(repeats=3))
# tensor([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3])
print(t.repeat_interleave(repeats=3, dim=0))
# tensor([[0, 1],
# [0, 1],
# [0, 1],
# [2, 3],
# [2, 3],
# [2, 3]])
print(t.repeat_interleave(repeats=3, dim=1))
# tensor([[0, 0, 0, 1, 1, 1],
# [2, 2, 2, 3, 3, 3]])
""" Example 3 """
t = torch.arange(4).reshape(2, 2)
print(t.repeat_interleave(repeats=torch.tensor([2, 3]), dim=0)) # t的第一行重复2次,第2行重复3次
# tensor([[0, 1],
# [0, 1],
# [2, 3],
# [2, 3],
# [2, 3]])
print(t.repeat_interleave(repeats=torch.tensor([3, 2]), dim=1)) # t的第一列重复3次,第2列重复2次
# tensor([[0, 0, 0, 1, 1],
# [2, 2, 2, 3, 3]])
边栏推荐
- Is it safe to open a stock account online? I'm a novice, please guide me
- Log4j2
- SSRF
- Principle of scalable contract delegatecall
- Some problems encountered in introducing lvgl into esp32 Arduino
- qt 仪表自定义控件
- Cluster Analysis in R Simplified and Enhanced
- xss-labs-master靶场环境搭建与1-6关解题思路
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
猜你喜欢

2022年遭“挤爆”的三款透明LED显示屏

H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
![[visual studio 2019] create and import cmake project](/img/51/6c2575030c5103aee6c02bec8d5e77.jpg)
[visual studio 2019] create and import cmake project
![[idea] use the plug-in to reverse generate code with one click](/img/b0/00375e61af764a77ea0150bf4f6d9d.png)
[idea] use the plug-in to reverse generate code with one click

Esp32 audio frame esp-adf add key peripheral process code tracking

FLESH-DECT(MedIA 2021)——一个material decomposition的观点

Dynamic memory (advanced 4)

可升级合约的原理-DelegateCall

电脑无缘无故黑屏,无法调节亮度。

Principle of scalable contract delegatecall
随机推荐
Dynamic memory (advanced 4)
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
Homer forecast motif
YYGH-BUG-04
File operation (detailed!)
PHP query distance according to longitude and latitude
easyExcel和lombok注解以及swagger常用注解
Cmake cross compilation
The selected cells in Excel form have the selection effect of cross shading
Is the stock account given by qiniu business school safe? Can I open an account?
抖音海外版TikTok:正与拜登政府敲定最终数据安全协议
Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research
Mish-撼动深度学习ReLU激活函数的新继任者
XSS labs master shooting range environment construction and 1-6 problem solving ideas
GGPlot Examples Best Reference
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
6方面带你认识LED软膜屏 LED软膜屏尺寸|价格|安装|应用
多文件程序X32dbg动态调试
How to Visualize Missing Data in R using a Heatmap
GGPlot Examples Best Reference