前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow:embedding_lookup

tensorflow:embedding_lookup

作者头像
ke1th
发布2018-01-02 11:34:33
1.2K0
发布2018-01-02 11:34:33
举报

embedding_lookup

代码语言:javascript
复制
import tensorflow as tf

embedding = tf.get_variable("embedding", initializer=tf.ones(shape=[10, 5]))
look_uop = tf.nn.embedding_lookup(embedding, [1, 2, 3, 4])
# embedding_lookup就像是给 其它行的变量加上了stop_gradient
w1 = tf.get_variable("w", shape=[5, 1])

z = tf.matmul(look_uop, w1)

opt = tf.train.GradientDescentOptimizer(0.1)

#梯度的计算和更新依旧和之前一样,没有需要注意的
gradients = tf.gradients(z, xs=[embedding])
train = opt.apply_gradients([(gradients[0],embedding)])

#print(gradients[4])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(train))
    print(sess.run(embedding))
代码语言:javascript
复制
[[ 1.          1.          1.          1.          1.        ]
 [ 0.90580809  1.0156796   0.96294552  1.01720285  1.08395708]
 [ 0.90580809  1.0156796   0.96294552  1.01720285  1.08395708]
 [ 0.90580809  1.0156796   0.96294552  1.01720285  1.08395708]
 [ 0.90580809  1.0156796   0.96294552  1.01720285  1.08395708]
 [ 1.          1.          1.          1.          1.        ]
 [ 1.          1.          1.          1.          1.        ]
 [ 1.          1.          1.          1.          1.        ]
 [ 1.          1.          1.          1.          1.        ]
 [ 1.          1.          1.          1.          1.        ]]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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