pytorch 常用函數(shù) max ,eq說明
max找出tensor 的行或者列最大的值:
找出每行的最大值:
import torch outputs=torch.FloatTensor([[1],[2],[3]]) print(torch.max(outputs.data,1))
輸出:
(tensor([ 1., 2., 3.]), tensor([ 0, 0, 0]))
找出每列的最大值:
import torch outputs=torch.FloatTensor([[1],[2],[3]]) print(torch.max(outputs.data,0))
輸出結(jié)果:
(tensor([ 3.]), tensor([ 2]))
Tensor比較eq相等:
import torch outputs=torch.FloatTensor([[1],[2],[3]]) targets=torch.FloatTensor([[0],[2],[3]]) print(targets.eq(outputs.data))
輸出結(jié)果:
tensor([[ 0], [ 1], [ 1]], dtype=torch.uint8)
使用sum() 統(tǒng)計相等的個數(shù):
import torch outputs=torch.FloatTensor([[1],[2],[3]]) targets=torch.FloatTensor([[0],[2],[3]]) print(targets.eq(outputs.data).cpu().sum())
輸出結(jié)果:
tensor(2)
補充知識:PyTorch - torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le
flyfish
torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le
以上全是簡寫
參數(shù)是input, other, out=None
逐元素比較input和other
返回是torch.BoolTensor
import torch a=torch.tensor([[1, 2], [3, 4]]) b=torch.tensor([[1, 2], [4, 3]]) print(torch.eq(a,b))#equals # tensor([[ True, True], # [False, False]]) print(torch.ne(a,b))#not equal to # tensor([[False, False], # [ True, True]]) print(torch.gt(a,b))#greater than # tensor([[False, False], # [False, True]]) print(torch.lt(a,b))#less than # tensor([[False, False], # [ True, False]]) print(torch.ge(a,b))#greater than or equal to # tensor([[ True, True], # [False, True]]) print(torch.le(a,b))#less than or equal to # tensor([[ True, True], # [ True, False]])
以上這篇pytorch 常用函數(shù) max ,eq說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于python 將列表作為參數(shù)傳入函數(shù)時的測試與理解
這篇文章主要介紹了基于python 將列表作為參數(shù)傳入函數(shù)時的測試與理解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫
這篇文章主要介紹了python如何使用ctypes庫調(diào)用DLL動態(tài)鏈接庫,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-10-10OpenCV之理解KNN鄰近算法k-Nearest?Neighbour
這篇文章主要為大家介紹了OpenCV之理解KNN鄰近算法k-Nearest?Neighbour,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Python實現(xiàn)按特定格式對文件進行讀寫的方法示例
這篇文章主要介紹了Python實現(xiàn)按特定格式對文件進行讀寫的方法,可實現(xiàn)文件按原有格式讀取與寫入的功能,涉及文件的讀取、遍歷、轉(zhuǎn)換、寫入等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Python中內(nèi)置數(shù)據(jù)類型list,tuple,dict,set的區(qū)別和用法
這篇文章主要給大家介紹了Python中內(nèi)置數(shù)據(jù)類型list,tuple,dict,set的區(qū)別和用法,都是非?;A(chǔ)的知識,十分的細致全面,有需要的小伙伴可以參考下。2015-12-12