Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tensorflow中embedding_lookup函数
def test_embedding_look_up():
input_ids = tf.placeholder(dtype=tf.int32, shape=[3,2])
#embedding = tf.get_variable('test', shape=[5,5])
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("embeding:\n", embedding.eval())
result = sess.run(input_embedding, feed_dict={input_ids:[[1, 2], [2, 1], [3, 3]]})
print("结果:\n", result)
print(result.shape)
test_embedding_look_up()
outputs:
embeding:
[[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]]
结果:
[[[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]]]
(3, 2, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.