即使向量相反,Keras Dot 也能给出正余弦相似度

Keras Dot giving positive cosine similiarity even when the vectors are opposite

我正在尝试查看来自 keras 的点功能。令我惊讶的是 +ve 和 -ve 预期的余弦相似度(-1 到 +1),此处显示的余弦相似度为 0.9999。如果向量像下面的例子那样正交相反,我们不应该得到 -1 吗?

张量流 2.

import tensorflow as tf
import numpy as np
x=np.asarray([-1.,-1.,-1],dtype=float)
x=np.reshape(x,(1,3))
y=np.asarray([1.,1.,1.],dtype=float)
y=np.reshape(x,(1,3))
tf.keras.layers.Dot(axes=-1,normalize=True)([x,y]).numpy()[0][0]
0.99999994

真的有点爱上了。你有

y=np.reshape(x,(1,3))

应该是,

y=np.reshape(y,(1,3))