Pytorch使用专题 | 1:torch.nn.functional.cosine_similarity使用详解

本文涉及的产品
实时数仓Hologres,5000CU*H 100GB 3个月
实时计算 Flink 版,5000CU*H 3个月
智能开放搜索 OpenSearch行业算法版,1GB 20LCU 1个月
简介: 介绍torch.nn.functional.cosine_similarity的使用

更多、更及时内容欢迎留意微信公众号小窗幽记机器学习

概述

根据官网文档的描述,其中 dim表示沿着对应的维度计算余弦相似。那么怎么理解呢?
首先,先介绍下所谓的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]
    ]
]
"""
AI 代码解读

image.png

假设有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)
AI 代码解读

结果如下:

tensor([0.9558, 0.9839])
AI 代码解读

那么,这个数值是怎么得来的?是按照

image.png

具体求解如下:

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))
AI 代码解读

运行结果如下:

tensor(0.9558)
tensor(0.9839)
AI 代码解读

可以用scipy.spatial进一步佐证:

from scipy import spatial

dataSetI = [1,3]
dataSetII = [5,7]
result = 1 - spatial.distance.cosine(dataSetI, dataSetII)
print(result)
AI 代码解读

运行结果如下:

0.95577900872195
AI 代码解读

同理:

dataSetI = [2,4]
dataSetII = [6,8]
result = 1 - spatial.distance.cosine(dataSetI, dataSetII)
print(result)
AI 代码解读

运行结果如下:

0.9838699100999074
AI 代码解读

按照dim=1求余弦相似:

output = F.cosine_similarity(input1, input2, dim=1)
print(output)
AI 代码解读

运行结果如下:

tensor([0.9734, 0.9972])
AI 代码解读

同理,用用scipy.spatial进一步佐证:

dataSetI = [1,2]
dataSetII = [5,6]
result = 1 - spatial.distance.cosine(dataSetI, dataSetII)
print(result)
AI 代码解读

运行结果:0.973417168333576

dataSetI = [3,4]
dataSetII = [7,8]
result = 1 - spatial.distance.cosine(dataSetI, dataSetII)
print(result)
AI 代码解读

运行结果:

0.9971641204866132
AI 代码解读

结果与F.cosine_similarity相符合。

【更多、更及时内容欢迎留意微信公众号小窗幽记机器学习

目录
打赏
0
0
0
0
131
分享
相关文章
tf.keras.layers.LSTM/tf.keras.layers.LSTMCell
【8月更文挑战第20天】tf.keras.layers.LSTM/tf.keras.layers.LSTMCell。
54 1
|
7月前
tf.keras.layers.Dense
【8月更文挑战第20天】tf.keras.layers.Dense。
133 2
tf.keras.layers.Conv2D
【8月更文挑战第20天】tf.keras.layers.Conv2D。
75 2
|
7月前
tf.keras.layers.MaxPooling2D/AveragePooling2D
【8月更文挑战第19天】tf.keras.layers.MaxPooling2D/AveragePooling2D。
63 0
|
7月前
tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like()
【8月更文挑战第11天】tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like()。
69 5
【Tensorflow+Keras】tf.keras.layers.Bidirectional()的解析与使用
本文解析了TensorFlow和Keras中的`tf.keras.layers.Bidirectional()`层,它用于实现双向RNN(如LSTM、GRU)的神经网络结构。文章详细介绍了该层的参数配置,并通过实例演示了如何构建含有双向LSTM层的模型,以及如何使用IMDB数据集进行模型训练和评估。
151 8
Torch
Torch是一个用于构建深度学习模型的开源机器学习库,它基于Lua编程语言。然而,由于PyTorch的出现,现在通常所说的"torch"指的是PyTorch。PyTorch是一个基于Torch的Python库,它提供了一个灵活而高效的深度学习框架。
337 1
【PyTorch简明教程】torch.Tensor()与torch.tensor()的区别
【PyTorch简明教程】torch.Tensor()与torch.tensor()的区别
165 0