MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _UnsortedSegmentMinOrMaxGrad

Function _UnsortedSegmentMinOrMaxGrad

tensorflow/python/ops/math_grad.py:428–443  ·  view source on GitHub ↗

Gradient for UnsortedSegmentMin and UnsortedSegmentMax.

(op, grad)

Source from the content-addressed store, hash-verified

426
427
428def _UnsortedSegmentMinOrMaxGrad(op, grad):
429 """ Gradient for UnsortedSegmentMin and UnsortedSegmentMax. """
430 # Get the number of selected (minimum or maximum) elements in each segment.
431 gathered_outputs, zero_clipped_indices, is_positive = \
432 _GatherDropNegatives(op.outputs[0], op.inputs[1])
433 is_selected = math_ops.equal(op.inputs[0], gathered_outputs)
434 is_selected = math_ops.logical_and(is_selected, is_positive)
435 num_selected = math_ops.unsorted_segment_sum(
436 math_ops.cast(is_selected, grad.dtype), op.inputs[1], op.inputs[2])
437 # Compute the gradient for each segment. The gradient for the ith segment is
438 # divided evenly among the selected elements in that segment.
439 weighted_grads = math_ops.divide(grad, num_selected)
440 gathered_grads, _, _ = _GatherDropNegatives(weighted_grads, None,
441 zero_clipped_indices, is_positive)
442 zeros = array_ops.zeros_like(gathered_grads)
443 return array_ops.where(is_selected, gathered_grads, zeros), None, None
444
445
446@ops.RegisterGradient("UnsortedSegmentSum")

Callers 2

_UnsortedSegmentMaxGradFunction · 0.85
_UnsortedSegmentMinGradFunction · 0.85

Calls 4

_GatherDropNegativesFunction · 0.85
equalMethod · 0.80
divideMethod · 0.80
castMethod · 0.45

Tested by

no test coverage detected