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

相关推荐
小兵张健9 小时前
35岁程序员的春天来了
人工智能
大怪v9 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
冬奇Lab9 小时前
OpenClaw 深度解析(六):节点、Canvas 与子 Agent
人工智能·开源
刀法如飞10 小时前
AI提示词框架深度对比分析
人工智能·ai编程
IT_陈寒12 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
1G13 小时前
openclaw控制浏览器/自动化的playwright MCP + Mcporter方案实现
人工智能
踩着两条虫13 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能
扉川川13 小时前
OpenClaw 架构解析:一个生产级 AI Agent 是如何设计的
前端·人工智能
Flittly13 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
星浩AI13 小时前
让模型自己写 Skills——从素材到自动生成工作流
人工智能·后端·agent