【问题标题】:How to calculate cosine similarity given sparse matrix data in TensorFlow?如何在TensorFlow中计算给定稀疏矩阵数据的余弦相似度?
【发布时间】:2020-11-12 07:00:53
【问题描述】:

我应该在 GitHub 网站上更改部分 python 脚本。这段代码是基于注意力的相似度度量,但我想将其转换为余弦相似度。

相应的代码在 layers.py 文件中(在 call 方法内)。

基于注意力的:

  def __call__(self, inputs):
    x = inputs
    # dropout
    if self.sparse_inputs:
        x = sparse_dropout(x, 1-self.dropout, self.num_features_nonzero)
    else:
        x = tf.nn.dropout(x, 1-self.dropout)

    # graph learning
    h = dot(x, self.vars['weights'], sparse=self.sparse_inputs)
    N = self.num_nodes
    edge_v = tf.abs(tf.gather(h,self.edge[0]) - tf.gather(h,self.edge[1]))
    edge_v = tf.squeeze(self.act(dot(edge_v, self.vars['a'])))
    sgraph = tf.SparseTensor(indices=tf.transpose(self.edge), values=edge_v, dense_shape=[N, N])
    sgraph = tf.sparse_softmax(sgraph)
    return h, sgraph

我根据我的要求编辑了上面的代码(余弦相似度)。但是,当我运行以下代码时,如下所示:

def __call__(self, inputs):
    x = inputs
    # dropout
    if self.sparse_inputs:
        x = sparse_dropout(x, 1-self.dropout, self.num_features_nonzero)
    else:
        x = tf.nn.dropout(x, 1-self.dropout)

    # graph learning
    h = dot(x, self.vars['weights'], sparse=self.sparse_inputs)
    N = self.num_nodes
    h_norm = tf.nn.l2_normalize(h)
    edge_v = tf.matmul(h_norm, tf.transpose(h_norm))
    h_norm_1 = tf.norm(h_norm)
    edge_v /= h_norm_1 * h_norm_1
    edge_v = dot(edge_v, self.vars['a']) # It causes an error when I add this line
    zero = tf.constant(0, dtype=tf.float32)
    where = tf.not_equal(edge_v, zero)
    indices = tf.where(where)
    values = tf.gather_nd(edge_v, indices)
    sgraph = tf.SparseTensor(indices, values, dense_shape= [N,N])
    return h, sgraph

脚本显示一些运行时错误:

Screenshot of error message

我怀疑这里的错误与第 226 行有关:

edge_v = dot(edge_v, self.vars['a']) # It causes an error when I add this line

关于如何成功完成此任务的任何警告?

GitHub上脚本的链接:

https://github.com/jiangboahu/GLCN-tf

注意:我不想使用内置函数,因为我认为它们不能精确地完成这项工作。

ETA:似乎有一些答案,但据我所知,它们似乎解决了不同的问题。

提前致谢

【问题讨论】:

    标签: tensorflow deep-learning conv-neural-network cosine-similarity attention-model


    【解决方案1】:

    是什么?方法导入了吗?

    应该是:

    edge_v = tf.keras.backend.dot(edge_v, self.vars['a'])
    

    edge_v = tf.tensordot(edge_v, self.vars['a'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 2019-04-22
      • 2013-07-11
      • 2017-04-15
      • 1970-01-01
      • 2016-10-19
      • 2014-03-25
      • 2016-10-22
      相关资源
      最近更新 更多