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

Function _SubGrad

tensorflow/python/ops/math_grad.py:1127–1157  ·  view source on GitHub ↗

Gradient for Sub.

(op, grad)

Source from the content-addressed store, hash-verified

1125
1126@ops.RegisterGradient("Sub")
1127def _SubGrad(op, grad):
1128 """Gradient for Sub."""
1129 y = op.inputs[1]
1130 skip_input_indices = None
1131 try:
1132 skip_input_indices = op.skip_input_indices
1133 if skip_input_indices is not None and 1 in skip_input_indices and _IsScalar(
1134 y):
1135 return grad, None
1136 except AttributeError:
1137 # No gradient skipping, so do the full gradient computation
1138 pass
1139 x = op.inputs[0]
1140 if (isinstance(grad, ops.Tensor) and
1141 _ShapesFullySpecifiedAndEqual(x, y, grad)):
1142 return grad, -grad
1143 (sx, rx, must_reduce_x), (sy, ry, must_reduce_y) = (
1144 SmartBroadcastGradientArgs(x, y, grad))
1145 if skip_input_indices is not None and 0 in skip_input_indices:
1146 gx = None
1147 elif not must_reduce_x:
1148 gx = grad
1149 else:
1150 gx = array_ops.reshape(math_ops.reduce_sum(grad, rx), sx)
1151 if skip_input_indices is not None and 1 in skip_input_indices:
1152 gy = None
1153 elif not must_reduce_y:
1154 gy = -grad
1155 else:
1156 gy = array_ops.reshape(math_ops.reduce_sum(-grad, ry), sy)
1157 return (gx, gy)
1158
1159
1160@ops.RegisterGradient("Mul")

Callers

nothing calls this directly

Calls 5

_IsScalarFunction · 0.85
reshapeMethod · 0.80
reduce_sumMethod · 0.80

Tested by

no test coverage detected