假设我有两个SparseTensor
,如下所示:
[[1, 0, 0, 0],
[2, 0, 0, 0],
[1, 2, 0, 0]]
以及
^{pr2}$我想从它们中提取前两行。我需要非零项的索引和值都是SparseTensor
s,这样我就可以将结果传递给tf.nn.embedding_lookup_sparse
。我该怎么做?在
我的申请是:
我想用单词嵌入,这在TensorFlow中是非常直接的。但现在我想使用稀疏嵌入,即:对于普通单词,它们有自己的嵌入。对于稀有词,它们的嵌入是普通词嵌入的稀疏线性组合。
所以我需要两本食谱来说明稀疏嵌入是如何组成的。在前面提到的例子中,cookbook说:对于第一个单词,它的嵌入由它自己的权重为1.0的嵌入组成。第二个词的情况类似。对于最后一个词,它说:这个词的嵌入是前两个词嵌入的线性组合,相应的权重分别为0.3和0.7。
我需要提取一行,然后将索引和权重提供给tf.nn.embedding_lookup_sparse
,以获得最终的嵌入。在TensorFlow中怎么做?在
或者我需要解决它,例如:预处理我的数据并处理TensorFlow中的食谱?在
它不应该表现得更像这样:
此版本将保持选定索引中索引的顺序和频率,因此可以多次选择同一行:
让
sp
作为2d SparseTensor的名称。首先可以为要提取的SparseTensor行创建一个指示符张量,即下次使用tf.聚集要将其传播到稀疏索引,请执行以下操作:
^{pr2}$最后
我找了一位对这个领域了解更多的工程师,他告诉我的是:
我不确定我们是否有一个有效的实现,但这里有一个使用动态分区和聚集操作的不太理想的实现。在
更新:
工程师还发送了一个示例来帮助处理多行,感谢您指出这一点!在
^{pr2}$相关问题 更多 >
编程相关推荐