tf.nn.embedding_lookup

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tf.nn.embedding_lookup相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/form' title='form'>form   nbsp   space   weight   tensor   col   als   mono   

1.tf.nn.embedding_lookup鐢ㄦ潵閫夊彇寮犻噺閲屽搴旂殑绱㈠紩鍏冪礌

%tensorflow_version 2.x
import tensorflow as tf
p=tf.Variable(tf.random.uniform([10,1]))
b=tf.nn.embedding_lookup(p,[1,3])
p
b

杈撳嚭鐨勭粨鏋滃垎鍒负p锛?/span>

<tf.Variable 鈥榁ariable:0鈥?shape=(10, 1) dtype=float32, numpy=
array([[0.79612887],
       [0.28201234],
       [0.20101798],
       [0.1620121 ],
       [0.88669086],
       [0.4243393 ],
       [0.51021874],
       [0.09500039],
       [0.12813437],
       [0.42305255]], dtype=float32)>

b:

<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[0.28201234],
       [0.1620121 ]], dtype=float32)>

鍙互鐪嬪嚭锛宐鏄敱p杈撳嚭鐨勫悜閲忎笂浣嶇疆1鍜?涓婂厓绱犵粍鎴愮殑銆?/span>

2.tf.random.uniform((6, 6), minval=low,maxval=high))杩斿洖6*6鐨勭煩闃碉紝浜х敓浜巐ow鍜宧igh涔嬮棿锛屼骇鐢熺殑鍊兼槸鍧囧寑鍒嗗竷鐨勩€?/span>

 

鎺ヤ笅鏉ユ洿鏀逛竴涓嬫暟鍊硷紝涓婇潰鏄敓鎴愪竴涓悜閲忥紝鎺ヤ笅鏉ョ敓鎴愪竴涓煩闃碉細

p=tf.Variable(tf.random.uniform([10,10],-1,1))
b=tf.nn.embedding_lookup(p,[1,3])
p
b

浜х敓鐨勭粨鏋滀负p锛?/span>

<tf.Variable 鈥榁ariable:0鈥?shape=(10, 10) dtype=float32, numpy=
array([[-0.7621522 ,  0.6107156 , -0.47999907,  0.5350437 ,  0.7630944 ,
         0.37270713, -0.8395808 , -0.879581  , -0.47662497, -0.05092502],
       [-0.21088243, -0.0150187 , -0.28028893,  0.3332212 ,  0.4568975 ,
         0.05019474, -0.19229984, -0.4012766 ,  0.38493705,  0.8479743 ],
       [ 0.3077824 , -0.8770895 ,  0.12883782,  0.6170182 , -0.6244514 ,
        -0.2808833 ,  0.5709777 ,  0.6452646 ,  0.24578142,  0.3655765 ],
       [-0.5822737 , -0.710577  , -0.997102  ,  0.8577807 ,  0.82289314,
        -0.510561  ,  0.95922303, -0.09372258, -0.80911994,  0.9954574 ],
       [-0.15612102, -0.00413752,  0.41538835,  0.50921464,  0.7637322 ,
         0.5406666 , -0.8686323 , -0.80358744, -0.12960792,  0.47586107],
       [ 0.33130383, -0.65484834, -0.6364062 , -0.12607336,  0.10087228,
        -0.54285645,  0.45991468,  0.36029506,  0.41191912,  0.65596604],
       [ 0.90655327,  0.86263967,  0.97394824, -0.9905188 , -0.03838801,
        -0.5840478 , -0.7306757 , -0.62264824, -0.19541001,  0.01948309],
       [ 0.27840662, -0.23048878,  0.2640462 ,  0.27937698, -0.13661599,
         0.72016   , -0.43872857, -0.40881586,  0.9849553 , -0.4254725 ],
       [ 0.824687  , -0.3534038 ,  0.78239155,  0.22957778, -0.00436497,
        -0.5633409 , -0.41481328, -0.35603738, -0.22372437, -0.64321375],
       [-0.7983091 ,  0.51379323,  0.87890744, -0.47110224, -0.91740274,
        -0.26170492, -0.8321235 , -0.46379066, -0.2834475 , -0.7457466 ]],
      dtype=float32)>

 

b锛?/span>

<tf.Tensor: shape=(2, 10), dtype=float32, numpy=
array([[-0.21088243, -0.0150187 , -0.28028893,  0.3332212 ,  0.4568975 ,
         0.05019474, -0.19229984, -0.4012766 ,  0.38493705,  0.8479743 ],
       [-0.5822737 , -0.710577  , -0.997102  ,  0.8577807 ,  0.82289314,
        -0.510561  ,  0.95922303, -0.09372258, -0.80911994,  0.9954574 ]],
      dtype=float32)>

鍙互鐪嬪嚭锛宐渚濇棫鏄痯涓1琛屽拰绗?琛岀殑鍊硷紝涔熷氨鏄粯璁ゆ槸浣滅敤鍦ㄨ涓婄殑

以上是关于tf.nn.embedding_lookup的主要内容,如果未能解决你的问题,请参考以下文章

TensorFlow中 tf.nn.embedding_lookup

TensorFlow中 tf.nn.embedding_lookup

深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)

TensorFlow学习(十三):构造LSTM超长简明教程

TensorFlow 嵌入查找

FM中Embeding的理解