'Compute adversarial loss based on FLAGS.adv_training_method.'

def adversarial_loss(self):

    def random_perturbation_loss():
        return adv_lib.random_perturbation_loss_bidir(self.tensors['cl_embedded'], self.cl_inputs[0].length, self.cl_loss_from_embedding)
    def adversarial_loss():
        return adv_lib.adversarial_loss_bidir(self.tensors['cl_embedded'], self.tensors['cl_loss'], self.cl_loss_from_embedding)
    def virtual_adversarial_loss():
        "Computes    virtual    adversarial    loss.\n\n                        Uses    lm_inputs    and    constructs    the    language    model    graph    if    it    hasn't    yet\n                        been    constructed.\n\n                        Also    ensures    that    the    LM    input    states    are    saved    for    LSTM    state-saving\n                        BPTT.\n\n                        Returns:\n                                loss:    float    scalar.\n                        "
        if (self.lm_inputs is None):
            self.language_model_graph(compute_loss=False)
        def logits_from_embedding(embedded, return_next_state=False):
            (_, next_states, logits, _) = self.cl_loss_from_embedding(embedded, inputs=self.lm_inputs, return_intermediates=True)
            if return_next_state:
                return (next_states, logits)
            else:
                return logits
        lm_embedded = (self.tensors['lm_embedded'], self.tensors['lm_embedded_reverse'])
        (next_states, lm_cl_logits) = logits_from_embedding(lm_embedded, return_next_state=True)
        va_loss = adv_lib.virtual_adversarial_loss_bidir(lm_cl_logits, lm_embedded, self.lm_inputs, logits_from_embedding)
        saves = [inp.save_state(state) for (inp, state) in zip(self.lm_inputs, next_states)]
        with tf.control_dependencies(saves):
            va_loss = tf.identity(va_loss)
        return va_loss
    def combo_loss():
        return (adversarial_loss() + virtual_adversarial_loss())
    adv_training_methods = {'rp': random_perturbation_loss, 'at': adversarial_loss, 'vat': virtual_adversarial_loss, 'atvat': combo_loss, '': (lambda : tf.constant(0.0)), None: (lambda : tf.constant(0.0))}
    with tf.name_scope('adversarial_loss'):
        return adv_training_methods[FLAGS.adv_training_method]()
