樣本查詢embedding向量

import tensorflow as tf


#生成10*5的張量
p=tf.Variable(tf.random_normal([10,5]))

# 模擬兩個樣本中的2個稀疏字段的embedding,有4個元素1,3,2,4
b = tf.nn.embedding_lookup(p, [[1, 3],[2,4]])
# b 的維度是2 * 2 * 5 ,batch=2 ,field size=2,dimension=5
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print("-"*10)
    print("size:",b.shape)
    print(sess.run(b))
    
    print("-"*10)
    print(sess.run(p))
    print(p)
    print(type(p))
    
----------
size: (2, 2, 5)
[[[ 0.9584495   1.0911089   0.4438062  -0.2470447  -0.01349077]
  [-3.468283    0.25270942  1.0326217  -0.5254639   0.6471565 ]]

 [[-1.2455957   0.7872764  -0.1749631  -0.67307574  0.6400736 ]
  [-1.3217722  -0.52964514  0.05733296  1.0814865   0.01955446]]]
----------
[[-0.679096    0.07259754  0.38894963  0.77810115  0.75532866]
 [ 0.9584495   1.0911089   0.4438062  -0.2470447  -0.01349077]
 [-1.2455957   0.7872764  -0.1749631  -0.67307574  0.6400736 ]
 [-3.468283    0.25270942  1.0326217  -0.5254639   0.6471565 ]
 [-1.3217722  -0.52964514  0.05733296  1.0814865   0.01955446]
 [-0.01609571 -0.21058281 -1.2163708   0.5605196   0.03176901]
 [ 2.4257965   1.1268165  -0.3213352  -1.2012012  -0.8926926 ]
 [-0.307383    0.14841548  1.1711972   0.36508134  1.7964875 ]
 [-0.70858204  1.6256514  -1.363502    0.9254432   0.70340014]
 [ 0.8675671  -1.1713122   0.9830054  -0.50111306 -0.3031871 ]]
<tf.Variable 'Variable_4:0' shape=(10, 5) dtype=float32_ref>
<class 'tensorflow.python.ops.variables.RefVariable'>
​

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章