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
相关推荐
磁场转动100万匹4 小时前
基于 dlib 与 OpenCV 的疲劳驾驶检测:眼睛纵横比(EAR)原理与代码逐段解析
pytorch·python
Dr_Fourier4 小时前
AWQ量化
c++·人工智能·pytorch·ai
就叫你天选之人啦1 天前
安装torch+vllm+flash_attn的prompt
人工智能·pytorch·python
Thomas.Sir2 天前
第16课:PyTorch|循环神经网络RNN与序列数据处理【让模型拥有“记忆”】
人工智能·pytorch·rnn
失舵之舟-2 天前
【全步骤】Pytorch 搭建自己的Unet语义分割平台
pytorch·语义分割·unet
盼小辉丶2 天前
PyTorch强化学习实战——分布式策略梯度
人工智能·pytorch·深度学习·强化学习
jzshmyt3 天前
我用 Python 从零“生成“了一个宇宙,然后让它观察自己(v14)
人工智能·pytorch·python·numpy·matplotlib·空间计算·scipy
Thomas.Sir3 天前
第13课:PyTorch|经典CNN网络模型从零复现【从LeNet到VGG的逐行实现】
网络·pytorch·cnn
Thomas.Sir3 天前
第7课:PyTorch|激活函数全品类详解与选型实战【神经网络的“火花塞”】
人工智能·pytorch·神经网络
Thomas.Sir3 天前
第10课:PyTorch|学习率调度策略全面精讲【训练过程的“自适应巡航”】
人工智能·pytorch·学习