前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow教程-embedding_lookup用法embedding_lookup()的用法实例 1实例2

tensorflow教程-embedding_lookup用法embedding_lookup()的用法实例 1实例2

作者头像
致Great
发布2018-06-12 16:02:53
1.4K0
发布2018-06-12 16:02:53
举报
文章被收录于专栏:程序生活

embedding_lookup()的用法

这个函数真的很常用,尤其word2vec

tf.nn.embedding_lookup()就是根据input_ids中的id,寻找embeddings中的第id行。比如input_ids=[1,3,5],则找出embeddings中第1,3,5行,组成一个tensor返回。

实例 1

代码语言:javascript
复制
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]}))

结果

代码语言:javascript
复制
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

代码语言:javascript
复制
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]]}))

结果

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.06.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • embedding_lookup()的用法
  • 实例 1
  • 实例2
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档