Processor for Variable.
| 127 | |
| 128 | |
| 129 | class _RefVariableProcessor(_OptimizableVariable): |
| 130 | """Processor for Variable.""" |
| 131 | |
| 132 | def __init__(self, v): |
| 133 | self._v = v |
| 134 | |
| 135 | def __str__(self): |
| 136 | return "<_RefVariableProcessor(%s)>" % self._v |
| 137 | |
| 138 | def target(self): |
| 139 | return self._v._ref() # pylint: disable=protected-access |
| 140 | |
| 141 | def update_op(self, optimizer, g): |
| 142 | if isinstance(g, ops.Tensor): |
| 143 | update_op = optimizer._apply_dense(g, self._v) # pylint: disable=protected-access |
| 144 | if self._v.constraint is not None: |
| 145 | with ops.control_dependencies([update_op]): |
| 146 | return self._v.assign(self._v.constraint(self._v)) |
| 147 | else: |
| 148 | return update_op |
| 149 | else: |
| 150 | assert isinstance(g, ops.IndexedSlices), ("Gradient ", g, " is neither a " |
| 151 | "tensor nor IndexedSlices.") |
| 152 | if self._v.constraint is not None: |
| 153 | raise RuntimeError( |
| 154 | "Cannot use a constraint function on a sparse variable.") |
| 155 | # pylint: disable=protected-access |
| 156 | return optimizer._apply_sparse_duplicate_indices(g, self._v) |
| 157 | |
| 158 | |
| 159 | class _DenseReadResourceVariableProcessor(_OptimizableVariable): |