What does tf.nn.embedding_lookup function do ?



  • 1 Answer(s)

          The tf.nn.embedding_lookup function is similar to tf.gather function which returns the elements of params based on the indexes specified by ids.

    Example:

    params = tf.constant([10,20,30,40])
    ids = tf.constant([0,1,2,3])
    print tf.nn.embedding_lookup(params,ids).eval()
    

          This will return [10 20 30 40], based on the index 0 is 10, the index 1 is 20, and so on.

    Hence,

    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 is more than that. Here params argument contains a list of tensors, rather than a single 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)
    

          For this, the indexes, denoted in ids, correspond to elements of tensors according to a partition strategy, here the default partition strategy is ‘mod’.

          On ‘mod‘ strategy, index 0 belongs to the first element of the first tensor in the list. Index 1 belongs to the first element of the second tensor. Index 2 belongs to the first element of the third tensor, and so on. Hence index i corresponds to the first element of the (i+1)th tensor , for all the indexes 0..(n-1), assuming params is a list of n tensors.

          Here, index n not belong to tensor n+1, because the list params contains only n tensors. So index n belongs to the second element of the first tensor. hence, index n+1 corresponds to the second element of the second tensor, etc.

    In the bellow 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 belongs to the first element of the first tensor: 1

    index 1 belongs to the first element of the second tensor: 10

    index 2 belongs to the second element of the first tensor: 2

    index 3 belongs to the second element of the second tensor: 20

    Hence, the output will be:

    [ 2 1 2 10 2 20]
    
    Answered on December 10, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.