本文整理汇总了Python中tensorflow.python.ops.sparse_ops.sparse_reorder方法的典型用法代码示例。如果您正苦于以下问题:Python sparse_ops.sparse_reorder方法的具体用法?Python sparse_ops.sparse_reorder怎么用?Python sparse_ops.sparse_reorder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.sparse_ops
的用法示例。
在下文中一共展示了sparse_ops.sparse_reorder方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _SparseReorderGrad
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_reorder [as 别名]
def _SparseReorderGrad(op, unused_output_indices_grad, output_values_grad):
"""Gradients for the SparseReorder op.
Args:
op: the SparseReorder op
unused_output_indices_grad: the incoming gradients of the output indices
output_values_grad: the incoming gradients of the output values
Returns:
Gradient for each of the 3 input tensors:
(input_indices, input_values, input_shape)
The gradients for input_indices and input_shape is None.
"""
input_indices = op.inputs[0]
input_shape = op.inputs[2]
num_entries = array_ops.shape(input_indices)[0]
entry_indices = math_ops.range(num_entries)
sp_unordered = sparse_tensor.SparseTensor(
input_indices, entry_indices, input_shape)
sp_ordered = sparse_ops.sparse_reorder(sp_unordered)
inverted_permutation = array_ops.invert_permutation(sp_ordered.values)
return (None,
array_ops.gather(output_values_grad, inverted_permutation),
None)
示例2: _per_batch_set_op
# 需要导入模块: from tensorflow.python.ops import sparse_ops [as 别名]
# 或者: from tensorflow.python.ops.sparse_ops import sparse_reorder [as 别名]
def _per_batch_set_op(set_op, x):
"""Computes a set operation on a single batch of prediction & labelled span.
Args:
set_op: A callable function which is a tf.sets operation.
x: A tuple of (pred_begin, pred_end, pred_label, gold_begin, gold_end,
gold_label) which are the prediction and gold labelled spans for a single
batch. Each element of the tuple is a 1-D Tensor. pred_* Tensors should
have the same size, and gold_* as well, but pred_* and gold_* Tensors may
have different sizes.
Returns:
Performs the sets operation and returns the number of results.
"""
x = tuple(math_ops.cast(i, dtypes.int64) for i in x)
pred_begin, pred_end, pred_label, gold_begin, gold_end, gold_label = x
# Combine spans together so they can be compared as one atomic unit. For
# example:
# If pred_begin = [0, 3, 5], pred_end = [2, 4, 6]
# gold_begin = [0, 5], gold_end = [2, 7]
# Then we combine the spans into:
# pred = [[0, 2], [3, 4], [5, 6]]
# gold = [[0, 2], [5, 7]]
#
# In the sets operation, we want [0, 2] to be treated as one atomic comparison
# unit (both begin=0 and end=2 offsets must match). Conversely, partial
# matches (like [5, 6] and [5, 7]) are not a match.
#
# This is done by constructing a SparseTensor (containing span begin, end,
# label points) for predictions and labels.
pred_begin = array_ops.expand_dims(pred_begin, 1)
pred_end = array_ops.expand_dims(pred_end, 1)
gold_begin = array_ops.expand_dims(gold_begin, 1)
gold_end = array_ops.expand_dims(gold_end, 1)
# Because the last dimension is ignored in comparisons for tf.sets operations,
# we add an unused last dimension.
unused_last_pred_dim = array_ops.zeros_like(pred_begin)
unused_last_gold_dim = array_ops.zeros_like(gold_begin)
pred_indices = array_ops.concat([pred_begin, pred_end, unused_last_pred_dim],
1)
gold_indices = array_ops.concat([gold_begin, gold_end, unused_last_gold_dim],
1)
# set_ops require the bounding shape to match. Find the bounding shape
# with the max number
max_shape = math_ops.reduce_max(
array_ops.concat([pred_indices, gold_indices], 0), 0)
max_shape = max_shape + array_ops.ones_like(max_shape)
pred = sparse_tensor.SparseTensor(pred_indices, pred_label, max_shape)
pred = sparse_ops.sparse_reorder(pred)
gold = sparse_tensor.SparseTensor(gold_indices, gold_label, max_shape)
gold = sparse_ops.sparse_reorder(gold)
results = set_op(pred, gold).indices
num_results = control_flow_ops.cond(
array_ops.size(results) > 0,
true_fn=lambda: array_ops.shape(results)[0],
false_fn=lambda: constant_op.constant(0))
return num_results