import tensorflow as tf

def apply_optimizers(objectives, trainer, config):
    # Make sure all losses are computed and apply loss scales.
    processed = []
    values = [ob.value for ob in objectives]
    for ob in objectives:
        loss = {min: ob.value, max: -ob.value}[ob.goal]
        loss *= config.loss_scales[ob.name]
        with tf.control_dependencies(values):
            loss = tf.identity(loss)
        processed.append(ob._replace(value=loss, goal=min))
    # Merge objectives that operate on the whole model to compute only one
    # backward pass and to share optimizer statistics.
    objectives = []
    losses = []
