【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 分钟前
1、python基础语法
开发语言·python
Chat_zhanggong34519 分钟前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法
霍格沃兹软件测试开发22 分钟前
Playwright MCP浏览器自动化指南:让AI精准理解你的命令
运维·人工智能·自动化
强化学习与机器人控制仿真27 分钟前
RSL-RL:开源人形机器人强化学习控制研究库
开发语言·人工智能·stm32·神经网络·机器人·强化学习·模仿学习
网易智企1 小时前
智能玩具新纪元:一个AI能力底座开启创新“加速度”
人工智能·microsoft
ituff1 小时前
微软认证考试又免费了
后端·python·flask
咚咚王者1 小时前
人工智能之数据分析 numpy:第十二章 数据持久化
人工智能·数据分析·numpy
沛沛老爹1 小时前
AI应用入门之LangChain中SerpAPI、LLM-Math等Tools的集成方法实践
人工智能·langchain·llm·ai入门·serpapi
roman_日积跬步-终至千里2 小时前
【强化学习基础(5)】策略搜索与学徒学习:从专家行为中学习加速学习过程
人工智能
梁正雄2 小时前
2、Python流程控制
开发语言·python