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

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
实时数仓Hologres,5000CU*H 100GB 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
132
分享
相关文章
【pytorch】【202504】关于torch.nn.Linear
小白从开始这段代码展示了`nn.Linear`的使用及其背后的原理。 此外,小白还深入研究了PyTorch的核心类`torch.nn.Module`以及其子类`torch.nn.Linear`的源码。`grad_fn`作为张量的一个属性,用于指导反向传播 进一步地,小白探讨了`requires_grad`与叶子节点(leaf tensor)的关系。叶子节点是指在计算图中没有前驱操作的张量,只有设置了`requires_grad=True`的叶子节点才会在反向传播时保存梯度。 最后,小白学习了PyTorch中的三种梯度模式 通过以上学习小白对PyTorch的自动求导机制有了更深刻的理解。
68 6
Pytorch学习笔记(二):nn.Conv2d()函数详解
这篇文章是关于PyTorch中nn.Conv2d函数的详解,包括其函数语法、参数解释、具体代码示例以及与其他维度卷积函数的区别。
977 0
Pytorch学习笔记(二):nn.Conv2d()函数详解
tf.keras.layers.LSTM/tf.keras.layers.LSTMCell
【8月更文挑战第20天】tf.keras.layers.LSTM/tf.keras.layers.LSTMCell。
63 1
tf.keras.layers.Conv2D
【8月更文挑战第20天】tf.keras.layers.Conv2D。
112 2
|
9月前
tf.keras.layers.Dense
【8月更文挑战第20天】tf.keras.layers.Dense。
180 2
|
9月前
tf.keras.layers.MaxPooling2D/AveragePooling2D
【8月更文挑战第19天】tf.keras.layers.MaxPooling2D/AveragePooling2D。
71 0
【Tensorflow+Keras】tf.keras.layers.Bidirectional()的解析与使用
本文解析了TensorFlow和Keras中的`tf.keras.layers.Bidirectional()`层,它用于实现双向RNN(如LSTM、GRU)的神经网络结构。文章详细介绍了该层的参数配置,并通过实例演示了如何构建含有双向LSTM层的模型,以及如何使用IMDB数据集进行模型训练和评估。
200 8
Torch
Torch是一个用于构建深度学习模型的开源机器学习库,它基于Lua编程语言。然而,由于PyTorch的出现,现在通常所说的"torch"指的是PyTorch。PyTorch是一个基于Torch的Python库,它提供了一个灵活而高效的深度学习框架。
403 1