Add operations to minimize `loss` by updating `var_list`. This method simply combines calls `compute_gradients()` and `apply_gradients()`. If you want to process the gradient before applying them call `compute_gradients()` and `apply_gradients()` explicitly instead of using this fun
(self, loss, global_step=None, var_list=None,
gate_gradients=GATE_OP, aggregation_method=None,
colocate_gradients_with_ops=False, name=None,
grad_loss=None)
| 416 | return self._loss_scale is not None |
| 417 | |
| 418 | def minimize(self, loss, global_step=None, var_list=None, |
| 419 | gate_gradients=GATE_OP, aggregation_method=None, |
| 420 | colocate_gradients_with_ops=False, name=None, |
| 421 | grad_loss=None): |
| 422 | """Add operations to minimize `loss` by updating `var_list`. |
| 423 | |
| 424 | This method simply combines calls `compute_gradients()` and |
| 425 | `apply_gradients()`. If you want to process the gradient before applying |
| 426 | them call `compute_gradients()` and `apply_gradients()` explicitly instead |
| 427 | of using this function. |
| 428 | |
| 429 | Args: |
| 430 | loss: A `Tensor` containing the value to minimize. |
| 431 | global_step: Optional `Variable` to increment by one after the |
| 432 | variables have been updated. |
| 433 | var_list: Optional list or tuple of `Variable` objects to update to |
| 434 | minimize `loss`. Defaults to the list of variables collected in |
| 435 | the graph under the key `GraphKeys.TRAINABLE_VARIABLES`. |
| 436 | gate_gradients: How to gate the computation of gradients. Can be |
| 437 | `GATE_NONE`, `GATE_OP`, or `GATE_GRAPH`. |
| 438 | aggregation_method: Specifies the method used to combine gradient terms. |
| 439 | Valid values are defined in the class `AggregationMethod`. |
| 440 | colocate_gradients_with_ops: If True, try colocating gradients with |
| 441 | the corresponding op. |
| 442 | name: Optional name for the returned operation. |
| 443 | grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`. |
| 444 | |
| 445 | Returns: |
| 446 | An Operation that updates the variables in `var_list`. If `global_step` |
| 447 | was not `None`, that operation also increments `global_step`. |
| 448 | |
| 449 | Raises: |
| 450 | ValueError: If some of the variables are not `Variable` objects. |
| 451 | |
| 452 | @compatibility(eager) |
| 453 | When eager execution is enabled, `loss` should be a Python function that |
| 454 | takes no arguments and computes the value to be minimized. Minimization (and |
| 455 | gradient computation) is done with respect to the elements of `var_list` if |
| 456 | not None, else with respect to any trainable variables created during the |
| 457 | execution of the `loss` function. `gate_gradients`, `aggregation_method`, |
| 458 | `colocate_gradients_with_ops` and `grad_loss` are ignored when eager |
| 459 | execution is enabled. |
| 460 | @end_compatibility |
| 461 | """ |
| 462 | grads_and_vars = self.compute_gradients( |
| 463 | loss, var_list=var_list, gate_gradients=gate_gradients, |
| 464 | aggregation_method=aggregation_method, |
| 465 | colocate_gradients_with_ops=colocate_gradients_with_ops, |
| 466 | grad_loss=grad_loss) |
| 467 | |
| 468 | vars_with_grad = [v for g, v in grads_and_vars if g is not None] |
| 469 | if not vars_with_grad: |
| 470 | raise ValueError( |
| 471 | "No gradients provided for any variable, check your graph for ops" |
| 472 | " that do not support gradients, between variables %s and loss %s." % |
| 473 | ([str(v) for _, v in grads_and_vars], loss)) |
| 474 | |
| 475 | return self.apply_gradients(grads_and_vars, global_step=global_step, |