日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)

發布時間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 常用函數

比較函數中有一些是逐元素比較,操作類似逐元素操作,還有一些類似歸并操作,常用的比較函數如下表所示。

表中第一行的比較操作已經實現了運算符重載,因此可以使用 a>=ba>ba !=ba == b,其返回的結果是一個 ByteTensor,可用來選取元素。

max/min 操作有些特殊,以 max 為例,有以下三種使用情況:

  • t.max(tensor) : 返回 tensor 中最大的一個數;
  • t.max(tensor,dim) : 指定維上最大數,返回 tensor 和下標;
  • t.max(tensor1,tensor2) : 比較兩個 tensor 相比較大的元素;

2. 使用示例

torch.gttorch.lttorch.getorch.letorch.eqtorch.ne 的函數參數和返回值是類似的,都如下所示:

  • 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 a BoolTensor

  • Returns:
    Tensor: A torch.BoolTensor containing 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

函數參數同 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

函數定義如下:

topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)

參數說明:

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

函數定義如下:

sort(input, dim=-1, descending=False, out=None) -> (Tensor, LongTensor)

函數參數如下:

    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

函數定義如下:

max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

函數參數如下:

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

函數定義如下:

min(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)
min(input, other, out=None) -> Tensor
min(input) -> Tensor

函數參數如下:

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

總結

以上是生活随笔為你收集整理的PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。