Gradient for Add.
(op, grad)
| 1091 | @ops.RegisterGradient("Add") |
| 1092 | @ops.RegisterGradient("AddV2") |
| 1093 | def _AddGrad(op, grad): |
| 1094 | """Gradient for Add.""" |
| 1095 | y = op.inputs[1] |
| 1096 | skip_input_indices = None |
| 1097 | try: |
| 1098 | skip_input_indices = op.skip_input_indices |
| 1099 | if skip_input_indices is not None and 1 in skip_input_indices and _IsScalar( |
| 1100 | y): |
| 1101 | return grad, None |
| 1102 | except AttributeError: |
| 1103 | # No gradient skipping, so do the full gradient computation |
| 1104 | pass |
| 1105 | x = op.inputs[0] |
| 1106 | if (isinstance(grad, ops.Tensor) and |
| 1107 | _ShapesFullySpecifiedAndEqual(x, y, grad)): |
| 1108 | return grad, grad |
| 1109 | (sx, rx, must_reduce_x), (sy, ry, must_reduce_y) = ( |
| 1110 | SmartBroadcastGradientArgs(x, y, grad)) |
| 1111 | if skip_input_indices is not None and 0 in skip_input_indices: |
| 1112 | gx = None |
| 1113 | elif not must_reduce_x: |
| 1114 | gx = grad |
| 1115 | else: |
| 1116 | gx = array_ops.reshape(math_ops.reduce_sum(grad, rx), sx) |
| 1117 | if skip_input_indices is not None and 1 in skip_input_indices: |
| 1118 | gy = None |
| 1119 | elif not must_reduce_y: |
| 1120 | gy = grad |
| 1121 | else: |
| 1122 | gy = array_ops.reshape(math_ops.reduce_sum(grad, ry), sy) |
| 1123 | return (gx, gy) |
| 1124 | |
| 1125 | |
| 1126 | @ops.RegisterGradient("Sub") |
nothing calls this directly
no test coverage detected