当前位置:网站首页>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]])
边栏推荐
- 揭露数据不一致的利器 —— 实时核对系统
- QT获取某个日期是第几周
- Seriation in R: How to Optimally Order Objects in a Data Matrice
- Homer预测motif
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- 可昇級合約的原理-DelegateCall
- Data analysis - Matplotlib sample code
- RPA进阶(二)Uipath应用实践
- How to Visualize Missing Data in R using a Heatmap
- Homer forecast motif
猜你喜欢

Amazon cloud technology community builder application window opens

XSS labs master shooting range environment construction and 1-6 problem solving ideas

How to Create a Nice Box and Whisker Plot in R

Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research

YYGH-BUG-05

YYGH-BUG-04

Data analysis - Matplotlib sample code

GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R

How to Create a Beautiful Plots in R with Summary Statistics Labels

GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
随机推荐
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
由粒子加速器产生的反中子形成的白洞
Mish shake the new successor of the deep learning relu activation function
MySQL linked list data storage query sorting problem
What is the relationship between digital transformation of manufacturing industry and lean production
Wechat applet uses Baidu API to achieve plant recognition
Is the stock account given by qiniu business school safe? Can I open an account?
CMake交叉编译
Homer预测motif
K-Means Clustering Visualization in R: Step By Step Guide
[idea] use the plug-in to reverse generate code with one click
YYGH-BUG-05
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
MySQL basic statement
TDSQL|就业难?腾讯云数据库微认证来帮你
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
【2022 ACTF-wp】
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
Seriation in R: How to Optimally Order Objects in a Data Matrice