<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def apply_optimizer(optimizer, losses, regularize=True, include_marked=True, clip_gradients_by_norm=None, **kwargs):
 """Apply an optimizer to the graph and returns a train_op. The resulting operation will minimize the specified losses, plus the regularization losses that have been collected during graph construction and the losses that were marked by calling `mark_as_required`. It will also apply any updates that have been collected (e.g. for moving average summaries). This is equivalent to: total_loss = prettytensor.create_composite_loss( losses=losses, regularize=regularize, include_marked=include_marked) train_op_without_updates = optimizer.minimize(total_loss) train_op = prettytensor.with_update_ops(train_op_without_updates) N.B. Pay special attention to the `gate_gradients` argument to the optimizer. If your graph is large, it will likely train unacceptably slow if you don't specify it as GATE_NONE. Args: optimizer: The optimizer the minimize. losses: A list of losses to apply. regularize: Whether or not to include the regularization losses. include_marked: Whether or not to use the marked losses. clip_gradients_by_norm: If not None, clip gradients by the norm using `tf.clip_by_norm`. **kwargs: Additional arguments to pass into the optimizer. Returns: An operation to use for training that also updates any required ops such as moving averages. """

  books = for_default_graph()

  g_step = kwargs.pop('global_step', books.global_step)
  total_loss = books.create_composite_loss(losses=losses,
                                           regularize=regularize,
                                           include_marked=include_marked)

  grads_and_vars = optimizer.compute_gradients(total_loss, **kwargs)
  if clip_gradients_by_norm is not None:
    clipped_grads_and_vars = []
    for g, v in grads_and_vars:
      if isinstance(g, tf.SparseTensor):
        cg = tf.SparseTensor(
            tf.clip_by_norm(g.values, clip_gradients_by_norm),
            g.indices,
            g.dense_shape)
      elif isinstance(g, tf.IndexedSlices):
        cg = tf.IndexedSlices(
            tf.clip_by_norm(g.values, clip_gradients_by_norm),
            g.indices)
      else:
        cg = tf.clip_by_norm(g, clip_gradients_by_norm)
      clipped_grads_and_vars.append((cg, v))
    grads_and_vars = clipped_grads_and_vars
  train_op = optimizer.apply_gradients(grads_and_vars, global_step=g_step)
  return books.with_update_ops(train_op)