Pytorch學(xué)習(xí)之torch用法----比較操作(Comparison Ops)
1. torch.eq(input, other, out=None)
說明: 比較元素是否相等,第二個(gè)參數(shù)可以是一個(gè)數(shù),或者是第一個(gè)參數(shù)同類型形狀的張量
參數(shù):
input(Tensor) ---- 待比較張量
other(Tenosr or float) ---- 比較張量或者數(shù)
out(Tensor,可選的) ---- 輸出張量
返回值: 一個(gè)torch.ByteTensor張量,包含了每個(gè)位置的比較結(jié)果(相等為1,不等為0)
>>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.eq(a, b) tensor([[1, 0], [0, 1]], dtype=torch.uint8)
2. torch.equal(tensor1, tensor2, out=None)
說明: 如果兩個(gè)張量有相同的形狀和元素值,則返回true,否則False
參數(shù):
tensor1(Tenosr) ---- 比較張量1
tensor2(Tensor) ---- 比較張量2
out(Tensor,可選的) ---- 輸出張量
>>> a = torch.Tensor([1, 2]) >>> b = torch.Tensor([1, 2]) >>> torch.equal(a, b) True
3. torch.ge(input, other, out=None)
說明: 逐元素比較input和other,即是否input >= other。
參數(shù):
input(Tensor) ---- 待對(duì)比的張量
other(Tensor or float) ---- 對(duì)比的張量或float值
out(Tensor,可選的) ---- 輸出張量,
>>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.ge(a, b) tensor([[1, 1], [0, 1]], dtype=torch.uint8)
4. torch.gt(input, other, out=None)
說明: 逐元素比較input和other,即是否input > other
參數(shù):
input(Tensor) ---- 要對(duì)比的張量
other(Tensor or float) ---- 要對(duì)比的張量或float值
out(Tensor,可選的) ---- 輸出張量
>>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.gt(a, b) tensor([[0, 1], [0, 0]], dtype=torch.uint8)
5. torch.kthvalue(input, k, dim=None, out=None)
說明: 取輸入張量input指定維度上第k個(gè)最小值。如果不指定dim。默認(rèn)為最后一維。返回一個(gè)元組(value, indices), 其中indices是原始輸入張量中沿dim維的第k個(gè)最小值下標(biāo)。
參數(shù):
input(Tensor) ---- 要對(duì)比的張量
k(int) ---- 第k個(gè)最小值
dim(int, 可選的) ---- 沿著此維度進(jìn)行排序
out(tuple,可選的) ---- 輸出元組
>>> x = torch.arange(1, 6) >>> x tensor([1, 2, 3, 4, 5]) >>> torch.kthvalue(x, 4) torch.return_types.kthvalue( values=tensor(4), indices=tensor(3)) >>> torch.kthvalue(x, 1) torch.return_types.kthvalue( values=tensor(1), indices=tensor(0))
6. torch.le(input, other, out=None)
說明: 逐元素比較input和other,即是否input <= other.
參數(shù):
input(Tenosr) ---- 要對(duì)比的張量
other(Tensor or float) ---- 對(duì)比的張量或float值
out(Tensor,可選的) ---- 輸出張量
>>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.le(a, b) tensor([[1, 0], [1, 1]], dtype=torch.uint8)
7. torch.lt(input, other, out=None)
說明: 逐元素比較input和other,即是否input < other
參數(shù):
input(Tensor) ---- 要對(duì)比的張量
other(Tensor or float) ---- 對(duì)比的張量或float值
out(Tensor,可選的) ---- 輸出張量
>>> a = torch.Tensor([[1, 2], [3, 4]]) >>> b = torch.Tensor([[1, 1], [4, 4]]) >>> torch.lt(a, b) tensor([[0, 0], [1, 0]], dtype=torch.uint8)
8. torch.max(input)
說明: 返回輸入張量所有元素的最大值
參數(shù):
input(Tensor) ---- 輸入張量
>>> a = torch.randn(1, 3) >>> a tensor([[ 0.1553, -0.4140, 1.8393]]) >>> torch.max(a) tensor(1.8393)
9. torch.max(input, dim, max=None, max_indices=None)
說明: 返回輸入張量給定維度上每行的最大值,并同時(shí)返回每個(gè)最大值的位置索引。
參數(shù):
input(Tensor) ---- 輸入張量
dim(int) ---- 指定的維度
max(Tensor,可選的) ---- 結(jié)果張量,包含給定維度上的最大值
max_indices(LongTensor,可選的) ---- 結(jié)果張量,包含給定維度上每個(gè)最大值的位置的索引。
>>> a = torch.randn(4, 4) >>> a tensor([[ 0.4067, -0.7722, -0.6560, -0.9621], [-0.8754, 0.0282, -0.7947, -0.1870], [ 0.4300, 0.5444, 0.3180, 1.2647], [ 0.0775, 0.5886, 0.1662, 0.8986]]) >>> torch.max(a, 1) torch.return_types.max( values=tensor([0.4067, 0.0282, 1.2647, 0.8986]), indices=tensor([0, 1, 3, 3]))
10. torch.max(input, other, out=None)
說明: 返回兩個(gè)元素的最大值。
參數(shù):
input(Tensor) ---- 待比較張量
other(Tensor) ---- 比較張量
out(Tensor,可選的) ---- 結(jié)果張量
>>> a = torch.randn(4) >>> a tensor([ 0.5767, -1.0841, -0.0942, -0.9405]) >>> b = torch.randn(4) >>> b tensor([-0.6375, 1.4165, 0.2738, -0.8996]) >>> torch.max(a, b) tensor([ 0.5767, 1.4165, 0.2738, -0.8996])
11.torch.min(input)
說明: 返回輸入張量所有元素的最小值
參數(shù):
input(Tensor) ---- 輸入張量
>>> a = torch.randn(1, 4) >>> a tensor([[-0.8142, -0.9847, -0.3637, 0.5191]]) >>> torch.min(a) tensor(-0.9847)
12. torch.min(input, dim, min=None, min_indices=None)
說明: 返回輸入張量給定維度上每行的最小值,并同時(shí)返回每個(gè)最小值的位置索引
參數(shù):
input(Tensor) ---- 輸入張量
dim(int) ---- 指定的維度
min(Tensor,可選的) ---- 結(jié)果張量,包含給定維度上的最小值
min_indices(LongTensor,可選的) ---- 結(jié)果張量,包含給定維度上每個(gè)最小值的位置索引。
>>> 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。第二個(gè)參數(shù)可以為一個(gè)數(shù)或與第一個(gè)參數(shù)相同形狀和類型的張量
參數(shù):
input(Tensor) ---- 待對(duì)比的張量
other(Tensor or float) ---- 對(duì)比的張量或float值
out(Tensor, 可選的) ---- 輸出張量
** 返回值:** 一個(gè)torch.ByteTensor 張量,包含了每個(gè)位置的比較結(jié)果,如果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)
說明: 對(duì)輸入張量input沿指定維度按升序排序,如果不給定dim,則默認(rèn)為輸入的最后一維。如果指定參數(shù)descending為True,則按降序排序。
參數(shù):
input(Tensor) ---- 要排序的張量
dim(int,可選的) ---- 沿著此維度排序
descending(bool,可選的) ---- 布爾值,控制升序排序
out(tuple,可選的) ---- 輸出張量
返回值: 為ByteTensor類型或與tensor相同類型,為元組(sorted_tensor,sorted_indices),sorted_indices為原始輸入中的下標(biāo)
>>> 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個(gè)最大值。如果不指定dim,則默認(rèn)input的最后一維,如果largest為False,則返回最小的k個(gè)值。
參數(shù):
input(Tensor) ---- 輸入張量
k(int) ---- “top-k"中的k值
dim(int,可選的) ---- 排序的維度
largest(bool,可選的) ---- 布爾值,控制返回最大或最小值
sorted(bool,可選的) ---- 布爾值,控制返回值是否排序
out(tuple,可選的) ---- 可選輸出張量
返回值: 返回一個(gè)元組(values, indices),其中indices是原始輸入張量input中排序元素下標(biāo)。如果設(shè)定布爾值sorted為True,將會(huì)確保返回的k個(gè)值被排序
>>> x = torch.arange(1, 6) >>> x tensor([1, 2, 3, 4, 5]) >>> torch.topk(x, 3) torch.return_types.topk( values=tensor([5, 4, 3]), indices=tensor([4, 3, 2])) >>> torch.topk(x, 3, 0, largest=False) torch.return_types.topk( values=tensor([1, 2, 3]), indices=tensor([0, 1, 2]))
以上這篇Pytorch學(xué)習(xí)之torch用法----比較操作(Comparison Ops)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實(shí)現(xiàn)簡易的植物識(shí)別小系統(tǒng)
這篇文章主要介紹了利用Python實(shí)現(xiàn)一個(gè)簡易的植物識(shí)別系統(tǒng),文中的示例代碼簡潔易懂,對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的小伙伴可以參考一下2021-12-12Python Unittest自動(dòng)化單元測試框架詳解
這篇文章主要為大家詳細(xì)介紹了Python Unittest自動(dòng)化單元測試框架的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Python用20行代碼實(shí)現(xiàn)完整郵件功能
這篇文章主要介紹了如何使用Python實(shí)現(xiàn)完整郵件功能的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到您2021-09-09Blender Python編程實(shí)現(xiàn)程序化建模生成超形示例詳解
這篇文章主要為大家介紹了Blender Python編程實(shí)現(xiàn)程序化建模生成超形示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Anaconda使用IDLE的實(shí)現(xiàn)示例
這篇文章主要介紹了Anaconda使用IDLE的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09