PyTorch中repeat与repeat_interleave的内涵及用法

torch.tensor.repeat(*sizes)
  • 官方解释:Repeats this tensor along the specified dimensions.
  • 参数说明:
    • sizes (torch.Size or int…) – The number of times to repeat this tensor along each dimension.
  • 个人理解
# 经过实测,我摸索到的规律如下:
# 对于一个形状为[5,]的变量X
Y = X.repeat([4])  # Y的形状为 [5,]*[4,]=[20,]   
Y = X.repeat([3,4])  # Y的形状为 [1,5]*[3,4]=[3,20]
Y = X.repeat([2,3,4])  # Y的形状为 [1,1,5]*[2,3,4]=[2,3,20]
Y = X.repeat([1,2,3,4])  # Y的形状为 [1,1,1,5]*[1,2,3,4]=[1,2,3,20]
# 对于一个形状为[4,5]的变量X
Y = X.repeat([3,4])  # Y的形状为 [4,5]*[3,4]=[12,20]
Y = X.repeat([2,3,4])  # Y的形状为 [1,4,5]*[2,3,4]=[2,12,20]
Y = X.repeat([1,2,3,4])  # Y的形状为 [1,1,4,5]*[1,2,3,4]=[1,2,12,20]
torch.repeat_interleave(input, repeats, dim=None, *, output_size=None)
  • 官方解释:Repeat elements of a tensor.
  • 参数说明:
    • input (Tensor) – the input tensor.
    • repeats (Tensor or int) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
    • dim (int, optional) – The dimension along which to repeat values. By default, use the flattened input array, and return a flat output array.
    • output_size (int, optional) – Total output size for the given axis ( e.g. sum of repeats). If given, it will avoid stream syncronization needed to calculate output shape of the tensor.
  • 个人理解
# 经过实测,我摸索到的规律如下:
# 在不指定需要重复的轴时,函数将输入的张量拉平(flatten)后,对每个元素复制指定的次数(repeats)返回。
# 在指定针对某个轴重复时,输出张量的尺寸是输入张量对应轴乘以重复次数。
# 最关键的是理解参数repeats的含义:指定每个元素的重复次数。
	# 输入为实数(单元素的张量)是代表所有元素的重复次数。
	# 输入为非单元素张量时,必须和指定轴的元素数相同,否则报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值