将TensorFlow sparse_tensor_dense_matmul转换为embedding_lookup_sparse格式

时间:2017-09-08 19:18:02

标签: python numpy tensorflow

TensorFlow documentation提到sp_a (indices, values)期望的SparseTensor格式为:[0, 1]: a [1, 0]: b [1, 4]: c [2, 2]: d

embedding_lookup_sparse

sp_ids sp_weights期望的SparseTensor格式:[0, 0]: 1 [0, 0]: a [1, 0]: 0 [1, 0]: b [1, 1]: 4 [1, 1]: c [2, 0]: 2 [2, 0]: d

sp_a

如何在TensorFlow中将sp_ids转换为sp_weightsdef clean(condition): if type == "object" then if condition then empty else with_entries( if (.value|type) == "object" and (.value|condition) then empty else .value |= clean(condition) end ) end elif type == "array" then map( if type == "object" and condition then empty else clean(condition) end ) else . end ; clean( has("field") and (.field == "status") ) 秒?如果不可能,我怎么能在numpy中做到?

1 个答案:

答案 0 :(得分:0)

我忽略了Tensorflow API中是否存在此转换的函数,但这是我将sp_a转换为sp_idssp_weights的方式:

import tensorflow as tf


indices = tf.constant([[0, 1],
                       [1, 0],
                       [1, 4],
                       [2, 2]], dtype=tf.int64)
values = tf.constant([1, 2, 3, 4])  # a, b, c, d
dense_shape = tf.constant([3, 5], dtype=tf.int64)
sp_a = tf.SparseTensor(indices=indices,
                       values=values,
                       dense_shape=dense_shape)

# transform sp_a into sp_ids and sp_weights
# Get sp_ids values
sp_ids_values = tf.slice(sp_a.indices,
                         begin=[0, 1],
                         size=[-1, 1])
sp_ids_values = tf.squeeze(sp_ids_values)

# Get the indices for sp_ids and sp_weights
d1 = tf.slice(sp_a.indices,
              begin=[0, 0],
              size=[-1, 1])
d2 = tf.expand_dims(scan_accum(tf.squeeze(d1)),
                    axis=1)
indices_ = tf.concat([d1, d2],
                     axis=1)

# Build sp_ids and sp_weights
sp_ids = tf.SparseTensor(indices=indices_,
                         values=sp_ids_values,
                         dense_shape=sp_a.dense_shape)
sp_weights = tf.SparseTensor(indices=indices_,
                             values=sp_a.values,
                             dense_shape=sp_a.dense_shape)

with tf.Session() as sess:
    print(sess.run(sp_ids))
    print(sess.run(sp_weights))

我定义scan_accum here