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

Function _grad_fn

tensorflow/python/ops/while_v2.py:637–673  ·  view source on GitHub ↗

Computes the gradient of `func_graph` in the current graph. This function builds the gradient graph of the corresponding forward-pass `func_graph` by differentiating `func_graph`'s outputs w.r.t. its inputs. Args: ys: A `Tensor` or list of tensors to be differentiated. xs: A `Tensor`

(ys, xs, args, func_graph)

Source from the content-addressed store, hash-verified

635
636
637def _grad_fn(ys, xs, args, func_graph):
638 """Computes the gradient of `func_graph` in the current graph.
639
640 This function builds the gradient graph of the corresponding forward-pass
641 `func_graph` by differentiating `func_graph`'s outputs w.r.t. its inputs.
642
643 Args:
644 ys: A `Tensor` or list of tensors to be differentiated.
645 xs: A `Tensor` or list of tensors to be used for differentiation.
646 args: The input arguments.
647 args[0] - Loop counter
648 args[1] - Total number of iterations.
649 args[2] - maximum_iterations.
650 args[3:] - Incoming gradients for `ys`.
651 func_graph: function.FuncGraph. The corresponding forward-pass function.
652
653 Returns:
654 The output gradient Tensors.
655 """
656 grad_ys = args[3:]
657
658 # Build the gradient graph. Note that this builds the gradient computation of
659 # func_graph in the current graph, which requires capturing tensors from
660 # func_graph. The captured func_graph tensors are resolved to external tensors
661 # after the forward While op has been rewritten in _resolve_grad_captures.
662 # TODO(srbs): Mark GradientsHelper as public?
663 grad_outs = gradients_util._GradientsHelper(
664 ys, xs, grad_ys=grad_ys, src_graph=func_graph,
665 unconnected_gradients="zero")
666
667 # TODO(b/118712257): Handle the case when grad_outs has None's e.g. when there
668 # is a tf.StopGradient in the loop body.
669 assert all(g is not None for g in grad_outs)
670 counter = args[0]
671 maximum_iterations = args[1]
672 total_iters = args[2]
673 return [counter + 1, maximum_iterations, total_iters] + grad_outs
674
675
676def _resolve_grad_captures(body_graph, body_grad_graph, while_op):

Callers 1

_create_grad_funcFunction · 0.70

Calls 1

allFunction · 0.85

Tested by

no test coverage detected