We often use tf.nn.embedding_lookup() to pick up elements in a tensor by ids. Here is an tutorial.
Understand tf.nn.embedding_lookup(): Pick Up Elements by Ids – TensorFlow Tutorial
However, it may encounter InvalidArgumentError: indices is not error when using this function. In this tutorial, we will introduce how to fix this error.
Please look at example code below:
import tensorflow as tf import numpy as np z = tf.Variable(np.array([[2,2,3],[6,7,2]], dtype = np.float32), name = 'x1') xw = tf.nn.embedding_lookup(z,4) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(xw))
Run this python code, you will find InvalidArgumentError: indices is not error occur.
Why does this InvalidArgumentError occur?
The reason is the id is out of tensor induce. As to example above, there are only 2 elements on axis = 0, however, the id is 4, which is out of [0, 2)
How to fix this InvalidArgumentError?
Making the id is in [0,2)
For example:
xw = tf.nn.embedding_lookup(z,1)
Then you will find this InvalidArgumentError is fixed.