tensor.equal、eq、ge、gt、le、lt、ne、sort、topk、kthvalue、isfinite、isinf、isnan

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

判等操作equal eq

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)

# 会比较shape 和 值是否都相同,相同则只返回一个TRUE或者FALSE
print('\n',torch.equal(a, b))
# 以下的是要对每一个元素进行比较,
print('\n', torch.eq(a, b))

打印结果:


 False

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

比较操作 ge gt le lt ne

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)

print('大于等于\n',torch.ge(a, b))
print('大于\n',torch.gt(a, b))
print('小于等于\n',torch.le(a, b))
print('小于\n',torch.lt(a, b))
print('不等于\n',torch.ne(a, b))

打印结果:

大于等于
 tensor([[False, False, False],
        [ True, False,  True]])
大于
 tensor([[False, False, False],
        [ True, False,  True]])
小于等于
 tensor([[ True,  True,  True],
        [False,  True, False]])
小于
 tensor([[ True,  True,  True],
        [False,  True, False]])
不等于
 tensor([[True, True, True],
        [True, True, True]])

sort


a = torch.tensor([1, 4, 4, 3, 5])
# 输出结果中的indices 为之前的索引值
print('默认升序\n', torch.sort(a))
print('指定为降序\n', torch.sort(a, descending=True))

a = torch.tensor([[1, 4, 4, 3, 5], [0, 6, 2,  3, 5]])
# 输出结果中的indices 为之前的索引值
print('默认升序\n', torch.sort(a, dim=0))  # dim指定维度 0 为按列排序,即每一列按从小到大排序
print('指定为降序\n', torch.sort(a, descending=True))

打印结果:

默认升序
 torch.return_types.sort(
values=tensor([1, 3, 4, 4, 5]),
indices=tensor([0, 3, 1, 2, 4]))
指定为降序
 torch.return_types.sort(
values=tensor([5, 4, 4, 3, 1]),
indices=tensor([4, 1, 2, 3, 0]))
默认升序
 torch.return_types.sort(
values=tensor([[0, 4, 2, 3, 5],
        [1, 6, 4, 3, 5]]),
indices=tensor([[1, 0, 1, 0, 0],
        [0, 1, 0, 1, 1]]))
指定为降序
 torch.return_types.sort(
values=tensor([[5, 4, 4, 3, 1],
        [6, 5, 3, 2, 0]]),
indices=tensor([[4, 1, 2, 3, 0],
        [1, 4, 3, 2, 0]]))

topk

a = torch.tensor([
    [2, 4, 3, 1, 5],
    [2, 3, 5, 1, 4]])
print('a\n', a)
print('a.shape\n', a.shape)
# 从打印结果来看,如果k=1 则值显示一行,这一行中的每一个数字都是其对应的列的最大值
# 如果是k=2 则表示打印两行,这两行也是每一列都是从大到小的排序的
print(torch.topk(a, k=1, dim=0))
print(torch.topk(a, k=2, dim=0))
# 这个返回的为每一行的最大值,返回一列因为k=1
print(torch.topk(a, k=1, dim=1))
# 返回3列
print(torch.topk(a, k=3, dim=1))

打印结果:

a
 tensor([[2, 4, 3, 1, 5],
        [2, 3, 5, 1, 4]])
a.shape
 torch.Size([2, 5])
torch.return_types.topk(
values=tensor([[2, 4, 5, 1, 5]]),
indices=tensor([[0, 0, 1, 0, 0]]))
torch.return_types.topk(
values=tensor([[2, 4, 5, 1, 5],
        [2, 3, 3, 1, 4]]),
indices=tensor([[0, 0, 1, 0, 0],
        [1, 1, 0, 1, 1]]))
torch.return_types.topk(
values=tensor([[5],
        [5]]),
indices=tensor([[4],
        [2]]))
torch.return_types.topk(
values=tensor([[5, 4, 3],
        [5, 4, 3]]),
indices=tensor([[4, 1, 2],
        [2, 4, 1]]))

kthvalue

a = torch.tensor([
    [2, 4, 3, 1, 5],
    [2, 3, 5, 1, 4]])
print('a\n', a)
# 此处的k 的下标是从1开始的不是0开始的
# 从打印结果来看,说明打印出来的,排好序后的第二行数据
print(torch.kthvalue(a, k=2, dim=0))

# 打印出来排好序(升序)的第二列数据
print(torch.kthvalue(a, k=2, dim=1))
# 打印出来排好序(升序)的第一列数据
print(torch.kthvalue(a, k=1, dim=1))

打印结果:

a
 tensor([[2, 4, 3, 1, 5],
        [2, 3, 5, 1, 4]])
torch.return_types.kthvalue(
values=tensor([2, 4, 5, 1, 5]),
indices=tensor([1, 0, 1, 1, 0]))
torch.return_types.kthvalue(
values=tensor([2, 2]),
indices=tensor([0, 0]))
torch.return_types.kthvalue(
values=tensor([1, 1]),
indices=tensor([3, 3]))

判断是否有边界 isfinite isinf isnan

import torch

print('判断是否有界')
a = torch.rand(2, 3)
print('a\n', a)
print('a/0 \n', a/0)
print('判断是否有界\n', torch.isfinite(a))
print('判断是否有界\n', torch.isfinite(a/0))
print('判断是否无界\n', torch.isinf(a/0))
print('判断是否是nan\n', torch.isnan(a))
import numpy as np
# nan是没有直接提供的所以可以通过np来实现
a = torch.tensor([1, 2, np.nan])
print('判断是否是nan\n', torch.isnan(a))

打印结果:

判断是否有界
a
 tensor([[0.4911, 0.4468, 0.5902],
        [0.0684, 0.3986, 0.7524]])
a/0 
 tensor([[inf, inf, inf],
        [inf, inf, inf]])
判断是否有界
 tensor([[True, True, True],
        [True, True, True]])
判断是否有界
 tensor([[False, False, False],
        [False, False, False]])
判断是否无界
 tensor([[True, True, True],
        [True, True, True]])
判断是否是nan
 tensor([[False, False, False],
        [False, False, False]])
判断是否是nan
 tensor([False, False,  True])

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值