Returns grad of clip_by_value.
(op, grad)
| 99 | # TODO(scottzhu): switch to use new implmentation in 2 weeks. |
| 100 | # @ops.RegisterGradient("ClipByValue") |
| 101 | def _clip_by_value_grad(op, grad): |
| 102 | """Returns grad of clip_by_value.""" |
| 103 | x = op.inputs[0] |
| 104 | y = op.inputs[1] |
| 105 | z = op.inputs[2] |
| 106 | gdtype = grad.dtype |
| 107 | sx = array_ops.shape(x) |
| 108 | sy = array_ops.shape(y) |
| 109 | sz = array_ops.shape(z) |
| 110 | gradshape = array_ops.shape(grad) |
| 111 | zeros = array_ops.zeros(gradshape, gdtype) |
| 112 | xymask = math_ops.less(x, y) |
| 113 | xzmask = math_ops.greater(x, z) |
| 114 | rx, ry = gen_array_ops.broadcast_gradient_args(sx, sy) |
| 115 | rx, rz = gen_array_ops.broadcast_gradient_args(sx, sz) |
| 116 | xgrad = array_ops.where(math_ops.logical_or(xymask, xzmask), zeros, grad) |
| 117 | ygrad = array_ops.where(xymask, grad, zeros) |
| 118 | zgrad = array_ops.where(xzmask, grad, zeros) |
| 119 | gx = array_ops.reshape(math_ops.reduce_sum(xgrad, rx), sx) |
| 120 | gy = array_ops.reshape(math_ops.reduce_sum(ygrad, ry), sy) |
| 121 | gz = array_ops.reshape(math_ops.reduce_sum(zgrad, rz), sz) |
| 122 | return (gx, gy, gz) |
| 123 | |
| 124 | |
| 125 | @tf_export("clip_by_norm") |
nothing calls this directly
no test coverage detected