def _build_encoder_agreement_loss(self):
    """Builds an agreement loss that enforces consistency of the encodings.

    Returns:
      loss: <float32> [] for the agreement losses.

    Raises:
      ValueError: if loss_name is not in {"cosine", "entropy", "mse"}.
    """
    aux_keys = ["src>aux", "tgt>aux"]

    # Encode (if necessary).
    for key in aux_keys:
      if key not in self.enc_outputs:
        encode_func = self.get_encode_func(*self.inputs[key])
        self.enc_outputs[key] = encode_func()

    with tf.name_scope("enc_agreement_loss"):
      # Build loss.
      if self._hparams.enc_agreement_loss in {"cosine", "l2"}:
        if self._hparams.enc_agreement_pool:
          preproc_op_type = "max_pool"
        else:
          preproc_op_type = "truncate"
        enc_src, enc_tgt = model_utils.make_sequences_compatible(
            self.enc_outputs["src>aux"].outputs,
            self.enc_outputs["tgt>aux"].outputs,
            op_type=preproc_op_type)
        if self._hparams.enc_agreement_loss == "cosine":
          dist_fn = functools.partial(losses.cosine_distance, normalize=True)
        else:
          dist_fn = functools.partial(losses.l2_distance, normalize=True)
        aux_loss_fn = losses.DistanceLoss(dist_fn)
        aux_loss = aux_loss_fn(enc_src, enc_tgt)
      elif self._hparams.enc_agreement_loss in {"xatt_cosine", "xatt_l2"}:
        if self._hparams.enc_agreement_loss.endswith("cosine"):
          dist_fn = functools.partial(losses.cosine_distance, normalize=True)
        else:
          dist_fn = functools.partial(losses.l2_distance, normalize=True)
        aux_loss_fn = losses.CrossAttentionDistanceLoss(dist_fn=dist_fn)
        aux_loss = aux_loss_fn(self.enc_outputs["src>aux"].outputs,
                               self.enc_outputs["tgt>aux"].outputs)
      else:
        raise ValueError("Unknown auxiliary loss: %s." %
                         self._hparams.enc_agreement_loss)

      aux_loss = self._hparams.enc_agreement_coeff * aux_loss

    return aux_loss
Builds an agreement loss that enforces consistency of the encodings.

    Returns:
      loss: <float32> [] for the agreement losses.

    Raises:
      ValueError: if loss_name is not in {"cosine", "entropy", "mse"}.
    
language/labs/consistent_zero_shot_nmt/models/agreement.py