def _apply_cond(self, apply_fn, grad, var, *args, **kwargs):
    """Apply conditionally if counter is zero."""
    grad_acc = self.get_slot(var, "grad_acc")

    def apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs):
      total_grad = (grad_acc + grad) / tf.cast(self._n_t, grad.dtype)
      adam_op = apply_fn(total_grad, var, *args, **kwargs)
      with tf.control_dependencies([adam_op]):
        grad_acc_to_zero_op = grad_acc.assign(tf.zeros_like(grad_acc),
                                              use_locking=self._use_locking)
      return tf.group(adam_op, grad_acc_to_zero_op)

    def accumulate_gradient(grad_acc, grad):
      assign_op = tf.assign_add(grad_acc, grad, use_locking=self._use_locking)
      return tf.group(assign_op)  # Strip return value

    return tf.cond(
        tf.equal(self._get_iter_variable(), 0),
        lambda: apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs),
        lambda: accumulate_gradient(grad_acc, grad))
def _apply_cond ( self , apply_fn , grad , var , * args , ** kwargs ) : grad_acc = self . get_slot ( var , "grad_acc" ) def apply_adam ( grad_acc , apply_fn , grad , var , * args , ** kwargs ) : total_grad = ( grad_acc + grad ) / tf . cast ( self . _n_t , grad . dtype ) adam_op = apply_fn ( total_grad , var , * args , ** kwargs ) with tf . control_dependencies ( [ adam_op ] ) : grad_acc_to_zero_op = grad_acc . assign ( tf . zeros_like ( grad_acc ) , use_locking = self . _use_locking ) return tf . group ( adam_op , grad_acc_to_zero_op ) def accumulate_gradient ( grad_acc , grad ) : assign_op = tf . assign_add ( grad_acc , grad , use_locking = self . _use_locking ) return tf . group ( assign_op ) return tf . cond ( tf . equal ( self . _get_iter_variable ( ) , 0 ) , lambda : apply_adam ( grad_acc , apply_fn , grad , var , * args , ** kwargs ) , lambda : accumulate_gradient ( grad_acc , grad ) )
Apply conditionally if counter is zero.