如何从TensorFlow中的SparseTensor中选择一行?

问题描述 投票:2回答:3

比方说,如果我有两个SparseTensors如下:

[[1, 0, 0, 0],
 [2, 0, 0, 0],
 [1, 2, 0, 0]]

[[1.0, 0, 0, 0],
 [1.0, 0, 0, 0],
 [0.3, 0.7, 0, 0]]

我想从中提取前两行。我需要非零条目的索引和值作为SparseTensors,以便我可以将结果传递给tf.nn.embedding_lookup_sparse。我怎样才能做到这一点?

我的应用是:我想使用单词嵌入,这在TensorFlow中非常简单。但是现在我想使用稀疏嵌入,即:对于常见的单词,它们有自己的嵌入。对于罕见的单词,它们的嵌入是常见单词嵌入的稀疏线性组合。所以我需要两本烹饪书来指示如何组成稀疏的嵌入。在前面提到的例子中,食谱说:对于第一个单词,它的嵌入包含自己的嵌入权重1.0。第二个词的情况类似。换句话说,这个词的嵌入是前两个词的嵌入的线性组合,相应的权重分别为0.3和0.7。我需要提取一行,然后将索引和权重提供给tf.nn.embedding_lookup_sparse以获得最终的嵌入。我怎么能在TensorFlow中做到这一点?

或者我需要解决它,即:预处理我的数据并处理TensorFlow中的食谱?

python tensorflow embedding
3个回答
4
投票

我在这里找到了一位了解这个领域的工程师,这是他传递的内容:

我不确定我们是否有一个有效的实现,但这是一个使用dynamic_partition和收集操作的不太优化的实现。

def sparse_slice(indices, values, needed_row_ids):
   num_rows = tf.shape(indices)[0]
   partitions = tf.cast(tf.equal(indices[:,0], needed_row_ids), tf.int32)
   rows_to_gather = tf.dynamic_partition(tf.range(num_rows), partitions, 2)[1]
   slice_indices = tf.gather(indices, rows_to_gather)
   slice_values = tf.gather(values, rows_to_gather)
   return slice_indices, slice_values

with tf.Session().as_default():
  indices = tf.constant([[0,0], [1, 0], [2, 0], [2, 1]])
  values = tf.constant([1.0, 1.0, 0.3, 0.7], dtype=tf.float32)
  needed_row_ids = tf.constant([1])
  slice_indices, slice_values = sparse_slice(indices, values, needed_row_ids)
  print(slice_indices.eval(), slice_values.eval())

更新:

工程师发送了一个示例来帮助处理多行,感谢您指出这一点!

def sparse_slice(indices, values, needed_row_ids):
  needed_row_ids = tf.reshape(needed_row_ids, [1, -1])
  num_rows = tf.shape(indices)[0]
  partitions = tf.cast(tf.reduce_any(tf.equal(tf.reshape(indices[:,0], [-1, 1]), needed_row_ids), 1), tf.int32)
  rows_to_gather = tf.dynamic_partition(tf.range(num_rows), partitions, 2)[1]
  slice_indices = tf.gather(indices, rows_to_gather)
  slice_values = tf.gather(values, rows_to_gather)
  return slice_indices, slice_values

with tf.Session().as_default():
  indices = tf.constant([[0,0], [1, 0], [2, 0], [2, 1]])
  values = tf.constant([1.0, 1.0, 0.3, 0.7], dtype=tf.float32)
  needed_row_ids = tf.constant([0, 2])

0
投票

sp成为你的2d SparseTensor的名字。您可以先为要提取的SparseTensor行创建一个指标张量,即

mask = tf.concat([tf.constant([True, True]), tf.fill([sp.dense_shape[0] - 2],
    False)], axis=0)

接下来使用tf.gather将其传播到稀疏索引:

mask_sp = tf.gather(mask, sp.indices[:, 0])

最后,

values = tf.boolean_mask(sp.values, mask_sp)
indices = tf.boolean_mask(sp.indices, mask_sp)
dense_shape = [sp.dense_shape[0] - 2, sp.dense_shape[1]]
output_sp = tf.SparseTensor(indices=indices, values=values, dense_shape=dense_shape)

0
投票

它不应该更像这样:

该版本将使指数的顺序和频率保持在selected_indices中,因此可以使例如多次选择同一行:

import tensorflow as tf
tf.enable_eager_execution()

def sparse_gather(indices, values, selected_indices, axis=0):
    """
    indices: [[idx_ax0, idx_ax1, idx_ax2, ..., idx_axk], ... []]
    values:  [ value1,                                 , ..., valuen]
    """
    mask = tf.equal(indices[:, axis][tf.newaxis, :], selected_indices[:, tf.newaxis])
    to_select = tf.where(mask)[:, 1]
    return tf.gather(indices, to_select, axis=0), tf.gather(values, to_select, axis=0)


indices = tf.constant([[1, 0], [2, 0], [3, 0], [7, 0]])
values = tf.constant([1.0, 2.0, 3.0, 7.0], dtype=tf.float32)
needed_row_ids = tf.constant([7, 3, 2, 2, 3, 7])
slice_indices, slice_values = sparse_gather(indices, values, needed_row_ids)
print(slice_indices, slice_values)
© www.soinside.com 2019 - 2024. All rights reserved.