Constructs symbolic derivatives of sum of `ys` w.r.t. x in `xs`. `ys` and `xs` are each a `Tensor` or a list of tensors. `grad_ys` is a list of `Tensor`, holding the gradients received by the `ys`. The list must be the same length as `ys`. `gradients()` adds ops to the graph to output the
(ys, # pylint: disable=invalid-name
xs,
grad_ys=None,
name="gradients",
gate_gradients=False,
aggregation_method=None,
stop_gradients=None,
unconnected_gradients=UnconnectedGradients.NONE)
| 161 | |
| 162 | @tf_export("gradients", v1=[]) |
| 163 | def gradients_v2(ys, # pylint: disable=invalid-name |
| 164 | xs, |
| 165 | grad_ys=None, |
| 166 | name="gradients", |
| 167 | gate_gradients=False, |
| 168 | aggregation_method=None, |
| 169 | stop_gradients=None, |
| 170 | unconnected_gradients=UnconnectedGradients.NONE): |
| 171 | """Constructs symbolic derivatives of sum of `ys` w.r.t. x in `xs`. |
| 172 | |
| 173 | `ys` and `xs` are each a `Tensor` or a list of tensors. `grad_ys` |
| 174 | is a list of `Tensor`, holding the gradients received by the |
| 175 | `ys`. The list must be the same length as `ys`. |
| 176 | |
| 177 | `gradients()` adds ops to the graph to output the derivatives of `ys` with |
| 178 | respect to `xs`. It returns a list of `Tensor` of length `len(xs)` where |
| 179 | each tensor is the `sum(dy/dx)` for y in `ys`. |
| 180 | |
| 181 | `grad_ys` is a list of tensors of the same length as `ys` that holds |
| 182 | the initial gradients for each y in `ys`. When `grad_ys` is None, |
| 183 | we fill in a tensor of '1's of the shape of y for each y in `ys`. A |
| 184 | user can provide their own initial `grad_ys` to compute the |
| 185 | derivatives using a different initial gradient for each y (e.g., if |
| 186 | one wanted to weight the gradient differently for each value in |
| 187 | each y). |
| 188 | |
| 189 | `stop_gradients` is a `Tensor` or a list of tensors to be considered constant |
| 190 | with respect to all `xs`. These tensors will not be backpropagated through, |
| 191 | as though they had been explicitly disconnected using `stop_gradient`. Among |
| 192 | other things, this allows computation of partial derivatives as opposed to |
| 193 | total derivatives. For example: |
| 194 | |
| 195 | ```python |
| 196 | a = tf.constant(0.) |
| 197 | b = 2 * a |
| 198 | g = tf.gradients(a + b, [a, b], stop_gradients=[a, b]) |
| 199 | ``` |
| 200 | |
| 201 | Here the partial derivatives `g` evaluate to `[1.0, 1.0]`, compared to the |
| 202 | total derivatives `tf.gradients(a + b, [a, b])`, which take into account the |
| 203 | influence of `a` on `b` and evaluate to `[3.0, 1.0]`. Note that the above is |
| 204 | equivalent to: |
| 205 | |
| 206 | ```python |
| 207 | a = tf.stop_gradient(tf.constant(0.)) |
| 208 | b = tf.stop_gradient(2 * a) |
| 209 | g = tf.gradients(a + b, [a, b]) |
| 210 | ``` |
| 211 | |
| 212 | `stop_gradients` provides a way of stopping gradient after the graph has |
| 213 | already been constructed, as compared to `tf.stop_gradient` which is used |
| 214 | during graph construction. When the two approaches are combined, |
| 215 | backpropagation stops at both `tf.stop_gradient` nodes and nodes in |
| 216 | `stop_gradients`, whichever is encountered first. |
| 217 | |
| 218 | All integer tensors are considered constant with respect to all `xs`, as if |
| 219 | they were included in `stop_gradients`. |
| 220 |
nothing calls this directly
no test coverage detected