张量流中的boolean_mask或稀疏点积 [英] boolean_mask or sparse dot product in tensorflow

查看:122
本文介绍了张量流中的boolean_mask或稀疏点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr是动态选择张量的某些项的最有效方法.

tl;dr what is the most efficient way to dynamically choose some entries of a tensor.

我正在尝试实现

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 is tf.nn.embedding_lookup(params, ids). It returns a Tensor, which 0-axis entries are a subset of Tensor params. The indices of kept entries are defined by Tensor ids.

tf.nn.embedding_lookup_sparse :与tf.nn.embedding_lookup,但将ids作为SparseTensor.

这篇关于张量流中的boolean_mask或稀疏点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆