首页 > 其他 > 详细

tensorflow用法记录

时间:2017-11-16 17:47:50      阅读:287      评论:0      收藏:0      [点我收藏+]

使用 embedding 变量

import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()

M = list('ABCD')
table = tf.contrib.lookup.index_table_from_tensor(
    mapping=tf.constant(M), num_oov_buckets=1, default_value=-1)

tf.tables_initializer().run()
tf.global_variables_initializer().run()


# 包含多个ID
IDs = tf.Variable(["A|B|C", "C|D|A|B","C|A|A|B|E|E"] )
embedding_mat = tf.constant(np.arange(20, dtype=float).reshape((20,1)))

# 查找embedding变量,并聚合
hs = tf.string_split(IDs, '|')
sp_ID = tf.SparseTensor(hs.indices, table.lookup(hs.values),hs.dense_shape)
AB = tf.nn.embedding_lookup_sparse(embedding_mat, sp_ID, sp_weights=None, combiner='mean')
AC = tf.nn.embedding_lookup_sparse(embedding_mat, sp_ID, sp_weights=None, combiner='sum')

print(AB.eval() )
print()
print(AC.eval())

结果

[[ 1.        ]
 [ 1.5       ]
 [ 1.83333333]]

[[  3.]
 [  6.]
 [ 11.]]

tensorflow用法记录

原文:http://www.cnblogs.com/bregman/p/7845160.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!