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

Function _SquaredDifferenceGrad

tensorflow/python/ops/math_grad.py:1449–1485  ·  view source on GitHub ↗

Returns the gradient for (x-y)^2.

(op, grad)

Source from the content-addressed store, hash-verified

1447
1448@ops.RegisterGradient("SquaredDifference")
1449def _SquaredDifferenceGrad(op, grad):
1450 """Returns the gradient for (x-y)^2."""
1451 x = op.inputs[0]
1452 y = op.inputs[1]
1453 skip_input_indices = None
1454 try:
1455 skip_input_indices = op.skip_input_indices
1456 except AttributeError:
1457 # No gradient skipping, so do the full gradient computation
1458 pass
1459
1460 with ops.control_dependencies([grad]):
1461 # The parens ensure that if grad is IndexedSlices, it'll get multiplied by
1462 # Tensor (not a number like 2.0) which causes it to convert to Tensor.
1463 x_grad = math_ops.scalar_mul(2.0, grad) * (x - y)
1464
1465 if (isinstance(grad, ops.Tensor) and
1466 _ShapesFullySpecifiedAndEqual(x, y, grad)):
1467 return x_grad, -x_grad
1468
1469 (sx, rx, must_reduce_x), (sy, ry, must_reduce_y) = (
1470 SmartBroadcastGradientArgs(x, y, grad))
1471
1472 if skip_input_indices is not None and 0 in skip_input_indices:
1473 gx = None
1474 elif must_reduce_x:
1475 gx = array_ops.reshape(math_ops.reduce_sum(x_grad, rx), sx)
1476 else:
1477 gx = x_grad
1478
1479 if skip_input_indices is not None and 1 in skip_input_indices:
1480 gy = None
1481 elif must_reduce_y:
1482 gy = -array_ops.reshape(math_ops.reduce_sum(x_grad, ry), sy)
1483 else:
1484 gy = -x_grad
1485 return (gx, gy)
1486
1487
1488# Logical operations have no gradients.

Callers

nothing calls this directly

Calls 5

reshapeMethod · 0.80
reduce_sumMethod · 0.80
control_dependenciesMethod · 0.45

Tested by

no test coverage detected