How to use tf.nn.embedding_lookup_sparse in TensorFlow?
<p>We have tried using <code>tf.nn.embedding_lookup</code> and it works. But it needs dense input data and now we need <code>tf.nn.embedding_lookup_sparse</code> for sparse input.</p>

<p>I have written the following code but get some errors.</p>

<pre><code>import tensorflow as tf
import numpy as np

example1 = tf.SparseTensor(indices=[[4], [7]], values=[1, 1], shape=[10])
example2 = tf.SparseTensor(indices=[[3], [6], [9]], values=[1, 1, 1], shape=[10])

vocabulary_size = 10
embedding_size = 1
var = np.array([0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0])
#embeddings = tf.Variable(tf.ones([vocabulary_size, embedding_size]))
embeddings = tf.Variable(var)

embed = tf.nn.embedding_lookup_sparse(embeddings, example2, None)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())

    print(sess.run(embed))
</code></pre>

<p>The error log looks like this.</p>

<p><a href="https://i.stack.imgur.com/jvZ3y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jvZ3y.png" alt="enter image description here"></a></p>

<p>Now I have no idea how to fix and use this method correctly. Any comment could be appreciated.</p>

<p>After diving into <code>safe_embedding_lookup_sparse</code>'s unit test, I'm more confused why I got this result if giving the sparse weights, especially why we got something like <code>embedding_weights[0][3]</code> where <code>3</code> is not appeared in the code above.</p>

<p><a href="https://i.stack.imgur.com/ofU8u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ofU8u.png" alt="enter image description here"></a></p>