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]])
相关推荐
断眉的派大星1 天前
pytorch中保存训练模型和加载训练模型的用法
人工智能·pytorch·python
郝学胜-神的一滴1 天前
激活函数:神经网络的「非线性灵魂」,让模型从“直线”走向“万能”
人工智能·pytorch·python·深度学习·神经网络·程序人生·机器学习
山海AI手册1 天前
028、边缘AI与嵌入式部署:TensorFlow Lite/PyTorch Mobile实战手记
人工智能·pytorch·tensorflow
郝学胜-神的一滴1 天前
深度学习激活函数核心精讲:Sigmoid 原理、推导与工程实践
人工智能·pytorch·python·深度学习·神经网络·机器学习
龙文浩_2 天前
AI梯度下降与PyTorch张量操作技术指南
人工智能·pytorch·python·深度学习·神经网络·机器学习·自然语言处理
Dfreedom.2 天前
PyTorch 与 scikit-learn 全景对比分析
人工智能·pytorch·深度学习·机器学习·scikit-learn
Dfreedom.2 天前
PyTorch 详解:动态计算图驱动的深度学习框架
人工智能·pytorch·python·深度学习
Z.风止3 天前
Large Model-learning(4)
人工智能·pytorch·笔记·python·深度学习·机器学习
EmmaXLZHONG3 天前
Deep Learning With Pytorch Notes
人工智能·pytorch·深度学习
龙文浩_3 天前
AI NLP核心技术指南
人工智能·pytorch·深度学习·神经网络·自然语言处理