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
相关推荐
递归不收敛3 小时前
吴恩达机器学习课程(PyTorch 适配)学习笔记:3.4 强化学习
pytorch·学习·机器学习
递归不收敛4 小时前
吴恩达机器学习课程(PyTorch适配)学习笔记:1.4 模型评估与问题解决
pytorch·学习·机器学习
蒋星熠21 小时前
反爬虫机制深度解析:从基础防御到高级对抗的完整技术实战
人工智能·pytorch·爬虫·python·深度学习·机器学习·计算机视觉
it技术1 天前
Pytorch项目实战 :基于RNN的实现情感分析
pytorch·后端
mooooon L1 天前
DAY 43 复习日-2025.10.7
人工智能·pytorch·python·深度学习·神经网络
ting_zh2 天前
PyTorch、TensorFlow、JAX 简介
人工智能·pytorch·tensorflow
wa的一声哭了2 天前
Stanford CS336 assignment1 | Transformer Language Model Architecture
人工智能·pytorch·python·深度学习·神经网络·语言模型·transformer
JJJJ_iii2 天前
【深度学习04】PyTorch:损失函数、优化器、模型微调、保存与加载
人工智能·pytorch·笔记·python·深度学习·机器学习
Francek Chen2 天前
【深度学习计算机视觉】10:转置卷积
人工智能·pytorch·深度学习·计算机视觉·卷积神经网络
算法与编程之美2 天前
探索flatten的其他参数用法及对报错异常进行修正
人工智能·pytorch·python·深度学习·机器学习