Yes, this feature is hard to understand until you get the point.
In its simplest form, it looks like tf.gather . It returns params elements according to the indices specified by ids .
For example (if you are inside tf.InteractiveSession() )
params = tf.constant([10,20,30,40]) ids = tf.constant([0,1,2,3]) print tf.nn.embedding_lookup(params,ids).eval()
will return [10 20 30 40] , because the first element (index 0) of parameters 10 , the second params element (index 1) is 20 , etc.
Similarly
params = tf.constant([10,20,30,40]) ids = tf.constant([1,1,3]) print tf.nn.embedding_lookup(params,ids).eval()
will return [20 20 40] .
But embedding_lookup more. The params argument can be a list of tensors, not just one tensor.
params1 = tf.constant([1,2]) params2 = tf.constant([10,20]) ids = tf.constant([2,0,2,1,2,3]) result = tf.nn.embedding_lookup([params1, params2], ids)
In this case, the indices indicated in ids correspond to tensor elements according to the partition strategy, where the default partition strategy is “mod”.
In the “mod” strategy, index 0 corresponds to the first element of the first tensor in the list. Index 1 corresponds to the first element of the second tensor. Index 2 corresponds to the first element of the tensor third , etc. Just the index i corresponds to the first element of the tensor (i + 1) th for all indices 0..(n-1) , counting params as a list of tensors n .
Now the index n cannot correspond to the tensor n + 1, since the list of params contains only the tensors n . Thus, the index n corresponds to the second element of the first tensor. Similarly, the index n+1 corresponds to the second element of the second tensor, etc.
So in the code
params1 = tf.constant([1,2]) params2 = tf.constant([10,20]) ids = tf.constant([2,0,2,1,2,3]) result = tf.nn.embedding_lookup([params1, params2], ids)
index 0 corresponds to the first element of the first tensor: 1
index 1 corresponds to the first element of the second tensor: 10
index 2 corresponds to the second element of the first tensor: 2
index 3 corresponds to the second element of the second tensor: 20
Thus, the result will be:
[ 2 1 2 10 2 20]