Pytorch: repeat, repeat_interleave, tile的用法
https://zhuanlan.zhihu.com/p/474153365
torch.repeat
使张量沿着某个维度进行复制, 并且不仅可以复制张量,也可以拓展张量的维度:
import torch
x = torch.randn(2, 4)
# 1. 沿着某个维度复制
x.repeat(1, 1).size() # torch.Size([2, 4])
x.repeat(2, 1).size() # torch.Size([4, 4])
x.repeat(1, 2).size() # torch.Size([2, 8])
# 2. 不仅可以复制维度, 还可以拓展维度
x.repeat(1, 1, 1).size() # torch.Size([1, 2, 4])
x.repeat(2, 1, 1).size() # torch.Size([2, 2, 4])
x.repeat(1, 1, 1, 1).size() # torch.Size([1, 1, 2, 4])
# 3. repeat中传入的参数不可以少于x的维度
x.repeat(1) # 报错
torch.repeat_interleave
torch.repeat_interleave的行为与numpy.repeat类似,但是和torch.repeat不同,这边还是以代码为例:
import torch
x = torch.randn(2, 2)
print(x)
>>> tensor([[ 0.4332, 0.1172],
[ 0.8808, -1.7127]])
print(x.repeat(2, 1))
>>> tensor([[ 0.4332, 0.1172],
[ 0.8808, -1.7127],
[ 0.4332, 0.1172],
[ 0.8808, -1.7127]])
print(x.repeat_interleave(2, dim=0))
>>> tensor([[ 0.4332, 0.1172],
[ 0.4332, 0.1172],
[ 0.8808, -1.7127],
[ 0.8808, -1.7127]])
print(x.repeat_interleave(2, dim=1))
>>> tensor([[ 0.4332, 0.4332, 0.1172, 0.1172],
[ 0.8808, 0.8808, -1.7127, -1.7127]])
# 如果不传dim参数, 则默认复制后拉平
print(x.repeat_interleave(2))
>>> tensor([ 0.4332, 0.4332, 0.1172, 0.1172, 0.8808, 0.8808, -1.7127, -1.7127])
从这个代码可以看出来torch.repeat更像是把tensor作为一个整体进行复制, 而torch.repeat_interleave更是针对tensor里的每个元素进行复制,并且torch.repeat_interleave可以通过传入一个一维的torch.Tensor来指定每个元素复制的次数
import torch
x = torch.tensor([[1, 2], [3, 4]])
result = torch.repeat_interleave(x, torch.tensor([1, 3]), dim=0)
print(result)
>>> tensor([[1, 2],
[3, 4],
[3, 4],
[3, 4]])
torch.tile
torch.tile函数也是元素复制的一个函数, 但是在传参上和torch.repeat不同,但是也是以input为一个整体进行复制, torch.tile如果只传入一个参数的话, 默认是沿着行进行复制
import torch
x = torch.tensor([[1, 2], [3, 4]])
# 只传入一个参数
print(x.tile((2, )))
>>> tensor([[1, 2, 1, 2],
[3, 4, 3, 4]])
print(x.repeat(1, 2))
>>> tensor([[1, 2, 1, 2],
[3, 4, 3, 4]])
torch.tile传入一个元组的话, 表示(行复制次数, 列复制次数)
import torch
x = torch.tensor([[1, 2], [3, 4]])
print(x.tile((2, 2)))
>>> tensor([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
print(x.repeat(2, 2))
>>> tensor([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
当传入的参数少于需要复制的元素的维度时, 如果一个tensor的形状为(2, 2, 2),传入tile中的参数为(2, 2)时, 会默认表示为(1, 2, 2)
import torch
x = torch.randn(2, 2, 2)
print(x)
>>> tensor([[[ 0.8517, 0.8721],
[-1.1591, -0.2000]],
[[ 0.3888, -0.8365],
[-1.6383, -0.1539]]])
print(x.tile((2, 2)))
>>> tensor([[[ 0.8517, 0.8721, 0.8517, 0.8721],
[-1.1591, -0.2000, -1.1591, -0.2000],
[ 0.8517, 0.8721, 0.8517, 0.8721],
[-1.1591, -0.2000, -1.1591, -0.2000]],
[[ 0.3888, -0.8365, 0.3888, -0.8365],
[-1.6383, -0.1539, -1.6383, -0.1539],
[ 0.3888, -0.8365, 0.3888, -0.8365],
[-1.6383, -0.1539, -1.6383, -0.1539]]])
当传入的参数多于需要复制的元素维度时,会拓展维度
import torch
x = torch.randn(2, 2)
print(x)
>>> tensor([[ 1.1165, -0.5559],
[-0.6341, 0.5215]])
print(x.tile((2, 2, 2)))
>>> tensor([[[ 1.1165, -0.5559, 1.1165, -0.5559],
[-0.6341, 0.5215, -0.6341, 0.5215],
[ 1.1165, -0.5559, 1.1165, -0.5559],
[-0.6341, 0.5215, -0.6341, 0.5215]],
[[ 1.1165, -0.5559, 1.1165, -0.5559],
[-0.6341, 0.5215, -0.6341, 0.5215],
[ 1.1165, -0.5559, 1.1165, -0.5559],
[-0.6341, 0.5215, -0.6341, 0.5215]]])
使用tile和reshape代替repeat_interleave
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]]) # shape: (2, 3)
y = torch.repeat_interleave(x, repeats=3, dim=0)
print(y)
>>> tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[4, 5, 6]])
# 直接使用tile, 无法得到类似的结果
z = torch.tile(x, (3, ))
print(z)
>>> tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6]])
z = torch.tile(x, (3, 1))
print(z)
>>> tensor([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
# 需要使用 tile + reshape 才可以得到类似的结果
z = torch.tile(x, (3, ))
print(z.shape) # (2, 9)
print(z.reshape(6, 3)) # 得到了和y一样的输出
>>> tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[4, 5, 6]])
Pytorch: repeat, repeat_interleave, tile的用法的更多相关文章
- Pytorch中nn.Conv2d的用法
Pytorch中nn.Conv2d的用法 nn.Conv2d是二维卷积方法,相对应的还有一维卷积方法nn.Conv1d,常用于文本数据的处理,而nn.Conv2d一般用于二维图像. 先看一下接口定义: ...
- numpy数组扩展函数repeat和tile用法
numpy.repeat(a, repeats, axis=None) >>> a = np.arange(3) >>> a array([0, 1, 2]) &g ...
- python tile函数用法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- Python-Numpy的tile函数用法
1.函数的定义与说明 函数格式tile(A,reps) A和reps都是array_like A的类型众多,几乎所有类型都可以:array, list, tuple, dict, matrix以及基本 ...
- [PyTorch]PyTorch中反卷积的用法
文章来源:https://www.jianshu.com/p/01577e86e506 pytorch中的 2D 卷积层 和 2D 反卷积层 函数分别如下: class torch.nn.Conv2d ...
- python3中numpy函数tile的用法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- numpy中tile的用法
a=arange(1,3) #a的结果是: array([1,2]) 1,当 tile(a,1) 时: tile(a,1) #结果是 array([1,2]) tile(a,2) #结果是 array ...
- pytorch实现yolov3(4) 非极大值抑制nms
在上一篇里我们实现了forward函数.得到了prediction.此时预测出了特别多的box以及各种class probability,现在我们要从中过滤出我们最终的预测box. 理解了yolov3 ...
- Python numpy中矩阵的用法总结
关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...
随机推荐
- VisonPro · 视觉定位工具包示例
一.概述 视觉定位工具包一般包含: 1.相机取像: 2.图像九点标定: 3.Mark点粗定位: 4.建立粗定位坐标系: 5.Mark点精定位 6.输出Mark点坐标,角度等信息. 二.分类 1.单特征 ...
- Android 12(S) 图像显示系统 - HWC HAL 初始化与调用流程
必读: Android 12(S) 图像显示系统 - 开篇 接口定义 源码位置:/hardware/interfaces/graphics/composer/ 在源码目录下可以看到4个版本的HIDL ...
- Spring Boot 知识点总结
现在仅总结重要和实用的知识点,更加全面的请见链接:1.:2.. 微服务:架构风格(服务微化):一个应用应该是一组小型服务:可以通过HTTP的方式进行互通:微服务:每一个功能元素终都是一个可独立替换和独 ...
- 【cartographer_ros】五: 发布和订阅陀螺仪Imu信息
上一节介绍了里程计Odometry传感数据的订阅和发布. 本节会介绍陀螺仪Imu数据的发布和订阅.陀螺仪在cartographer中主要用于前端位置预估和后端优化. 目录 1:sensor_msgs/ ...
- windows10 使用elasticsearch和kibana
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-windows-x86_64.zip https:// ...
- IIS部署的H5的单页面跳转的配置
<?xml version="1.0" encoding="UTF-8"?><configuration> <system.web ...
- 基于 Hexo 从零开始搭建个人博客(二)
阅读本篇前,请先配置好相应的环境,请仔细阅读教程 基于 Hexo 从零开始搭建个人博客(一). 原文链接:基于 Hexo 从零开始搭建个人博客(二) 前言 博客搭建过程遇到任何问题,优先在本页面搜索, ...
- 透过Redis源码探究Hash表的实现
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/667 本文使用的Redis 5.0源码 概述 我们在学习 Redis ...
- 不是第七代的 Win 7
贡献者:历史上的今天 Windows 7 是由微软公司(Microsoft)2009 年 10 月 22 日发布的桌面端操作系统,它影响了每个行业的方方面面,以至于很多人仍然在日常生活和工作中使用它. ...
- python 进程理解
简介 线程理解中介绍过,再回顾一遍,一个应用程序由多个进程组成,一个进程由多个线程组成,由操作系统根据优先级.时间片来绝对线程的运行 进程 python的进程不同于线程,在主流的cpython解释器下 ...