【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.

相关推荐
陕西企来客1 分钟前
2026年9月西安 AI 搜索优化是什么?功能与价值解读
人工智能·西安 ai 搜索优化是什么
Allen_LVyingbo8 分钟前
医疗AI基础2026-构建可靠智能体的编程路径(上)
大数据·数据库·人工智能·python·自动化
MetaLite19 分钟前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程
angered25 分钟前
「AI 应用 / AI Agent」行业日报 · 2026-09-05
人工智能·ai编程
CAIE注册人工智能工程师26 分钟前
突发!GPT-6刚刚开放使用、上线API,能体验AGI模型了
人工智能·gpt·agi
进击切图仔33 分钟前
Autodl 平台接受客户端端口数据
人工智能
新知图书35 分钟前
第8章 智能体设计模式5:上下文工程
人工智能·智能体
IT_陈寒38 分钟前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端
Rnan-prince43 分钟前
堆与TopK · 从零到通透 —— 9 节全系列复盘
python·算法
熊野君1 小时前
第 7 章 AI时代产品经理新增能力
大数据·人工智能·经验分享·职场和发展·产品经理