Returns a node to compute gradient of y wrt x.
(x, y, y_shape)
| 194 | |
| 195 | |
| 196 | def _compute_dx_and_dy(x, y, y_shape): |
| 197 | """Returns a node to compute gradient of y wrt x.""" |
| 198 | # We make up a dy so that we can compute the gradients. We don't really use |
| 199 | # the value of dy -- we will always feed it. We need to add an identity node |
| 200 | # so that we can always feed it properly. Otherwise, for the Add operation, |
| 201 | # dx is the same as dy and we cannot fetch the tensor that we are feeding. |
| 202 | with x.graph.as_default(): |
| 203 | dy_orig = constant_op.constant(1.0, shape=y_shape, dtype=y.dtype) |
| 204 | dy = array_ops.identity(dy_orig) |
| 205 | # We compute the gradients for y wrt. x |
| 206 | grads = gradients.gradients(y, x, dy) |
| 207 | assert len(grads) == 1 |
| 208 | return grads[0], dy_orig |
| 209 | |
| 210 | |
| 211 | def _compute_gradient(x, |
no test coverage detected