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

相关推荐
weixin_668几秒前
OCR 模型深度对比分析报告 - AI分析
人工智能·ocr
wasp5202 分钟前
从 Vibe Coding 到真·生产力:OpenHarness 的“Harness 方程式”及其实战分析
人工智能·架构·开源·agent
V胡桃夹子4 分钟前
pyenv-win 完整安装+使用手册
python·pyenv
weixin_408099675 分钟前
【完整教程】天诺脚本如何调用 OCR 文字识别 API?自动识别屏幕文字实战(附代码)
前端·人工智能·后端·ocr·api·天诺脚本·自动识别文字脚本
ego.iblacat9 分钟前
Python 连接 MySQL 数据库
数据库·python·mysql
lvyuanj21 分钟前
深度解析Agent技术演进路径与未来趋势
人工智能
卷卷说风控33 分钟前
Claude Code 技术架构深扒:Prompt / Context / Harness 三维设计实践
人工智能·架构·prompt
Frank学习路上39 分钟前
【AI技能】跟着费曼学自动驾驶
人工智能·机器学习·自动驾驶
humors2211 小时前
各厂商工具包网址
java·数据库·python·华为·sdk·苹果·工具包
Want5951 小时前
Rokid AI Glasses应用开发实战:把记账助手“戴”在脸上
人工智能