问题描述
我应该在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,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
我怀疑这里的错误与第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:在我看来,似乎有一些答案,但它们似乎可以解决不同的问题。
预先感谢一堆
解决方法
什么是点?您导入了该方法吗?
它应该是:
edge_v = tf.keras.backend.dot(edge_v,self.vars['a'])
或
edge_v = tf.tensordot(edge_v,self.vars['a'])