游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 论文笔记

  • 一些小知识点

  • 《深度学习500问》

  • pytorch知识点

    • unsqueeze与squeeze函数
    • torch.arange函数详解
    • TensorDataset、Dataset和DataLoader
    • 如何在pytorch中使用tensorboard
    • tensor比较大小函数
      • torch.ge、torch.gt、torch.le、torch.lt、torch.ne、torch.eq
  • 人工智能
  • pytorch知识点
yangzhixuan
2024-05-30
目录

tensor比较大小函数

参考:https://blog.csdn.net/qq_50001789/article/details/128973672 (opens new window)

# torch.ge、torch.gt、torch.le、torch.lt、torch.ne、torch.eq

torch.ge(input, other, *, out=None) → Tensor
torch.gt(input, other, *, out=None) → Tensor
torch.le(input, other, *, out=None) → Tensor
torch.lt(input, other, *, out=None) → Tensor
torch.ne(input, other, *, out=None) → Tensor
torch.eq(input, other, *, out=None) → Tensor
1
2
3
4
5
6

功能:

  • torch.ge:实现大于等于(≥)运算

  • torch.gt:实现大于(>)运算

  • torch.le:实现小于等于(≤)运算

  • torch.lt:实现小于(<)运算

  • torch.ne:实现不等于(≠)运算 输入:

  • input:待比较的数组

  • other:比较数值,可以是数组,也可以是一个数。tensor或float格式

输出:

布尔张量,尺寸和input相同,当input和other元素之间符合运算时,对应位置元素为True,否则为Flase。

注:

  • 第二个参数可以是一个数字,也可以是一个张量数组,只要与第一个参数满足广播条件即可;
  • 也可以通过tensor加后缀的形式实现,如a.ge,a相当于input,即待比较的数组;
  • 如果输入的是数组,则必须是tensor类型

代码案例:

import torch
a=torch.arange(5)
b=torch.tensor(3)
print(a)
print(b)
print(torch.ge(a,b))
print(torch.gt(a,b))
print(torch.le(a,b))
print(torch.lt(a,b))
print(torch.ne(a,b))
print(torch.eq(a,b))
1
2
3
4
5
6
7
8
9
10
11

输出

tensor([0, 1, 2, 3, 4])
tensor(3)
# 大于等于
tensor([False, False, False,  True,  True])
# 大于
tensor([False, False, False, False,  True])
# 小于等于
tensor([ True,  True,  True,  True, False])
# 小于
tensor([ True,  True,  True, False, False])
# 不等于
tensor([ True,  True,  True, False,  True])
# 等于
tensor([False, False, False,  True, False])
1
2
3
4
5
6
7
8
9
10
11
12
13
14

官方文档

torch.ge() (opens new window)

torch.gt() (opens new window)

torch.le() (opens new window)

torch.lt() (opens new window)

torch.ne() (opens new window)

torch.eq() (opens new window)

编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
如何在pytorch中使用tensorboard

← 如何在pytorch中使用tensorboard

最近更新
01
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
02
半监督学习经典方法 Π-model、Mean Teacher
04-10
03
A survey on semi-supervised learning
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式