pytorch逐元素比较tensor大小实例

yipeiwu_com6年前Python基础

如下所示:

import torch
a = torch.tensor([[0.01, 0.011], [0.009, 0.9]])
mask = a.gt(0.01)
print(mask)

tensor比较大小可以用tensor.gt属性。上面比较了a中每个元素和0.01的大小,大于0.01的元素输出True。输出结果:

tensor([[False, True],
    [False, True]])

我们取出tenor a中对应的大于0.01的值:

a[mask]

将对应满足条件的元素输出并自动拉伸为一个一维向量输出:

tensor([0.0110, 0.9000])

我们也可以比较两个tensor大小

b = torch.tensor([[0.02, 1], [0, 1.0]])
torch.gt(a, b)
tensor([[False, False],
    [ True, False]])

以上这篇pytorch逐元素比较tensor大小实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案

Python IDLE 错误描述: Subprocess Startup Error IDLE's subprocess didn't make connection. Eithe...

Python把csv数据写入list和字典类型的变量脚本方法

如下所示: #coding=utf8 import csv import logging logging.basicConfig(level=logging.DEBUG,...

python变量不能以数字打头详解

在编写python函数时,无意中发现一个问题:python中的变量不能以数字打头,以下函数中定义了一个变量3_num_varchar,执行时报错。 函数如下: def databas...

对Python中内置异常层次结构详解

如下所示: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exceptio...

Python处理Excel文件实例代码

因为工作需求,需要审核一部分query内容是否有效,query储存在Excel中,文本内容为页面的Title,而页面的URL以HyperLink的格式关联到每个Cell。 于是本能的想到...