Skip to content

Instantly share code, notes, and snippets.

@lawlite19
Created June 23, 2017 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawlite19/02a598c6d9b91a53d0a38fd081f9cbc4 to your computer and use it in GitHub Desktop.
Save lawlite19/02a598c6d9b91a53d0a38fd081f9cbc4 to your computer and use it in GitHub Desktop.
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