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
相关推荐
%4719 小时前
DAY 38
人工智能·pytorch·深度学习
Είναι η κοπέλα1 天前
PyTorch 模型导出与部署实战:ONNX + onnxruntime(可直接落地)
人工智能·pytorch·python
呆萌很1 天前
PyTorch CosineAnnealingLR的T_max和eta_min参数设置
人工智能·pytorch·python
盼小辉丶1 天前
PyTorch强化学习实战——用预训练语言模型和ChatGPT玩转文本游戏
pytorch·深度学习·语言模型·chatgpt·强化学习
Lee_jerome2 天前
从 PyTorch 权重到 RK3588 板端推理:ResNet18 二分类模型完整部署教程
pytorch·边缘计算·rk3588·模型部署·onnx·resnet18·int8量化
菜冻鱼2 天前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
菜冻鱼2 天前
Python-pytorch-模型保存与加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
小白学大数据2 天前
CuPy vs Numba vs PyTorch:GPU 加速方案怎么选
人工智能·pytorch·爬虫·python
菜冻鱼2 天前
Python-pytorch-数据加载
开发语言·人工智能·pytorch·python·深度学习·机器学习
现代野蛮人2 天前
【深度学习实验】—— 利用 RNN 模型进行心脏病预测
pytorch·python·tensorflow·ml