我是靠谱客的博主 无限鱼,这篇文章主要介绍tensorflow学习笔记--tf.nn.embedding_lookup,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np import tensorflow as tf vocab_size=10000 embedding_size=100 #随机产生10个整型数,取值范围是(0,9] encoder_inputs = np.random.randint(10, size=10) print(encoder_inputs) embedding = tf.get_variable('embedding', [vocab_size, embedding_size]) print('embedding.shape=',embedding.shape) #将10个10000维的数嵌入到100维 encoder_inputs_embedded = tf.nn.embedding_lookup(embedding, encoder_inputs) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) embedded = sess.run(encoder_inputs_embedded) print('embedded.shape=', embedded.shape)

结果:

复制代码
1
2
3
4
[3 4 9 2 1 6 9 0 2 3] embedding.shape= (10000, 100) embedded.shape= (10, 100)

注意:get_variable方法如果再次运行,会报错。需要重启kernel. 另外,需要运行

复制代码
1
sess.run(tf.global_variables_initializer())

将全局变量初始化。




最后

以上就是无限鱼最近收集整理的关于tensorflow学习笔记--tf.nn.embedding_lookup的全部内容,更多相关tensorflow学习笔记--tf内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部