research/gam/gam/trainer/trainer_classification.py [484:528]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    left = tf.concat((labels_ll_left, labels_lu_left, predictions_uu_left),
                     axis=0)
    right = tf.concat(
        (predictions_ll_right, predictions_lu_right, predictions_uu_right),
        axis=0)
    dists = tf.reduce_sum(tf.square(left - right), axis=-1)

    # Estimate a weight for each distance, depending on the predictions
    # of the agreement model. For the labeled samples, we can use the actual
    # agreement between the labels, no need to estimate.
    agreement_ll = tf.cast(
        tf.equal(labels_ll_left_idx, labels_ll_right_idx), dtype=tf.float32)
    _, agreement_lu, _, _ = self.trainer_agr.create_agreement_prediction(
        src_features=features_lu_left,
        tgt_features=features_lu_right,
        is_train=is_train,
        src_indices=indices_lu_left,
        tgt_indices=indices_lu_right)
    _, agreement_uu, _, _ = self.trainer_agr.create_agreement_prediction(
        src_features=features_uu_left,
        tgt_features=features_uu_right,
        is_train=is_train,
        src_indices=indices_uu_left,
        tgt_indices=indices_uu_right)
    agreement = tf.concat((agreement_ll, agreement_lu, agreement_uu), axis=0)
    if self.penalize_neg_agr:
      # Since the agreement is predicting scores between [0, 1], anything
      # under 0.5 should represent disagreement. Therefore, we want to encourage
      # agreement whenever the score is > 0.5, otherwise don't incur any loss.
      agreement = tf.nn.relu(agreement - 0.5)

    # Create a Tensor containing the weights assigned to each pair in the
    # agreement regularization loss, depending on how many samples in the pair
    # were labeled. This weight can be either reg_weight_ll, reg_weight_lu,
    # or reg_weight_uu.
    num_ll = tf.shape(predictions_ll_right)[0]
    num_lu = tf.shape(predictions_lu_right)[0]
    num_uu = tf.shape(predictions_uu_left)[0]
    weights = tf.concat(
        (self.reg_weight_ll * tf.ones(num_ll,), self.reg_weight_lu *
         tf.ones(num_lu,), self.reg_weight_uu * tf.ones(num_uu,)),
        axis=0)

    # Scale each distance by its agreement weight and regularzation weight.
    loss = tf.reduce_mean(dists * weights * agreement)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



research/gam/gam/trainer/trainer_classification_gcn.py [512:556]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    left = tf.concat((labels_ll_left, labels_lu_left, predictions_uu_left),
                     axis=0)
    right = tf.concat(
        (predictions_ll_right, predictions_lu_right, predictions_uu_right),
        axis=0)
    dists = tf.reduce_sum(tf.square(left - right), axis=-1)

    # Estimate a weight for each distance, depending on the predictions
    # of the agreement model. For the labeled samples, we can use the actual
    # agreement between the labels, no need to estimate.
    agreement_ll = tf.cast(
        tf.equal(labels_ll_left_idx, labels_ll_right_idx), dtype=tf.float32)
    _, agreement_lu, _, _ = self.trainer_agr.create_agreement_prediction(
        src_features=features_lu_left,
        tgt_features=features_lu_right,
        is_train=is_train,
        src_indices=indices_lu_left,
        tgt_indices=indices_lu_right)
    _, agreement_uu, _, _ = self.trainer_agr.create_agreement_prediction(
        src_features=features_uu_left,
        tgt_features=features_uu_right,
        is_train=is_train,
        src_indices=indices_uu_left,
        tgt_indices=indices_uu_right)
    agreement = tf.concat((agreement_ll, agreement_lu, agreement_uu), axis=0)
    if self.penalize_neg_agr:
      # Since the agreement is predicting scores between [0, 1], anything
      # under 0.5 should represent disagreement. Therefore, we want to encourage
      # agreement whenever the score is > 0.5, otherwise don't incur any loss.
      agreement = tf.nn.relu(agreement - 0.5)

    # Create a Tensor containing the weights assigned to each pair in the
    # agreement regularization loss, depending on how many samples in the pair
    # were labeled. This weight can be either reg_weight_ll, reg_weight_lu,
    # or reg_weight_uu.
    num_ll = tf.shape(predictions_ll_right)[0]
    num_lu = tf.shape(predictions_lu_right)[0]
    num_uu = tf.shape(predictions_uu_left)[0]
    weights = tf.concat(
        (self.reg_weight_ll * tf.ones(num_ll,), self.reg_weight_lu *
         tf.ones(num_lu,), self.reg_weight_uu * tf.ones(num_uu,)),
        axis=0)

    # Scale each distance by its agreement weight and regularzation weight.
    loss = tf.reduce_mean(dists * weights * agreement)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



