torch.where用法介绍

torch.where用法1介绍

python 复制代码
torch.where(condition, x, y) → Tensor

这个用法介绍直接搜就可以,不做介绍

torch.where用法2介绍(在yolov8中计算TP中出现)

python 复制代码
torch.where(condition) → Tensor

返回的condition中为True的索引

直接举一个例子吧

python 复制代码
condition = torch.tensor([[True, False, False], [False, True, False], [True, True, True]]) # torch.Size([3, 3])
torch.where(condition)
'''
(tensor([0, 1, 2, 2, 2]), tensor([0, 1, 0, 1, 2]))
'''

返回的是一个二维元祖,tensor([0, 1, 2, 2, 2])这个代表的是condition中的第一维所有的索引,tensor([0, 1, 0, 1, 2])这个代表的是condition第二维中所有的索引,例如,输出结果组合起来

python 复制代码
[0, 1, 2, 2, 2]
[0, 1, 0, 1, 2]
一一组合起来
[(0, 0), (1, 1), (2, 0), (2, 1), (2, 2)]
可以看到
condition[0][0]=True, condition[1][1]=True, condition[2][0]=True, condition[2][1]=True, condition[2][2]=True
其余的都为False
相关推荐
盼小辉丶2 天前
PyTorch实战(30)——使用TorchScript和ONNX导出通用PyTorch模型
人工智能·pytorch·深度学习·模型部署
封奚泽优2 天前
使用mmdetection项目进行训练记录
pytorch·python·cuda·mmdetection·mmcv
tony3652 天前
pytorch分布式训练解释
人工智能·pytorch·分布式
weixin_贾2 天前
深度学习基础理论与 PyTorch 实战 —— 从传统机器学习到前沿模型全攻略
pytorch·深度学习·机器学习
大连好光景3 天前
PyTorch深度学习----优化器
pytorch·深度学习·学习
多恩Stone4 天前
【3D-AICG 系列-11】Trellis 2 的 Shape VAE 训练流程梳理
人工智能·pytorch·算法·3d·aigc
隔壁大炮4 天前
08. PyTorch_张量基本创建方式
人工智能·pytorch·python
隔壁大炮4 天前
07. PyTorch框架简介
人工智能·pytorch·python
大鹏的NLP博客4 天前
Rust + PyTorch 实现 BGE 向量检索系统
人工智能·pytorch·rust
勾股导航6 天前
蚁群优化算法
人工智能·pytorch·python