网站建设服务网络服务,江诗丹顿手表网站,百度新闻发布平台,wordpress降低版本tf.nn.embedding_lookup ( tensor, id )
作用#xff1a;选取一个张量里面索引对应的元素 应用场景#xff1a;单值离散特征的 embedding#xff0c;相当于 one-hot 编码
用户\水果苹果香蕉草莓芒果西瓜木瓜火龙果user11000000user20001000user30000001
import tensorflow…tf.nn.embedding_lookup ( tensor, id )
作用选取一个张量里面索引对应的元素 应用场景单值离散特征的 embedding相当于 one-hot 编码
用户\水果苹果香蕉草莓芒果西瓜木瓜火龙果user11000000user20001000user30000001
import tensorflow as tf
import numpy as npp tf.Variable(np.arange(21).reshape(7,3))
u tf.nn.embedding_lookup(p, ids[0, 3, 6])
with tf.Session() as s:s.run(tf.global_variables_initializer())print(s.run(t))# [[ 0 1 2] --- p
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]
# [12 13 14]
# [15 16 17]
# [18 19 20]]# [[ 0 1 2] ---user1
# [ 9 10 11] ---user2
# [18 19 20]] ---user3
tf.nn.embedding_lookup_sparse(params, sp_ids)
作用选取一个张量里面多个索引对应的元素的平均值 应用场景多值离散特征的 embedding相当于多次 one-hot 后取平均值
用户\水果苹果香蕉草莓芒果西瓜木瓜火龙果user11110000user21001000user30000111
import tensorflow as tf
import numpy as npp tf.Variable(np.arange(21).reshape(7,3), dtypetf.float32)
gs tf.SparseTensor(indices[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1]], values[0,1,2,6,0,3,4,5], dense_shape(3,3))
embedded_tags tf.nn.embedding_lookup_sparse(p, sp_idstags, sp_weightsNone)with tf.Session() as s:s.run([tf.global_variables_initializer(), tf.tables_initializer()])print(s.run([embedded_tags]))#[[ 3. , 4. , 5. ], ---user1
# [ 9. , 10. , 11. ], ---user2
# [13.5, 14.5, 15.5]] ---user3比如 user1( [ 0 1 2 ] [ 3 4 5 ] [ 6 7 8 ] ) / 3 [ 3. , 4. , 5. ]
相关参考推荐系统遇上深度学习(四)–多值离散特征的embedding解决方案