当前位置:网站首页>Repeat, tile and repeat in pytorch_ The difference between interleave
Repeat, tile and repeat in pytorch_ The difference between interleave
2022-07-02 11:56:00 【raelum】
torch.Tensor.repeat
repeat It can be understood visually as repeating the existing tensor many times to form “ Block matrix ”.
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]]])
It can be seen that repeat Dimensions Not lower than The dimension of tensor itself .
torch.Tensor.tile
In most cases ,tile And repeat It's the same thing , as follows :
""" 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]]])
And repeat The difference is , When the dimension to be repeated is lower than the tensor dimension ,tile Can also deal with , here tile Can use front 1 1 1 Automatically replenish dimensions .
torch.Tensor.repeat_interleave
Previously mentioned repeat and tile All are Repeat the entire tensor , And this time repeat_interleave It is Repeat the elements in the tensor .
The parameters are as follows :
torch.Tensor.repeat_interleave(repeats, dim=None)
repeats: Represents the number of times each element in the tensor will be repeated . It can be integer or tensor ;dim: Determines which axis to repeat numbers along . By default, the input will be flattened and repeated , Finally, output the flattened tensor .
""" 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 Repeat the first line of 2 Time , The first 2 Row repeat 3 Time
# tensor([[0, 1],
# [0, 1],
# [2, 3],
# [2, 3],
# [2, 3]])
print(t.repeat_interleave(repeats=torch.tensor([3, 2]), dim=1)) # t The first column of repeats 3 Time , The first 2 Column repeat 2 Time
# tensor([[0, 0, 0, 1, 1],
# [2, 2, 2, 3, 3]])
边栏推荐
- C#基于当前时间,获取唯一识别号(ID)的方法
- Cluster Analysis in R Simplified and Enhanced
- File operation (detailed!)
- YYGH-BUG-05
- ORB-SLAM2不同线程间的数据共享与传递
- 可升级合约的原理-DelegateCall
- A white hole formed by antineutrons produced by particle accelerators
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- 预言机链上链下调研
- vant tabs组件选中第一个下划线位置异常
猜你喜欢
![[visual studio 2019] create and import cmake project](/img/51/6c2575030c5103aee6c02bec8d5e77.jpg)
[visual studio 2019] create and import cmake project

HOW TO ADD P-VALUES TO GGPLOT FACETS

Tdsql | difficult employment? Tencent cloud database micro authentication to help you

How to Visualize Missing Data in R using a Heatmap

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

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

Develop scalable contracts based on hardhat and openzeppelin (II)

How to Visualize Missing Data in R using a Heatmap

YYGH-BUG-05

Some problems encountered in introducing lvgl into esp32 Arduino
随机推荐
K-Means Clustering Visualization in R: Step By Step Guide
Esp32 audio frame esp-adf add key peripheral process code tracking
RPA advanced (II) uipath application practice
YYGH-9-预约下单
电脑无缘无故黑屏,无法调节亮度。
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Log4j2
R HISTOGRAM EXAMPLE QUICK REFERENCE
2022年遭“挤爆”的三款透明LED显示屏
Principe du contrat évolutif - delegatecall
QT获取某个日期是第几周
A sharp tool for exposing data inconsistencies -- a real-time verification system
QT meter custom control
The selected cells in Excel form have the selection effect of cross shading
easyExcel和lombok注解以及swagger常用注解
How to Create a Beautiful Plots in R with Summary Statistics Labels
进入前六!博云在中国云管理软件市场销量排行持续上升
PyTorch搭建LSTM实现服装分类(FashionMNIST)
C # method of obtaining a unique identification number (ID) based on the current time
What is the relationship between digital transformation of manufacturing industry and lean production