pytorch 常用函数 max ,eq说明

作者:蓝鲸123 时间:2023-06-25 11:09:21 

max找出tensor 的行或者列最大的值:

找出每行的最大值:


import torch
outputs=torch.FloatTensor([[1],[2],[3]])
print(torch.max(outputs.data,1))

输出:

(tensor([ 1., 2., 3.]), tensor([ 0, 0, 0]))

找出每列的最大值:


import torch
outputs=torch.FloatTensor([[1],[2],[3]])
print(torch.max(outputs.data,0))

输出结果:

(tensor([ 3.]), tensor([ 2]))

Tensor比较eq相等:


import torch

outputs=torch.FloatTensor([[1],[2],[3]])
targets=torch.FloatTensor([[0],[2],[3]])
print(targets.eq(outputs.data))

输出结果:


tensor([[ 0],
[ 1],
[ 1]], dtype=torch.uint8)

使用sum() 统计相等的个数:


import torch

outputs=torch.FloatTensor([[1],[2],[3]])
targets=torch.FloatTensor([[0],[2],[3]])
print(targets.eq(outputs.data).cpu().sum())

输出结果:

tensor(2)

补充知识:PyTorch - torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le

flyfish

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

以上全是简写

参数是input, other, out=None

逐元素比较input和other

返回是torch.BoolTensor

pytorch 常用函数 max ,eq说明


import torch

a=torch.tensor([[1, 2], [3, 4]])
b=torch.tensor([[1, 2], [4, 3]])

print(torch.eq(a,b))#equals
# tensor([[ True, True],
#     [False, False]])

print(torch.ne(a,b))#not equal to
# tensor([[False, False],
#     [ True, True]])

print(torch.gt(a,b))#greater than
# tensor([[False, False],
#     [False, True]])

print(torch.lt(a,b))#less than
# tensor([[False, False],
#     [ True, False]])

print(torch.ge(a,b))#greater than or equal to
# tensor([[ True, True],
#     [False, True]])

print(torch.le(a,b))#less than or equal to
# tensor([[ True, True],
#     [ True, False]])

来源:https://blog.csdn.net/TH_NUM/article/details/80783037

标签:pytorch,函数,max,eq
0
投稿

猜你喜欢

  • 详解inet_pton()和inet_ntop()函数

    2023-07-23 04:38:26
  • python科学计算之numpy——ufunc函数用法

    2023-05-13 15:13:12
  • MySQL 性能优化的最佳20多条经验分享

    2024-01-22 00:42:49
  • Python语法学习之进程池与进程锁详解

    2021-09-01 23:16:17
  • JS验证逗号隔开可以是中文字母数字

    2024-04-19 10:48:08
  • pandas去重复行并分类汇总的实现方法

    2021-06-12 17:38:37
  • Python多进程编程常用方法解析

    2022-06-06 17:23:34
  • Linux下rpm方式安装mysql教程

    2024-01-21 07:40:53
  • 关于javascript原型的修改与重写(覆盖)差别详解

    2023-07-02 05:07:26
  • Python多进程模式实现多核CPU并行计算

    2022-12-01 21:26:20
  • ASP写的不错的"数字分页"涵数

    2008-10-19 17:21:00
  • 利用golang进行OpenCV学习和开发的步骤

    2024-05-22 10:16:56
  • Python读取图片为16进制表示简单代码

    2021-07-24 09:34:15
  • asp无限级分类加js收缩伸展功能代码

    2009-12-08 12:25:00
  • Node.js使用NodeMailer发送邮件实例代码

    2024-05-02 17:37:10
  • Echarts图表移动端横屏进入退出的实现

    2024-05-11 09:06:45
  • 如何使用MyBatis Plus实现数据库curd操作

    2024-01-21 17:37:13
  • html元素input使用方法

    2007-12-06 13:02:00
  • python石头剪刀布小游戏(三局两胜制)

    2021-07-24 00:06:30
  • golang 实用库gotable的具体使用

    2024-04-27 15:31:48
  • asp之家 网络编程 m.aspxhome.com