| 1286 | } |
| 1287 | |
| 1288 | Status UnsortedSegmentMinOrMaxGrad(const Scope& scope, const Operation& op, |
| 1289 | const std::vector<Output>& grad_inputs, |
| 1290 | std::vector<Output>* grad_outputs) { |
| 1291 | if (op.num_inputs() != 3) { |
| 1292 | return errors::InvalidArgument("UnsortedSegmentMax requires 3 arguments"); |
| 1293 | } |
| 1294 | |
| 1295 | if (grad_inputs.size() != 1) { |
| 1296 | return errors::InvalidArgument( |
| 1297 | "UnsortedSegmentMax grad requires 1 grad input"); |
| 1298 | } |
| 1299 | |
| 1300 | auto grad = grad_inputs[0]; |
| 1301 | // Get the number of selected (minimum or maximum) elements in each segment. |
| 1302 | auto zero_clipped_indices = GetZeroClippedIndices(scope, op.input(1)); |
| 1303 | auto is_positive = GetIsPositive(scope, op.output(0), op.input(1)); |
| 1304 | Output gathered_outputs = GatherDropNegatives( |
| 1305 | scope, op.output(0), zero_clipped_indices, is_positive); |
| 1306 | Output is_selected = Equal(scope, op.input(0), gathered_outputs); |
| 1307 | is_selected = LogicalAnd(scope, is_selected, is_positive); |
| 1308 | auto num_selected = UnsortedSegmentSum( |
| 1309 | scope, Cast(scope, is_selected, grad.type()), op.input(1), op.input(2)); |
| 1310 | // Compute the gradient for each segment.The gradient for the ith segment is |
| 1311 | // divided evenly among the selected elements in that segment. |
| 1312 | auto weighted_grads = Div(scope, grad, num_selected); |
| 1313 | auto gathered_grads = GatherDropNegatives(scope, weighted_grads, |
| 1314 | zero_clipped_indices, is_positive); |
| 1315 | auto zeros = ZerosLike(scope, gathered_grads); |
| 1316 | grad_outputs->push_back(SelectV2(scope, is_selected, gathered_grads, zeros)); |
| 1317 | grad_outputs->push_back(NoGradient()); |
| 1318 | grad_outputs->push_back(NoGradient()); |
| 1319 | return scope.status(); |
| 1320 | } |
| 1321 | |
| 1322 | REGISTER_GRADIENT_OP("UnsortedSegmentMax", UnsortedSegmentMinOrMaxGrad); |
| 1323 | REGISTER_GRADIENT_OP("UnsortedSegmentMin", UnsortedSegmentMinOrMaxGrad); |
nothing calls this directly
no test coverage detected