torch.where()两种用法

参考官方文档

官方文档中只给了第一种用法。根据条件condition,从input,other中选择元素f返回。如果满足条件,则返回input元素。若不满足,返回other元素。

还有一种用法是通过where返回张量中满足条件condition的坐标,以二维张量为例。

代码如下:

python 复制代码
import torch

nums = torch.tensor([
    [1, 2, 3], [4, 5, 6], [7, 8, 9]
])

x_loc, y_loc = torch.where(nums>5)
print('x_loc: ', x_loc)
print('y_loc: ', y_loc)

z = torch.where(nums>5, 10, 1)
print('z: ', z)

输出结果如下:

bash 复制代码
x_loc: tensor([1, 2, 2, 2])
y_loc: tensor([2, 0, 1, 2])
z: tensor([[ 1,  1,  1],
        [ 1,  1, 10],
        [10, 10, 10]])
相关推荐
豆浩宇15 小时前
Conda环境隔离和PyCharm配置,完美同时运行PaddlePaddle和PyTorch
人工智能·pytorch·算法·计算机视觉·pycharm·conda·paddlepaddle
㱘郳21 小时前
cifar10分类对比:使用PyTorch卷积神经网络和SVM
pytorch·分类·cnn
Tiger Z2 天前
《动手学深度学习v2》学习笔记 | 2.4 微积分 & 2.5 自动微分
pytorch·深度学习·ai
先做个垃圾出来………2 天前
PyTorch 模型文件介绍
人工智能·pytorch·python
我不是小upper2 天前
一文详解深度学习中神经网络的各层结构与功能!
人工智能·pytorch·深度学习
钱彬 (Qian Bin)2 天前
一文掌握工业缺陷检测项目实战(Pytorch算法训练、部署、C++ DLL制作、Qt集成)
c++·pytorch·python·qt·实战·工业缺陷检测·faster rcnn
vvilkim2 天前
PyTorch 中的循环神经网络 (RNN/LSTM):时序数据处理实战指南
pytorch·rnn·lstm
Hello Mr.Z2 天前
使用pytorch创建/训练/推理OCR模型
人工智能·pytorch·python
点云SLAM2 天前
PyTorch 中.backward() 详解使用
人工智能·pytorch·python·深度学习·算法·机器学习·机器人
山烛2 天前
深度学习:CNN 模型训练中的学习率调整(基于 PyTorch)
人工智能·pytorch·python·深度学习·cnn·调整学习率