PyTorch中torch.nn.functional.cosine_similarity使用詳解
概述
根據(jù)官網(wǎng)文檔的描述,其中 dim表示沿著對應(yīng)的維度計算余弦相似。那么怎么理解呢?
首先,先介紹下所謂的dim:
a = torch.tensor([[ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ], dtype=torch.float) print(a.shape) """ [ [ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ] """
假設(shè)有2個矩陣:[[1, 2], [3, 4]] 和 [[5, 6], [7, 8]]
, 求2者的余弦相似。
按照dim=0求余弦相似:
import torch.nn.functional as F input1 = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) input2 = torch.tensor([[5, 6], [7, 8]], dtype=torch.float) output = F.cosine_similarity(input1, input2, dim=0) print(output)
結(jié)果如下:
tensor([0.9558, 0.9839])
那么,這個數(shù)值是怎么得來的?是按照
具體求解如下:
print(F.cosine_similarity(torch.tensor([1,3], dtype=torch.float) , torch.tensor([5,7], dtype=torch.float), dim=0)) print(F.cosine_similarity(torch.tensor([2,4], dtype=torch.float) , torch.tensor([6,8], dtype=torch.float), dim=0))
運行結(jié)果如下:
tensor(0.9558)tensor(0.9839)
可以用scipy.spatial
進一步佐證:
from scipy import spatial dataSetI = [1,3] dataSetII = [5,7] result = 1 - spatial.distance.cosine(dataSetI, dataSetII) print(result)
運行結(jié)果如下:
0.95577900872195
同理:
dataSetI = [2,4] dataSetII = [6,8] result = 1 - spatial.distance.cosine(dataSetI, dataSetII) print(result)
運行結(jié)果如下:
0.9838699100999074
按照dim=1求余弦相似:
output = F.cosine_similarity(input1, input2, dim=1) print(output)
運行結(jié)果如下:
tensor([0.9734, 0.9972])
同理,用用scipy.spatial
進一步佐證:
dataSetI = [1,2] dataSetII = [5,6] result = 1 - spatial.distance.cosine(dataSetI, dataSetII) print(result)
運行結(jié)果:0.973417168333576
dataSetI = [3,4] dataSetII = [7,8] result = 1 - spatial.distance.cosine(dataSetI, dataSetII) print(result)
運行結(jié)果:
0.9971641204866132
結(jié)果與F.cosine_similarity
相符合。
補充:給定一個張量,計算多個張量與它的余弦相似度,并將計算得到的余弦相似度標準化。
import torch def get_att_dis(target, behaviored): attention_distribution = [] for i in range(behaviored.size(0)): attention_score = torch.cosine_similarity(target, behaviored[i].view(1, -1)) # 計算每一個元素與給定元素的余弦相似度 attention_distribution.append(attention_score) attention_distribution = torch.Tensor(attention_distribution) return attention_distribution / torch.sum(attention_distribution, 0) # 標準化 a = torch.FloatTensor(torch.rand(1, 10)) print('a', a) b = torch.FloatTensor(torch.rand(3, 10)) print('b', b) similarity = get_att_dis(target=a, behaviored=b) print('similarity', similarity)
a tensor([[0.9255, 0.2194, 0.8370, 0.5346, 0.5152, 0.4645, 0.4926, 0.9882, 0.2783,
0.9258]])
b tensor([[0.6874, 0.4054, 0.5739, 0.8017, 0.9861, 0.0154, 0.8513, 0.8427, 0.6669,
0.0694],
[0.1720, 0.6793, 0.7764, 0.4583, 0.8167, 0.2718, 0.9686, 0.9301, 0.2421,
0.0811],
[0.2336, 0.4783, 0.5576, 0.6518, 0.9943, 0.6766, 0.0044, 0.7935, 0.2098,
0.0719]])
similarity tensor([0.3448, 0.3318, 0.3234])
總結(jié)
到此這篇關(guān)于PyTorch中torch.nn.functional.cosine_similarity使用的文章就介紹到這了,更多相關(guān)PyTorch torch.nn.functional.cosine_similarity使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python中居然可以定義兩個同名通參數(shù)的函數(shù)
今天小編就為大家分享一篇在Python中居然可以定義兩個同名通參數(shù)的函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python3.7安裝matplotlib失敗問題的完美解決方法
由于學習需要安裝matplotlib庫,閱讀網(wǎng)上教程后一直出現(xiàn)各種各樣的錯誤,下面這篇文章主要給大家介紹了關(guān)于python3.7安裝matplotlib失敗問題的完美解決方法,需要的朋友可以參考下2022-07-07pandas探索你的數(shù)據(jù)實現(xiàn)可視化示例詳解
這篇文章主要為大家介紹了pandas探索你的數(shù)據(jù)實現(xiàn)可視化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Pygame實現(xiàn)游戲最小系統(tǒng)功能詳解
這篇文章主要介紹了Pygame實現(xiàn)游戲最小系統(tǒng),Pygame是一個專門用來開發(fā)游戲的 Python 模塊,主要為開發(fā)、設(shè)計 2D 電子游戲而生,具有免費、開源,支持多種操作系統(tǒng),具有良好的跨平臺性等優(yōu)點2022-11-11解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題
今天小編就為大家分享一篇解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07