Gradient for UnsortedSegmentMin and UnsortedSegmentMax.
(op, grad)
| 426 | |
| 427 | |
| 428 | def _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") |
no test coverage detected