码迷,mamicode.com
首页 > 其他好文 > 详细

tf.nn.embedding_lookup

时间:2020-03-19 21:27:11      阅读:75      评论:0      收藏:0      [点我收藏+]

标签: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:

<tf.Variable ‘Variable: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)>

可以看出,b是由p输出的向量上位置1和3上元素组成的。

2.tf.random.uniform((6, 6), minval=low,maxval=high))返回6*6的矩阵,产生于low和high之间,产生的值是均匀分布的。

 

接下来更改一下数值,上面是生成一个向量,接下来生成一个矩阵:

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

产生的结果为p:

<tf.Variable ‘Variable: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:

<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)>

可以看出,b依旧是p中第1行和第3行的值,也就是默认是作用在行上的

tf.nn.embedding_lookup

标签:form   接下来   nbsp   space   weight   tensor   col   als   mono   

原文地址:https://www.cnblogs.com/liuxiangyan/p/12527188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!