本文整理汇总了Python中dragnn.python.dragnn_ops.extract_fixed_features方法的典型用法代码示例。如果您正苦于以下问题:Python dragnn_ops.extract_fixed_features方法的具体用法?Python dragnn_ops.extract_fixed_features怎么用?Python dragnn_ops.extract_fixed_features使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dragnn.python.dragnn_ops
的用法示例。
在下文中一共展示了dragnn_ops.extract_fixed_features方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fixed_feature_lookup
# 需要导入模块: from dragnn.python import dragnn_ops [as 别名]
# 或者: from dragnn.python.dragnn_ops import extract_fixed_features [as 别名]
def fixed_feature_lookup(component, state, channel_id, stride):
"""Looks up fixed features and passes them through embeddings.
Embedding vectors may be scaled by weights if the features specify it.
Args:
component: Component object in which to look up the fixed features.
state: MasterState object for the live nlp_saft::dragnn::MasterState.
channel_id: int id of the fixed feature to look up.
stride: int Tensor of current batch * beam size.
Returns:
NamedTensor object containing the embedding vectors.
"""
feature_spec = component.spec.fixed_feature[channel_id]
check.Gt(feature_spec.embedding_dim, 0,
'Embeddings requested for non-embedded feature: %s' % feature_spec)
embedding_matrix = component.get_variable(fixed_embeddings_name(channel_id))
with tf.op_scope([embedding_matrix], 'fixed_embedding_' + feature_spec.name):
indices, ids, weights = dragnn_ops.extract_fixed_features(
state.handle, component=component.name, channel_id=channel_id)
size = stride * feature_spec.size
embeddings = embedding_lookup(embedding_matrix, indices, ids, weights, size)
dim = feature_spec.size * feature_spec.embedding_dim
return NamedTensor(
tf.reshape(embeddings, [-1, dim]), feature_spec.name, dim=dim)
示例2: fixed_feature_lookup
# 需要导入模块: from dragnn.python import dragnn_ops [as 别名]
# 或者: from dragnn.python.dragnn_ops import extract_fixed_features [as 别名]
def fixed_feature_lookup(component, state, channel_id, stride):
"""Looks up fixed features and passes them through embeddings.
Embedding vectors may be scaled by weights if the features specify it.
Args:
component: Component object in which to look up the fixed features.
state: MasterState object for the live ComputeSession.
channel_id: int id of the fixed feature to look up.
stride: int Tensor of current batch * beam size.
Returns:
NamedTensor object containing the embedding vectors.
"""
feature_spec = component.spec.fixed_feature[channel_id]
check.Gt(feature_spec.embedding_dim, 0,
'Embeddings requested for non-embedded feature: %s' % feature_spec)
embedding_matrix = component.get_variable(fixed_embeddings_name(channel_id))
with tf.op_scope([embedding_matrix], 'fixed_embedding_' + feature_spec.name):
indices, ids, weights = dragnn_ops.extract_fixed_features(
state.handle, component=component.name, channel_id=channel_id)
size = stride * feature_spec.size
embeddings = embedding_lookup(embedding_matrix, indices, ids, weights, size)
dim = feature_spec.size * feature_spec.embedding_dim
return NamedTensor(
tf.reshape(embeddings, [-1, dim]), feature_spec.name, dim=dim)
示例3: fixed_feature_lookup
# 需要导入模块: from dragnn.python import dragnn_ops [as 别名]
# 或者: from dragnn.python.dragnn_ops import extract_fixed_features [as 别名]
def fixed_feature_lookup(component, state, channel_id, stride, during_training):
"""Looks up fixed features and passes them through embeddings.
Embedding vectors may be scaled by weights if the features specify it.
Args:
component: Component object in which to look up the fixed features.
state: MasterState object for the live ComputeSession.
channel_id: int id of the fixed feature to look up.
stride: int Tensor of current batch * beam size.
during_training: True if this is being called from a training code path.
This controls, e.g., the use of feature ID dropout.
Returns:
NamedTensor object containing the embedding vectors.
"""
feature_spec = component.spec.fixed_feature[channel_id]
check.Gt(feature_spec.embedding_dim, 0,
'Embeddings requested for non-embedded feature: %s' % feature_spec)
if feature_spec.is_constant:
embedding_matrix = tf.get_variable(fixed_embeddings_name(channel_id))
else:
embedding_matrix = component.get_variable(fixed_embeddings_name(channel_id))
with tf.op_scope([embedding_matrix], 'fixed_embedding_' + feature_spec.name):
indices, ids, weights = dragnn_ops.extract_fixed_features(
state.handle, component=component.name, channel_id=channel_id)
if during_training and feature_spec.dropout_id >= 0:
ids, weights = apply_feature_id_dropout(ids, weights, feature_spec)
if component.master.build_runtime_graph:
# To simplify integration with NN compilers, assume that each feature in
# the channel extracts exactly one ID and no weights.
# TODO(googleuser): Relax this restriction?
embeddings = []
for index in range(feature_spec.size):
feature_id = component.add_cell_input(
tf.int32, [1], 'fixed_channel_{}_index_{}_ids'.format(
channel_id, index))
embeddings.append(tf.gather(embedding_matrix, feature_id))
embeddings = tf.concat(embeddings, 1)
else:
size = stride * feature_spec.size
embeddings = embedding_lookup(embedding_matrix, indices, ids, weights,
size)
dim = feature_spec.size * feature_spec.embedding_dim
return NamedTensor(
tf.reshape(embeddings, [-1, dim]), feature_spec.name, dim=dim)