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
相关推荐
Smoothcloud润云5 小时前
PyTorch大模型权重加载与启动耗时优化实战:定位磁盘、内存与显存瓶颈
人工智能·pytorch·python·深度学习·gpu算力
小弥儿12 小时前
GitHub今日热榜 | 2026-10-12:pytorch 上升四位
人工智能·pytorch·学习·开源·github
论文复现现场14 小时前
YOLO26 单卡 RTX 4090 24GB 训练教程:自定义数据集、显存优化与云 GPU 部署
pytorch·目标检测·rtx4090·算家云·yolo26
朝朝辞暮i16 小时前
第一部分:ACT 阶段总结——从 Behavior Cloning 到完整的 Transformer Action Policy
人工智能·pytorch·深度学习·transformer·vla
盼小辉丶19 小时前
PyTorch强化学习实战(28)——遗传算法在强化学习中的应用
pytorch·深度学习·强化学习
Smoothcloud润云1 天前
PyTorch推理服务动态批处理实战:兼顾吞吐与延迟
人工智能·pytorch·python·深度学习·ai·gpu算力
Smoothcloud润云1 天前
PyTorch推理请求超时与取消处理实战
人工智能·pytorch·python·深度学习·机器学习·gpu算力·gpu
czq_26867194872 天前
Python打卡第36天
pytorch·python·深度学习
程序猿阿森3 天前
混合精度详解:FP16 vs BF16,Loss Scaling 与 PyTorch 实战
人工智能·pytorch·python
Είναι η κοπέλα4 天前
模型量化完全指南:GGUF、GPTQ、AWQ 怎么选
人工智能·pytorch·python