PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函數(shù)
比較函數(shù)中有一些是逐元素比較,操作類似逐元素操作,還有一些類似歸并操作,常用的比較函數(shù)如下表所示。
表中第一行的比較操作已經(jīng)實現(xiàn)了運算符重載,因此可以使用 a>=b,a>b ,a !=b 和 a == b,其返回的結(jié)果是一個 ByteTensor,可用來選取元素。
max/min 操作有些特殊,以 max 為例,有以下三種使用情況:
t.max(tensor): 返回tensor中最大的一個數(shù);t.max(tensor,dim): 指定維上最大數(shù),返回tensor和下標;t.max(tensor1,tensor2): 比較兩個tensor相比較大的元素;
2. 使用示例
torch.gt、torch.lt、torch.ge、torch.le、torch.eq、torch.ne 的函數(shù)參數(shù)和返回值是類似的,都如下所示:
-
Args:
input (Tensor): the tensor to compare
other (Tensor or float): the tensor or value to compare
out (Tensor, optional): the output tensor that must be aBoolTensor -
Returns:
Tensor: Atorch.BoolTensorcontaining a True at each location where comparison is true
2.1 torch.gt
In [1]: import torch as tIn [2]: a = t.Tensor([[1,2],[3,4]])In [3]: a
Out[3]:
tensor([[1., 2.],[3., 4.]])In [4]: a.gt(4)
Out[4]:
tensor([[False, False],[False, False]])In [7]: a.gt(t.Tensor([[1,1], [3, 3]]))
Out[7]:
tensor([[False, True],[False, True]])
2.2 torch.lt
函數(shù)參數(shù)同 torch.gt。
In [9]: a.lt(4)
Out[9]:
tensor([[ True, True],[ True, False]])In [10]: a.lt(t.Tensor([[1,1], [3, 3]]))
Out[10]:
tensor([[False, False],[False, False]])In [11]:
2.3 torch.ge
In [12]: a.ge(4)
Out[12]:
tensor([[False, False],[False, True]])In [13]: a.ge(t.Tensor([[1,1], [3, 3]]))
Out[13]:
tensor([[True, True],[True, True]])
2.4 torch.le
In [14]: a.le(4)
Out[14]:
tensor([[True, True],[True, True]])In [15]: a.le(t.Tensor([[1,1], [3, 3]]))
Out[15]:
tensor([[ True, False],[ True, False]])In [16]:
2.5 torch.eq
In [16]: a.eq(4)
Out[16]:
tensor([[False, False],[False, True]])In [17]: a.eq(t.Tensor([[1,1], [3, 3]]))
Out[17]:
tensor([[ True, False],[ True, False]])
2.6 torch.ne
In [18]: a.ne(4)
Out[18]:
tensor([[ True, True],[ True, False]])In [19]: a.ne(t.Tensor([[1,1], [3, 3]]))
Out[19]:
tensor([[False, True],[False, True]])
2.7 torch.topk
函數(shù)定義如下:
topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)
參數(shù)說明:
Returns the :attr:`k` largest elements of the given :attr:`input` tensor alonga given dimension.If :attr:`dim` is not given, the last dimension of the `input` is chosen.If :attr:`largest` is ``False`` then the `k` smallest elements are returned.A namedtuple of `(values, indices)` is returned, where the `indices` are the indicesof the elements in the original `input` tensor.The boolean option :attr:`sorted` if ``True``, will make sure that the returned`k` elements are themselves sortedArgs:input (Tensor): the input tensor.k (int): the k in "top-k"dim (int, optional): the dimension to sort alonglargest (bool, optional): controls whether to return largest orsmallest elementssorted (bool, optional): controls whether to return the elementsin sorted orderout (tuple, optional): the output tuple of (Tensor, LongTensor) that can beoptionally given to be used as output buffers
In [21]: a
Out[21]:
tensor([[1., 2.],[3., 4.]])In [22]: a.topk(2)
Out[22]:
torch.return_types.topk(
values=tensor([[2., 1.],[4., 3.]]),
indices=tensor([[1, 0],[1, 0]]))In [24]: a.topk(1, dim=1)
Out[24]:
torch.return_types.topk(
values=tensor([[2.],[4.]]),
indices=tensor([[1],[1]]))In [25]:
2.8 torch.sort
函數(shù)定義如下:
sort(input, dim=-1, descending=False, out=None) -> (Tensor, LongTensor)
函數(shù)參數(shù)如下:
Sorts the elements of the :attr:`input` tensor along a given dimensionin ascending order by value.If :attr:`dim` is not given, the last dimension of the `input` is chosen.If :attr:`descending` is ``True`` then the elements are sorted in descendingorder by value.A namedtuple of (values, indices) is returned, where the `values` are thesorted values and `indices` are the indices of the elements in the original`input` tensor.Args:input (Tensor): the input tensor.dim (int, optional): the dimension to sort alongdescending (bool, optional): controls the sorting order (ascending or descending)out (tuple, optional): the output tuple of (`Tensor`, `LongTensor`) that canbe optionally given to be used as output buffers
In [28]: b = t.randn(2,3)In [29]: b
Out[29]:
tensor([[-0.1936, -1.8862, 0.1491],[ 0.8152, 1.1863, -0.4711]])In [30]: b.sort()
Out[30]:
torch.return_types.sort(
values=tensor([[-1.8862, -0.1936, 0.1491],[-0.4711, 0.8152, 1.1863]]),
indices=tensor([[1, 0, 2],[2, 0, 1]]))In [31]: b.sort(dim=0)
Out[31]:
torch.return_types.sort(
values=tensor([[-0.1936, -1.8862, -0.4711],[ 0.8152, 1.1863, 0.1491]]),
indices=tensor([[0, 0, 1],[1, 1, 0]]))In [32]:
2.9 torch.max
函數(shù)定義如下:
max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
函數(shù)參數(shù)如下:
Args:input (Tensor): the input tensor.dim (int): the dimension to reduce.keepdim (bool): whether the output tensor has :attr:`dim` retained or not. Default: ``False``.out (tuple, optional): the result tuple of two output tensors (max, max_indices)
或者:
Args:input (Tensor): the input tensor.other (Tensor): the second input tensorout (Tensor, optional): the output tensor.
示例如下:
In [2]: import torch as tIn [4]: a = t.Tensor([[1,2], [3,4]])In [5]: a
Out[5]:
tensor([[1., 2.],[3., 4.]])In [7]: a.max()
Out[7]: tensor(4.)In [8]: b = t.Tensor([[2,0], [2,6]])In [9]: a.max(b)
Out[9]:
tensor([[2., 2.],[3., 6.]])In [10]: a.max(dim=0)
Out[10]:
torch.return_types.max(
values=tensor([3., 4.]),
indices=tensor([1, 1]))In [11]: a.max(dim=1)
Out[11]:
torch.return_types.max(
values=tensor([2., 4.]),
indices=tensor([1, 1]))In [12]:
2.10 torch.min
函數(shù)定義如下:
min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
min(input, other, out=None) -> Tensor
min(input) -> Tensor
函數(shù)參數(shù)如下:
Args:input (Tensor): the input tensor.dim (int): the dimension to reduce.keepdim (bool): whether the output tensor has :attr:`dim` retained or not.out (tuple, optional): the tuple of two output tensors (min, min_indices)
或者:
Args:input (Tensor): the input tensor.other (Tensor): the second input tensorout (Tensor, optional): the output tensor.
使用示例:
In [13]: a = t.Tensor([[1,2], [3,4]])In [14]: a
Out[14]:
tensor([[1., 2.],[3., 4.]])In [15]: a.min()
Out[15]: tensor(1.)In [16]: b = t.Tensor([[2,0], [2,6]])In [17]: b
Out[17]:
tensor([[2., 0.],[2., 6.]])In [18]: a.min(b)
Out[18]:
tensor([[1., 0.],[2., 4.]])In [19]: a.min(dim=0)
Out[19]:
torch.return_types.min(
values=tensor([1., 2.]),
indices=tensor([0, 0]))In [20]: a.min(dim=1)
Out[20]:
torch.return_types.min(
values=tensor([1., 3.]),
indices=tensor([0, 0]))In [21]:
總結(jié)
以上是生活随笔為你收集整理的PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个关于倔强的个性签名!
- 下一篇: 盛开头的成语有哪些?