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

相关推荐
YueJoy.AI7 小时前
AI应用的API安全:从认证到授权的完整指南
人工智能·ai·语言模型
码农小旋风8 小时前
使用 ChatGPT 聚合站前,先看安全和隐私判断清单
人工智能·安全·自然语言处理·chatgpt·claude
周易宅8 小时前
CLAUDE.md 与 MEMORY.md:AI 编程助手配置的两条平行铁轨
人工智能·ai·agent·claude
明志数科8 小时前
灵犀X2学跳舞技术解析:机器人动作学习的数据方案
人工智能·计算机视觉
不懂的浪漫8 小时前
Role Agent 方法论:如何把一个标准工作流 Agent 化
人工智能·ai·agent
彦为君8 小时前
JavaSE-07-异常机制
java·开发语言·后端·python·spring
Bruce_Liuxiaowei8 小时前
2026年5月第5周网络安全形势周报
人工智能·安全·web安全·ai·智能体
适应规律8 小时前
【无标题】
人工智能·python·算法
Rain5098 小时前
mini-cc 的 MCP 协议:给 AI 装个 USB-C 接口
c语言·开发语言·前端·人工智能·架构·node.js·ai编程