张量流中的boolean_mask或稀疏点积 [英] boolean_mask or sparse dot product in tensorflow
问题描述
tl; dr是动态选择张量的某些项的最有效方法.
tl;dr what is the most efficient way to dynamically choose some entries of a tensor.
我正在尝试实现 语法GCN .基本上,我需要为每个标签使用不同的权重矩阵(让我们忽略此问题的偏见),并在每次运行时选择要使用的相关条目,这些条目将由稀疏矩阵选择(每个条目最多有一个标签)在一个方向上,并且几乎没有边缘,因此甚至没有边缘.
I am trying to implement syntactic GCN in Tensorflow. Basically, I need to have a different weight matrix for every label (lets ignore biases for this question) and choose at each run the relevant entries to use, those would be chosen by a sparse matrix (for each entry there is at most one label in one direction and mostly no edge so not even that).
更具体地讲,当我有一个标记边缘的稀疏矩阵(零一)时,最好将其用于蒙版中,稀疏密集的张量乘法还是只使用普通乘法(我想不是后者,但为简单起见,请在示例中使用它
More concretely, when I have a sparse matrix of labeled edges (zero-one), is it better to use it in a mask, a sparse-dense tensor multiplication or maybe just use normal multiplication (I guess not the latter, but for simplicty use it in the example)
示例:
units = 6 # output size
x = ops.convert_to_tensor(inputs[0], dtype=self.dtype)
labeled_edges = ops.convert_to_tensor(inputs[1], dtype=self.dtype)
edges_shape = labeled_edges.get_shape().as_list()
labeled_edges = expand_dims(labeled_edges, -2)
labeled_edges = tile(
labeled_edges, [1] * (len(edges_shape) - 1) + [units, 1])
graph_kernel = math_ops.multiply(self.kernel, labeled_edges) # here is the question basically
outputs = standard_ops.tensordot(x, graph_kernel, [[1], [0]])
outputs = math_ops.reduce_sum(outputs, [-1])
推荐答案
要回答您的tl; dr问题,您可以尝试使用以下任一方法:
To answer your tl;dr question, you can try using either of the following:
-
tf.nn.embedding_lookup
:典型用法是tf.nn.embedding_lookup(params, ids)
.它返回Tensor
,其中0轴条目是Tensor
参数的子集.保留条目的索引由Tensor
id定义.
tf.nn.embedding_lookup
: typical usage istf.nn.embedding_lookup(params, ids)
. It returns aTensor
, which 0-axis entries are a subset ofTensor
params. The indices of kept entries are defined byTensor
ids.
tf.nn.embedding_lookup_sparse
:与tf.nn.embedding_lookup
,但将ids
作为SparseTensor
.
这篇关于张量流中的boolean_mask或稀疏点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!