parameters: (self, grads_and_vars, global_step=None, name=None)
return_statement: return conditional_update
docstring: Apply gradients to global variables.
  
      This is the second part of `minimize()`. It returns an `Operation` that
      applies gradients.
  
      Args:
        grads_and_vars: List of (gradient, variable) pairs as returned by
          `compute_gradients()`.
        global_step: Optional `Variable` to increment by one after the variables
          have been updated.
        name: Optional name for the returned operation.  Default to the name
          passed to the `Optimizer` constructor.
  
      Returns:
        An `Operation` that applies the specified gradients. If `global_step`
        was not None, that operation also increments `global_step`.
docstring_summary: Apply gradients to global variables.
docstring_tokens: Apply gradients to global variables .
function: def apply_gradients(self, grads_and_vars, global_step=None, name=None):
      """Apply gradients to global variables.
  
      This is the second part of `minimize()`. It returns an `Operation` that
      applies gradients.
  
      Args:
        grads_and_vars: List of (gradient, variable) pairs as returned by
          `compute_gradients()`.
        global_step: Optional `Variable` to increment by one after the variables
          have been updated.
        name: Optional name for the returned operation.  Default to the name
          passed to the `Optimizer` constructor.
  
      Returns:
        An `Operation` that applies the specified gradients. If `global_step`
        was not None, that operation also increments `global_step`.
      """
      local_vars = [v for g, v in grads_and_vars if g is not None]
      grads = [g for g, v in grads_and_vars if g is not None]
  
      def _variable_creator(next_creator, collections, **kwargs):
        if not collections:
          collections = [ops.GraphKeys.LOCAL_VARIABLES]
        elif ops.GraphKeys.GLOBAL_VARIABLES in collections:
          collections = list(collections)
          collections.append(ops.GraphKeys.LOCAL_VARIABLES)
          collections.remove(ops.GraphKeys.GLOBAL_VARIABLES)
        return next_creator(collections=collections, **kwargs)
  
      # theta = theta - lr * grad
      with variable_scope.variable_creator_scope(_variable_creator):
        local_update_op = self._opt.apply_gradients(grads_and_vars)
  
      # a = a + grad
      update_ops = []
      update_ops.append(local_update_op)
      grad_vars = [self._grad_map[var] for var in local_vars]
      for g, grad_var in zip(grads, grad_vars):
        update_ops.append(state_ops.assign_add(grad_var, g))
  
      global_center_vars = [self._global_map[var] for var in local_vars]
  
      # update global variables.
      def _Update_global_variables():
        global_norm = []
        # a = a / t
        for g in grad_vars:
          global_norm.append(state_ops.assign(g, g / self._period))
        # apply
        with ops.control_dependencies(global_norm):
          apply_global_op = self._opt.apply_gradients(
              zip(grad_vars, global_center_vars))
  
        # pull
        with ops.control_dependencies([apply_global_op]):
          update_ops = []
          if global_step:
            with ops.colocate_with(global_step):
              update_ops.append(state_ops.assign_add(global_step, 1))
  
          for lvar in local_vars:
            g_val = self._global_map[lvar].read_value()
            update_ops.append(state_ops.assign(lvar, g_val))
          for grad_var in grad_vars:
            update_ops.append(
                state_ops.assign(grad_var, array_ops.zeros_like(grad_var)))
          variable_update = control_flow_ops.group(*(update_ops))
        return variable_update
  
      local_update = state_ops.assign_add(
          self._local_step, 1, name='local_step_update').op
  
      with ops.control_dependencies([local_update]):
        condition = math_ops.equal(
            math_ops.mod(self._local_step, self._period), 0)
      with ops.control_dependencies(update_ops):
        conditional_update = control_flow_ops.cond(
            condition, _Update_global_variables, control_flow_ops.no_op)
      return conditional_update
function_tokens: def apply_gradients ( self , grads_and_vars , global_step = None , name = None ) : local_vars = [ v for g , v in grads_and_vars if g is not None ] grads = [ g for g , v in grads_and_vars if g is not None ] def _variable_creator ( next_creator , collections , * * kwargs ) : if not collections : collections = [ ops . GraphKeys . LOCAL_VARIABLES ] elif ops . GraphKeys . GLOBAL_VARIABLES in collections : collections = list ( collections ) collections . append ( ops . GraphKeys . LOCAL_VARIABLES ) collections . remove ( ops . GraphKeys . GLOBAL_VARIABLES ) return next_creator ( collections = collections , * * kwargs ) # theta = theta - lr * grad with variable_scope . variable_creator_scope ( _variable_creator ) : local_update_op = self . _opt . apply_gradients ( grads_and_vars ) # a = a + grad update_ops = [ ] update_ops . append ( local_update_op ) grad_vars = [ self . _grad_map [ var ] for var in local_vars ] for g , grad_var in zip ( grads , grad_vars ) : update_ops . append ( state_ops . assign_add ( grad_var , g ) ) global_center_vars = [ self . _global_map [ var ] for var in local_vars ] # update global variables. def _Update_global_variables ( ) : global_norm = [ ] # a = a / t for g in grad_vars : global_norm . append ( state_ops . assign ( g , g / self . _period ) ) # apply with ops . control_dependencies ( global_norm ) : apply_global_op = self . _opt . apply_gradients ( zip ( grad_vars , global_center_vars ) ) # pull with ops . control_dependencies ( [ apply_global_op ] ) : update_ops = [ ] if global_step : with ops . colocate_with ( global_step ) : update_ops . append ( state_ops . assign_add ( global_step , 1 ) ) for lvar in local_vars : g_val = self . _global_map [ lvar ] . read_value ( ) update_ops . append ( state_ops . assign ( lvar , g_val ) ) for grad_var in grad_vars : update_ops . append ( state_ops . assign ( grad_var , array_ops . zeros_like ( grad_var ) ) ) variable_update = control_flow_ops . group ( * ( update_ops ) ) return variable_update local_update = state_ops . assign_add ( self . _local_step , 1 , name = 'local_step_update' ) . op with ops . control_dependencies ( [ local_update ] ) : condition = math_ops . equal ( math_ops . mod ( self . _local_step , self . _period ) , 0 ) with ops . control_dependencies ( update_ops ) : conditional_update = control_flow_ops . cond ( condition , _Update_global_variables , control_flow_ops . no_op ) return conditional_update