本文整理匯總了Python中syntaxnet.ops.gen_parser_ops.unpack_syntax_net_sparse_features方法的典型用法代碼示例。如果您正苦於以下問題:Python gen_parser_ops.unpack_syntax_net_sparse_features方法的具體用法?Python gen_parser_ops.unpack_syntax_net_sparse_features怎麽用?Python gen_parser_ops.unpack_syntax_net_sparse_features使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類syntaxnet.ops.gen_parser_ops
的用法示例。
在下文中一共展示了gen_parser_ops.unpack_syntax_net_sparse_features方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: EmbeddingLookupFeatures
# 需要導入模塊: from syntaxnet.ops import gen_parser_ops [as 別名]
# 或者: from syntaxnet.ops.gen_parser_ops import unpack_syntax_net_sparse_features [as 別名]
def EmbeddingLookupFeatures(params, sparse_features, allow_weights):
"""Computes embeddings for each entry of sparse features sparse_features.
Args:
params: list of 2D tensors containing vector embeddings
sparse_features: 1D tensor of strings. Each entry is a string encoding of
dist_belief.SparseFeatures, and represents a variable length list of
feature ids, and optionally, corresponding weights values.
allow_weights: boolean to control whether the weights returned from the
SparseFeatures are used to multiply the embeddings.
Returns:
A tensor representing the combined embeddings for the sparse features.
For each entry s in sparse_features, the function looks up the embeddings
for each id and sums them into a single tensor weighing them by the
weight of each id. It returns a tensor with each entry of sparse_features
replaced by this combined embedding.
"""
if not isinstance(params, list):
params = [params]
# Lookup embeddings.
sparse_features = tf.convert_to_tensor(sparse_features)
indices, ids, weights = gen_parser_ops.unpack_syntax_net_sparse_features(
sparse_features)
embeddings = tf.nn.embedding_lookup(params, ids)
if allow_weights:
# Multiply by weights, reshaping to allow broadcast.
broadcast_weights_shape = tf.concat([tf.shape(weights), [1]], 0)
embeddings *= tf.reshape(weights, broadcast_weights_shape)
# Sum embeddings by index.
return tf.unsorted_segment_sum(embeddings, indices, tf.size(sparse_features))