Apply gradients to a replica variable.
(v, g)
| 782 | self._create_slots(var_list) |
| 783 | |
| 784 | def update(v, g): |
| 785 | """Apply gradients to a replica variable.""" |
| 786 | assert v is not None |
| 787 | |
| 788 | try: |
| 789 | # Convert the grad to Tensor or IndexedSlices if necessary. |
| 790 | g = ops.convert_to_tensor_or_indexed_slices(g) |
| 791 | except TypeError: |
| 792 | raise TypeError("Gradient must be convertible to a Tensor" |
| 793 | " or IndexedSlices, or None: %s" % g) |
| 794 | if not isinstance(g, (ops.Tensor, ops.IndexedSlices)): |
| 795 | raise TypeError( |
| 796 | "Gradient must be a Tensor, IndexedSlices, or None: %s" % g) |
| 797 | p = _get_processor(v) |
| 798 | |
| 799 | if context.executing_eagerly() or ( |
| 800 | resource_variable_ops.is_resource_variable(v) and |
| 801 | not v._in_graph_mode): # pylint: disable=protected-access |
| 802 | scope_name = v.name.split(":")[0] |
| 803 | else: |
| 804 | scope_name = v.op.name |
| 805 | |
| 806 | # device_policy is set because non-mirrored tensors will be read in |
| 807 | # `update_op`. `_resource_apply_dense`, `lr_t`, `beta1_t` and `beta2_t` |
| 808 | # is an example. |
| 809 | with ops.name_scope("update_" + scope_name): |
| 810 | return p.update_op(self, g) |
| 811 | |
| 812 | with ops.name_scope(name, self._name) as sname: |
| 813 | self._prepare() |