首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在张量流中实现余弦相似度

在张量流中实现余弦相似度
EN

Stack Overflow用户
提问于 2019-05-09 03:27:29
回答 1查看 1.3K关注 0票数 0

我的问题是关于下面的方程式

上面的单向量方程。但是如果我有一批向量,比如我的X和Y的维数是(None,32),那么就会有一些问题。

还要记住,在编码环境中,批处理中的一个示例已经处于转置状态。我的问题是,当我们需要对None,32进行转置时,代码将不会接受和转置为None dimenation.So,我用以下方式解决了它:

代码语言:javascript
复制
def Cosine_similarity(X, Y, feature_dim):

  L = tf.compat.v1.initializers.glorot_normal()(shape=[feature_dim, feature_dim])

  out1 = tf.matmul(X, L)
  out2 = tf.matmul(Y, L)

  out_numerator = tf.reduce_sum(tf.multiply(out1, out2), axis = 1)

  out3 = tf.reduce_sum(tf.multiply(out1, out1), axis = 1)
  out3 = tf.sqrt(out3)

  out4 = tf.reduce_sum(tf.multiply(out2, out2), axis = 1)
  out4 = tf.sqrt(out4)

  out_denominator = tf.multiply(out3, out4)

  final_out = tf.divide(out_numerator, out_denominator)

return final_out

这来自于以下内容:

代码语言:javascript
复制
<XA.YA> = (XA)^T (YA)

        = tf.reduce_sum(tf.multiply((X A) , (Y A)), axis = 1)

所以我只想知道这个实现是否正确?或者,如果我遗漏了什么,你可以纠正我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-09 05:54:19

我不确定我是否理解您对(无)维度的担忧。

如果我理解正确的话,两个形状相同的矩阵XY ([batch, target_dim])之间的余弦相似性只是X * Y^T与一些L2归一化的矩阵乘法。注意:X将是您的out1Y将是您的out2

代码语言:javascript
复制
def Cosine_similarity(x, y, A):
  """Pair-wise Cosine similarity.

  First `x` and `y` are transformed by A.
  `X = xA^T` with shape [batch, target_dim],
  `Y = yA^T` with shape [batch, target_dim].

  Args:
    x: shaped [batch, feature_dim].
    y: shaped [batch, feature_dim].
    A: shaped [targte_dim, feature_dim]. Transformation matrix to project
      from `feature_dim` to `target_dim`.

  Returns:
    A cosine similarity matrix shaped [batch, batch]. The entry
    at (i, j) is the cosine similarity value between vector `X[i, :]` and
    `Y[j, :]` where `X`, `Y` are the transformed `x` and y` by `A` 
    respectively. In the other word, entry at (i, j) is the pair-wise 
    cosine similarity value between the i-th example of `x` and the j-th 
    example of `y`.
  """

  x = tf.matmul(x, A, transpose_b=True)
  y = tf.matmul(y, A, transpose_b=True)
  x_norm = tf.nn.l2_normalize(x, axis=-1)
  y_norm = tf.nn.l2_normalize(y, axis=-1)
  y_norm_trans = tf.transpose(y_norm, [1, 0])
  sim = tf.matmul(x_norm, y_norm_trans)
  return sim

import numpy as np

feature_dim = 8
target_dim = 4
batch_size = 2
x = tf.placeholder(tf.float32, shape=(None, dim))
y = tf.placeholder(tf.float32, shape=(None, dim))
A = tf.placeholder(tf.float32, shape=(target_dim, feature_dim))

sim = Cosine_similarity(x, y, A)

with tf.Session() as sess:
  x, y, sim = sess.run([x, y, sim], feed_dict={
      x: np.ones((batch_size, feature_dim)), 
      y: np.random.rand(batch_size, feature_dim),
      A: np.random.rand(target_dim, feature_dim)})
  print 'x=\n', x
  print 'y=\n', y
  print 'sim=\n', sim

结果:

代码语言:javascript
复制
x=
[[ 1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.]]
y=
[[ 0.01471654  0.76577073  0.97747731  0.06429122  0.91344446  0.47987637
   0.09899797  0.773938  ]
 [ 0.8555786   0.43403915  0.92445409  0.03393625  0.30154493  0.60895061
   0.1233703   0.58597666]]
sim=
[[ 0.95917791  0.98181278]
 [ 0.95917791  0.98181278]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56047777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档