Pytorch学习之torch用法----比较操作(Comparison Ops)
2020-06-28 12:01:17 来源:易采站长站 作者:王旭
min_indices(LongTensor,可选的) ---- 结果张量,包含给定维度上每个最小值的位置索引。
>>> a = torch.randn(4, 4) >>> a tensor([[-0.0243, -0.7382, 0.3102, 0.9720], [-0.3805, -0.7999, -1.2856, 0.2657], [-1.0284, -0.1638, -0.8840, 1.2679], [-1.0347, -2.3428, 0.3107, 1.0575]]) >>> torch.min(a, 1) torch.return_types.min( values=tensor([-0.7382, -1.2856, -1.0284, -2.3428]), indices=tensor([1, 2, 0, 1]))
13. torch.ne(input, other, out=None)
说明: 逐元素比较input和other,即是否input 不等于 other。第二个参数可以为一个数或与第一个参数相同形状和类型的张量
参数:
input(Tensor) ---- 待对比的张量
other(Tensor or float) ---- 对比的张量或float值
out(Tensor, 可选的) ---- 输出张量
** 返回值:** 一个torch.ByteTensor 张量,包含了每个位置的比较结果,如果tensor和other不相等为True,返回1.
>>> import torch >>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.ne(a, b) tensor([[0, 1], [1, 0]], dtype=torch.uint8)
14. torch.sort(input, dim=None, descending=False, out=None)
说明: 对输入张量input沿指定维度按升序排序,如果不给定dim,则默认为输入的最后一维。如果指定参数descending为True,则按降序排序。
参数:
input(Tensor) ---- 要排序的张量
dim(int,可选的) ---- 沿着此维度排序
descending(bool,可选的) ---- 布尔值,控制升序排序
out(tuple,可选的) ---- 输出张量
返回值: 为ByteTensor类型或与tensor相同类型,为元组(sorted_tensor,sorted_indices),sorted_indices为原始输入中的下标
>>> x = torch.randn(3, 4) >>> x tensor([[-0.3613, -0.2583, -0.4276, -1.3106], [-1.1577, -0.7505, 1.7217, -0.6247], [-0.1338, 0.4423, 0.0280, -1.4796]]) >>> sorted, indices = torch.sort(x) >>> sorted tensor([[-1.3106, -0.4276, -0.3613, -0.2583], [-1.1577, -0.7505, -0.6247, 1.7217], [-1.4796, -0.1338, 0.0280, 0.4423]]) >>> indices tensor([[3, 2, 0, 1], [0, 1, 3, 2], [3, 0, 2, 1]])
15. torch.topk(input, dim=None, largest=True, sorted=True, out=None)
说明: 沿指定dim维度返回输入张量input中k个最大值。如果不指定dim,则默认input的最后一维,如果largest为False,则返回最小的k个值。
参数:
input(Tensor) ---- 输入张量
k(int) ---- “top-k"中的k值
dim(int,可选的) ---- 排序的维度
largest(bool,可选的) ---- 布尔值,控制返回最大或最小值
sorted(bool,可选的) ---- 布尔值,控制返回值是否排序
out(tuple,可选的) ---- 可选输出张量
返回值: 返回一个元组(values, indices),其中indices是原始输入张量input中排序元素下标。如果设定布尔值sorted为True,将会确保返回的k个值被排序













闽公网安备 35020302000061号