pytorch 常用函数 max ,eq说明

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

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]])

以上这篇pytorch 常用函数 max ,eq说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • pytorch中torch.max和Tensor.view函数用法详解

    torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值. 例如: >>> si=torch.randn(4,5) >>> print(si) tensor([[ 1.1659, -1.5195, 0.0455, 1.7610, -0.2064], [-0.3443, 2.0483, 0.6303, 0.9475, 0.4364], [-1.5268, -1.0833, 1.6847, 0.0145, -0.2088], [-0.86

  • PyTorch中topk函数的用法详解

    听名字就知道这个函数是用来求tensor中某个dim的前k大或者前k小的值以及对应的index. 用法 torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor) input:一个tensor数据 k:指明是得到前k个数据以及其index dim: 指定在哪个维度上排序, 默认是最后一个维度 largest:如果为True,按照大到小排序: 如果为False,按照小到大排序

  • PyTorch中常用的激活函数的方法示例

    神经网络只是由两个或多个线性网络层叠加,并不能学到新的东西,简单地堆叠网络层,不经过非线性激活函数激活,学到的仍然是线性关系. 但是加入激活函数可以学到非线性的关系,就具有更强的能力去进行特征提取. 构造数据 import torch import torch.nn.functional as F from torch.autograd import Variable import matplotlib.pyplot as plt x = torch.linspace(-5, 5, 200) #

  • 基于python及pytorch中乘法的使用详解

    numpy中的乘法 A = np.array([[1, 2, 3], [2, 3, 4]]) B = np.array([[1, 0, 1], [2, 1, -1]]) C = np.array([[1, 0], [0, 1], [-1, 0]]) A * B : # 对应位置相乘 np.array([[ 1, 0, 3], [ 4, 3, -4]]) A.dot(B) : # 矩阵乘法 ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim

  • pytorch 常用函数 max ,eq说明

    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(ou

  • pytorch常用函数定义及resnet模型修改实例

    目录 模型定义常用函数 利用nn.Parameter()设计新的层 nn.Sequential nn.ModuleList() nn.ModuleDict() nn.Flatten 模型修改案例 修改模型层 添加外部输入 模型定义常用函数 利用nn.Parameter()设计新的层 import torch from torch import nn class MyLinear(nn.Module): def __init__(self, in_features, out_features):

  • pytorch常用函数之torch.randn()解读

    目录 pytorch常用函数torch.randn() pytorch torch.chunk(tensor, chunks, dim) 总结 pytorch常用函数torch.randn() torch.randn(*sizes, out=None) → Tensor 功能:从标准正态分布(均值为0,方差为1)中抽取的一组随机数.返回一个张量 sizes (int…) - 整数序列,定义输出张量的形状 out (Tensor, optinal) - 结果张量 eg: random = torc

  • 浅谈pytorch中torch.max和F.softmax函数的维度解释

    在利用torch.max函数和F.Ssoftmax函数时,对应该设置什么维度,总是有点懵,遂总结一下: 首先看看二维tensor的函数的例子: import torch import torch.nn.functional as F input = torch.randn(3,4) print(input) tensor([[-0.5526, -0.0194, 2.1469, -0.2567], [-0.3337, -0.9229, 0.0376, -0.0801], [ 1.4721, 0.1

  • jQuery常用知识点总结以及平时封装常用函数

    本文为大家介绍了jQuery中常用知识点及函数,包含许多细节方面的知识,下面我们一起学习一下. jQuery中为我们提供了很多有用的属性,自己总结的一些常用的函数.个人认为在在线排盘开发中会比较常用的,仅供大家学习和参考. 刚开始学习前端的时候开始整理这个文档,现在内容已经逐渐增多.虽然现在看起来,文档里的内容非常简单,但是看着这些内容,好像还依稀记得这一行行代码当时被记录的情景.所以我想把这段回忆保存起来,为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. ** 此文

  • JS常用函数和常用技巧小结

    学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. Ajax请求 jquery ajax函数 我自己封装了一个ajax的函数,代码如下: var Ajax = function(url, type success, error) { $.ajax({ url: url, type: type, dataType: 'json', timeout: 10000, success: fu

  • 基于Java中Math类的常用函数总结

    Java中比较常用的几个数学公式的总结: //取整,返回小于目标函数的最大整数,如下将会返回-2 Math.floor(-1.8): //取整,返回发育目标数的最小整数 Math.ceil() //四舍五入取整 Math.round() //计算平方根 Math.sqrt() //计算立方根 Math.cbrt() //返回欧拉数e的n次幂 Math.exp(3); //计算乘方,下面是计算3的2次方 Math.pow(3,2); //计算自然对数 Math.log(); //计算绝对值 Mat

  • oracle常用函数汇总(分享)

    一.运算符算术运算符:+ - * / 可以在select 语句中使用连接运算符:|| select deptno|| dname from dept; 比较运算符:> >= = != < <= like between is null in逻辑运算符:not and or 集合运算符: intersect ,union, union all, minus 要求:对应集合的列数和数据类型相同     查询中不能包含long 列     列的标签是第一个集合的标签     使用orde

  • PHP常用函数总结(180多个)

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 浮点数进一取整 3.floor(): 舍去法取整 echo floor(9.999); // 9 浮点数直接舍去小数部分 4.fmod(): 浮点数取余 $x = 5.7; $y = 1.3; // 两个浮点数,x>y 浮点余数 $r = fmod($x, $y); // $r equals 0.

随机推荐