python - 计算 Keras 中两个张量之间的余弦相似度

标签 python keras

我一直在关注一个教程,该教程展示了如何制作 word2vec模型。
本教程使用这段代码:similarity = merge([target, context], mode='cos', dot_axes=0) (没有提供其他信息,但我想这来自 keras.layers )
现在,我对 merge 进行了一些研究。方法,但我找不到太多关于它的信息。
据我了解,它已经被很多类似layers.Add(), layers.Concat()...的功能所取代。 .
我应该使用什么?有.Dot() ,它有一个 axis参数(似乎是正确的)但没有 mode范围。
在这种情况下我可以使用什么?

最佳答案

Dot Keras 中的层现在支持内置 余弦相似度使用 normalize = True范围。
来自 Keras 文档:

keras.layers.Dot(axes, normalize=True)

normalize: Whether to L2-normalize samples along the dot product axis before taking the dot product. If set to True, then the output of the dot product is the cosine proximity between the two samples.


Source

关于python - 计算 Keras 中两个张量之间的余弦相似度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003027/

相关文章:

tensorflow - Keras/Tensorflow 中一组矩阵之间的成对距离

python - Python 中的简单上下文管理器

python - 产品变体未反射(reflect) Django 电子商务项目中订单摘要中的更新数量

python - 如何在返回列表项的标签中显示函数的结果?

python - 如何为 TF2.0+ keras CNN 定义用于图像分类的加权损失函数?

python-3.x - 克隆前和克隆后的 keras 模型表现不同

python - 如何在 Tensorflow 中结合 feature_columns、model_to_estimator 和数据集 API

javascript - 如何在 PyQT 5.7 中从 JavaScript 访问 Python 代码?

python - 检查特定字符串是否出现在另一个字符串中

python - 使用 Tensorflow 处理文本数据 2 "ERROR: Attempted to pad to a smaller size than the input element"