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

相关推荐
kszlgy2 小时前
Day 52 神经网络调参指南
python
wrj的博客4 小时前
python环境安装
python·学习·环境配置
康康的AI博客4 小时前
腾讯王炸:CodeMoment - 全球首个产设研一体 AI IDE
ide·人工智能
中达瑞和-高光谱·多光谱4 小时前
中达瑞和LCTF:精准调控光谱,赋能显微成像新突破
人工智能
mahtengdbb14 小时前
【目标检测实战】基于YOLOv8-DynamicHGNetV2的猪面部检测系统搭建与优化
人工智能·yolo·目标检测
Pyeako4 小时前
深度学习--BP神经网络&梯度下降&损失函数
人工智能·python·深度学习·bp神经网络·损失函数·梯度下降·正则化惩罚
清 澜4 小时前
大模型面试400问第一部分第一章
人工智能·大模型·大模型面试
不大姐姐AI智能体5 小时前
搭了个小红书笔记自动生产线,一句话生成图文,一键发布,支持手机端、电脑端发布
人工智能·经验分享·笔记·矩阵·aigc
摘星编程5 小时前
OpenHarmony环境下React Native:Geolocation地理围栏
python