直接上代码:
#coding:utf-8
import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
c = np.random.random([6,5]) ##随机生成一个6*5的矩阵
b = tf.nn.embedding_lookup(c, [1, 3, 5]) ##查找矩阵中的序号为1、3和5的行
with tf.compat.v1.Session() as sess:
#sess.run(tf.initialize_all_variables())
#print("The val of c:")
print(c)
#print("The val of b:")
print(sess.run(b))
tf.nn.embedding_lookup的第一个输入参数为embedding表(随机生成的6*5矩阵),会根据第二个输入索引[1,3,5]找到对应embedding表中的embedding向量并返回,返回:
[[0.64235185 0.6707816 0.58392453 0.33220543 0.67992624]
[0.64109123 0.48419797 0.35707349 0.1467957 0.5385074 ]
[0.38189233 0.44806117 0.67364115 0.19870934 0.52501514]
[0.20098046 0.914524 0.5199566 0.93645699 0.31077416]
[0.84544595 0.81905171 0.05012403 0.31296981 0.1903282 ]
[0.04697684 0.93789082 0.00644587 0.05577378 0.83547316]]
[[0.64109123 0.48419797 0.35707349 0.1467957 0.5385074 ]
[0.20098046 0.914524 0.5199566 0.93645699 0.31077416]
[0.04697684 0.93789082 0.00644587 0.05577378 0.83547316]]