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

相关推荐
收放扳机3 小时前
多机连线(In-Line)场景下,收放板机“缓存工位”的容量与架构选型参考
人工智能·科技·自动化·制造·pcb工艺
曲径海盗3 小时前
【Python 学习第 38 天】for 循环
python·学习笔记·编程入门
武天旭3 小时前
AI安全--AI大模型的后门攻击检测与防御
人工智能·深度学习·安全·机器学习
智能运维指南4 小时前
研制过程“两张皮“怎么破:研制一体化支撑平台的定位、能力边界与落地前提
java·前端·人工智能·嘉为蓝鲸
人工智能AI技术4 小时前
DeepInfer开源炸场!别再妄想个人电脑跑满血DeepSeek
人工智能
深蓝电商API4 小时前
用AI生成拟人行为:让鼠标轨迹更“有人味”
人工智能·爬虫
在世修行4 小时前
智学文枢:变式题生成引擎
python·ai·pyside·题目生成
znnnk4 小时前
数据库被AI“重写”:2026,从CPU中心走向GPU中心
数据库·人工智能·oracle
深蓝AI4 小时前
网站正在对 AI Agent 集体关门:亚马逊封锁 Muse、银行拒绝登录,开发者怎么活下去
人工智能
jason_renyu4 小时前
Vue 4 × AI:流式对话、Agent 状态与高性能渲染的下一代组合
人工智能·vue4·vue4和ai·vue4xai