tf.nn.embedding_lookup()

tf.nn.embedding_lookup(
    params,
    ids,
    partition_strategy='mod',
    name=None,
    validate_indices=True,
    max_norm=None
)

: Feature vector tensor which select a row corresponding to the index

TensorFlow link: https://tensorflow.google.cn/api_docs/python/tf/nn/embedding_lookup?hl=en

parameter:

  • params: tensor or array;
  • id: corresponding index
  • partition_strategy: partition_strategy is used when len (params)> 1, params element aliquot not divided, then the former (max_id + 1)% len ( params) a multisection id.
    • When partition_strategy = 'mod' time, 13 ids is divided into 5 partitions: [[0, 5, 10], [1, 6, 11], [2, 7, 12], [3, 8], [ 4, 9]], i.e. the data is mapped according to the column, and then the operation look_up. The default is the mod
    • When partition_strategy = 'div' time, 13 ids is divided into 5 partitions: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10], [ 11, 12]], i.e. the data are successively sorted according to standard procedures, and then to look_up operation.

 

 

(From FIG https://www.jianshu.com/p/abea0d9d2436 )

 

For example:

import numpy as np
A = tf.convert_to_tensor(np.array([[[1],[2]],[[3],[4]],[[5],[6]]]))
B = tf.nn.embedding_lookup(A, [[0,1],[1,0],[0,0]])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print('A',sess.run(A))
    print('A shape',A.shape)
    print('B',sess.run(B))
    print('B shape',B.shape)

result:

A [[[1]
  [2]]

 [[3]
  [4]]

 [[5]
  [6]]]
A shape (3, 2, 1)
B [[[[1]
   [2]]

  [[3]
   [4]]]


 [[[3]
   [4]]

  [[1]
   [2]]]


 [[[1]
   [2]]

  [[1]
   [2]]]]
B shape (3, 2, 2, 1)

  

  

references:

[1] tf.nn.embedding_lookup record

Guess you like

Origin www.cnblogs.com/nxf-rabbit75/p/11282480.html