embedding_lookup()的用法
这个函数真的很常用,尤其word2vec
tf.nn.embedding_lookup()就是根据input_ids中的id,寻找embeddings中的第id行。比如input_ids=[1,3,5],则找出embeddings中第1,3,5行,组成一个tensor返回。
实例 1
import tensorflow as tf import numpy as np input_ids = tf.placeholder(tf.int32, shape=[None], name="input_ids") embedding = tf.Variable(np.identity(5, dtype=np.int32)) input_embedding = tf.nn.embedding_lookup(embedding, input_ids) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) print("embedding=\n", embedding.eval()) print("input_embedding=\n", sess.run(input_embedding, feed_dict={input_ids: [1, 2, 3, 0, 3, 2, 1]}))
结果
embedding= [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]] input_embedding= [[0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [1 0 0 0 0] [0 0 0 1 0] [0 0 1 0 0] [0 1 0 0 0]] [Finished in 3.8s]
实例2
import tensorflow as tf import numpy as np input_ids = tf.placeholder(dtype=tf.int32, shape=[3, 2]) embedding = tf.Variable(np.identity(5, dtype=np.int32)) input_embedding = tf.nn.embedding_lookup(embedding, input_ids) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) print("embedding=\n", embedding.eval()) print("input_embedding=\n", sess.run(input_embedding, feed_dict={input_ids: [[1, 2], [2, 1], [3, 3]]}))
结果
embedding= [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]] input_embedding= [[[0 1 0 0 0] [0 0 1 0 0]] [[0 0 1 0 0] [0 1 0 0 0]] [[0 0 0 1 0] [0 0 0 1 0]]] [Finished in 4.0s]
来自:https://blog.csdn.net/u013041398/article/details/60955847
https://blog.csdn.net/laolu1573/article/details/77170407