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

Method compute_gradients

tensorflow/python/training/optimizer.py:478–593  ·  view source on GitHub ↗

Compute gradients of `loss` for the variables in `var_list`. This is the first part of `minimize()`. It returns a list of (gradient, variable) pairs where "gradient" is the gradient for "variable". Note that "gradient" can be a `Tensor`, an `IndexedSlices`, or `None` if there is n

(self, loss, var_list=None,
                        gate_gradients=GATE_OP,
                        aggregation_method=None,
                        colocate_gradients_with_ops=False,
                        grad_loss=None)

Source from the content-addressed store, hash-verified

476 name=name)
477
478 def compute_gradients(self, loss, var_list=None,
479 gate_gradients=GATE_OP,
480 aggregation_method=None,
481 colocate_gradients_with_ops=False,
482 grad_loss=None):
483 """Compute gradients of `loss` for the variables in `var_list`.
484
485 This is the first part of `minimize()`. It returns a list
486 of (gradient, variable) pairs where "gradient" is the gradient
487 for "variable". Note that "gradient" can be a `Tensor`, an
488 `IndexedSlices`, or `None` if there is no gradient for the
489 given variable.
490
491 Args:
492 loss: A Tensor containing the value to minimize or a callable taking
493 no arguments which returns the value to minimize. When eager execution
494 is enabled it must be a callable.
495 var_list: Optional list or tuple of `tf.Variable` to update to minimize
496 `loss`. Defaults to the list of variables collected in the graph
497 under the key `GraphKeys.TRAINABLE_VARIABLES`.
498 gate_gradients: How to gate the computation of gradients. Can be
499 `GATE_NONE`, `GATE_OP`, or `GATE_GRAPH`.
500 aggregation_method: Specifies the method used to combine gradient terms.
501 Valid values are defined in the class `AggregationMethod`.
502 colocate_gradients_with_ops: If True, try colocating gradients with
503 the corresponding op.
504 grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`.
505
506 Returns:
507 A list of (gradient, variable) pairs. Variable is always present, but
508 gradient can be `None`.
509
510 Raises:
511 TypeError: If `var_list` contains anything else than `Variable` objects.
512 ValueError: If some arguments are invalid.
513 RuntimeError: If called with eager execution enabled and `loss` is
514 not callable.
515
516 @compatibility(eager)
517 When eager execution is enabled, `gate_gradients`, `aggregation_method`,
518 and `colocate_gradients_with_ops` are ignored.
519 @end_compatibility
520 """
521 if self.doing_loss_scaling():
522 loss_scale = self._loss_scale()
523 if callable(loss):
524 loss = lambda: loss() * loss_scale
525 else:
526 loss = loss * loss_scale
527
528 if callable(loss):
529 with backprop.GradientTape() as tape:
530 if var_list is not None:
531 tape.watch(var_list)
532 loss_value = loss()
533
534 # Scale loss if using a "mean" loss reduction and multiple replicas.
535 # Have to be careful to call distribute_lib.get_loss_reduction()

Calls 15

doing_loss_scalingMethod · 0.95
_scale_lossMethod · 0.95
_unscale_gradsMethod · 0.95
_assert_valid_dtypesMethod · 0.95
gradientMethod · 0.80
executing_eagerlyMethod · 0.80
_get_processorFunction · 0.70
lossFunction · 0.50
GradientTapeMethod · 0.45
watchMethod · 0.45
watched_variablesMethod · 0.45
control_dependenciesMethod · 0.45