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
相关推荐
盼小辉丶6 小时前
PyTorch实战(29)——使用TorchServe部署PyTorch模型
人工智能·pytorch·深度学习·模型部署
IRevers1 天前
【YOLO】YOLO-Master 腾讯轻量级YOLO架构超越YOLO-13(含检测和分割推理)
图像处理·人工智能·pytorch·python·yolo·transformer·边缘计算
小锋java12341 天前
【技术专题】PyTorch2 深度学习 - 张量(Tensor)的定义与操作
pytorch·深度学习
归一码字1 天前
DDPG手写讲解
人工智能·pytorch
七夜zippoe2 天前
图神经网络实战:从社交网络到推荐系统的工业级应用
网络·人工智能·pytorch·python·神经网络·cora
本是少年2 天前
构建 HuggingFace 图像-文本数据集指南
pytorch·transformer
Suryxin.3 天前
从0开始复现nano-vllm「model_runner-py」下半篇之核心数据编排与执行引擎调度
人工智能·pytorch·深度学习·ai·vllm
weixin_468466854 天前
PyTorch导出ONNX格式分割模型及在C#中调用预测
人工智能·pytorch·深度学习·c#·跨平台·onnx·语义分割
七夜zippoe5 天前
PyTorch深度革命:从自动微分到企业级应用
人工智能·pytorch·python
好的收到1115 天前
PyTorch深度学习(小土堆)笔记3:小土堆 Dataset 类实战笔记,99% 的新手都踩坑!看完秒懂数据加载底层逻辑!
pytorch·笔记·深度学习