判等操作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])