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

Function gradients

tensorflow/python/ops/gradients_impl.py:44–159  ·  view source on GitHub ↗

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,
              xs,
              grad_ys=None,
              name="gradients",
              colocate_gradients_with_ops=False,
              gate_gradients=False,
              aggregation_method=None,
              stop_gradients=None,
              unconnected_gradients=UnconnectedGradients.NONE)

Source from the content-addressed store, hash-verified

42
43@tf_export(v1=["gradients"])
44def gradients(ys,
45 xs,
46 grad_ys=None,
47 name="gradients",
48 colocate_gradients_with_ops=False,
49 gate_gradients=False,
50 aggregation_method=None,
51 stop_gradients=None,
52 unconnected_gradients=UnconnectedGradients.NONE):
53 """Constructs symbolic derivatives of sum of `ys` w.r.t. x in `xs`.
54
55 `ys` and `xs` are each a `Tensor` or a list of tensors. `grad_ys`
56 is a list of `Tensor`, holding the gradients received by the
57 `ys`. The list must be the same length as `ys`.
58
59 `gradients()` adds ops to the graph to output the derivatives of `ys` with
60 respect to `xs`. It returns a list of `Tensor` of length `len(xs)` where
61 each tensor is the `sum(dy/dx)` for y in `ys`.
62
63 `grad_ys` is a list of tensors of the same length as `ys` that holds
64 the initial gradients for each y in `ys`. When `grad_ys` is None,
65 we fill in a tensor of '1's of the shape of y for each y in `ys`. A
66 user can provide their own initial `grad_ys` to compute the
67 derivatives using a different initial gradient for each y (e.g., if
68 one wanted to weight the gradient differently for each value in
69 each y).
70
71 `stop_gradients` is a `Tensor` or a list of tensors to be considered constant
72 with respect to all `xs`. These tensors will not be backpropagated through,
73 as though they had been explicitly disconnected using `stop_gradient`. Among
74 other things, this allows computation of partial derivatives as opposed to
75 total derivatives. For example:
76
77 ```python
78 a = tf.constant(0.)
79 b = 2 * a
80 g = tf.gradients(a + b, [a, b], stop_gradients=[a, b])
81 ```
82
83 Here the partial derivatives `g` evaluate to `[1.0, 1.0]`, compared to the
84 total derivatives `tf.gradients(a + b, [a, b])`, which take into account the
85 influence of `a` on `b` and evaluate to `[3.0, 1.0]`. Note that the above is
86 equivalent to:
87
88 ```python
89 a = tf.stop_gradient(tf.constant(0.))
90 b = tf.stop_gradient(2 * a)
91 g = tf.gradients(a + b, [a, b])
92 ```
93
94 `stop_gradients` provides a way of stopping gradient after the graph has
95 already been constructed, as compared to `tf.stop_gradient` which is used
96 during graph construction. When the two approaches are combined,
97 backpropagation stops at both `tf.stop_gradient` nodes and nodes in
98 `stop_gradients`, whichever is encountered first.
99
100 All integer tensors are considered constant with respect to all `xs`, as if
101 they were included in `stop_gradients`.

Callers 3

fwd_gradientsFunction · 0.90
_hessian_vector_productFunction · 0.70
hessiansFunction · 0.70

Calls 1

_mutation_lockMethod · 0.80

Tested by

no test coverage detected