【Torch API】pytorch 中repeat_interleave函数详解

torch.``repeat_interleave(input , repeats , dim=None) → Tensor

Repeat elements of a tensor.

Parameters

  • input (Tensor) -- the input tensor.

  • repeats (Tensoror 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.

Returns

Repeated tensor which has the same shape as input, except along the given axis.

复制代码
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
>>> torch.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
        [3, 3, 3, 4, 4, 4]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
tensor([[1, 2],
        [3, 4],
        [3, 4]])

torch.``repeat_interleave(repeats) → Tensor

If the repeats is tensor([n1, n2, n3, ...]), then the output will be tensor([0, 0, ..., 1, 1, ..., 2, 2, ..., ...]) where 0 appears n1 times, 1 appears n2 times, 2 appears n3 times, etc.

相关推荐
FreeCode4 小时前
智能体化系统(Agentic System)开发面临的挑战及应对
人工智能·agent
程序猿小D4 小时前
【完整源码+数据集+部署教程】 【零售和消费品&存货】【无人零售】自动售卖机饮料检测系统源码&数据集全套:改进yolo11-KernelWarehouse
python·yolo·计算机视觉·目标跟踪·数据集·yolo11·自动售卖机饮料检测系统
寒怜z4 小时前
python 景区游客量统计
python
程序员晚枫4 小时前
Python 3.14发布:多解释器让性能飙升300%,GIL时代即将终结!
python
程序猿小D4 小时前
【完整源码+数据集+部署教程】 【零售和消费品&存货】条形码检测系统源码&数据集全套:改进yolo11-TADDH
python·yolo·计算机视觉·目标跟踪·数据集·yolo11·条形码检测系统
废弃的小码农4 小时前
测试基础--Day01--软件测试基础理论
python·功能测试·测试工具
leafff1234 小时前
Stable Diffusion在进行AI 创作时对算力的要求
人工智能·stable diffusion
Juchecar4 小时前
AI大模型商业模式分析
人工智能
leafff1235 小时前
Stable Diffusion进行AIGC创作时的算力优化方案
人工智能·stable diffusion·aigc
FIN66685 小时前
昂瑞微:以射频“芯”火 点亮科技强国之路
前端·人工智能·科技·前端框架·智能