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

相关推荐
Dm_dotnet1 小时前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算1 小时前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
用户2519162427111 小时前
Python之语言特点
python
机器之心1 小时前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
刘立军2 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
Juchecar2 小时前
交叉熵:深度学习中最常用的损失函数
人工智能
林木森ai3 小时前
爆款AI动物运动会视频,用Coze(扣子)一键搞定全流程(附保姆级拆解)
人工智能·aigc
聚客AI3 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
BeerBear5 小时前
【保姆级教程-从0开始开发MCP服务器】一、MCP学习压根没有你想象得那么难!.md
人工智能·mcp
小气小憩5 小时前
“暗战”百度搜索页:Monica悬浮球被“围剿”,一场AI Agent与传统巨头的流量攻防战
前端·人工智能