def get_lambda_permutation_loss(labels, predicted_scores): tmp_labels = tf.cast(tf.squeeze(labels), tf.int32) y, idx = tf.unique(tmp_labels) which_rank = tf.shape(y)[0] - 1 which_rank = tf.cast(which_rank, tf.float32) raw_pairwise_label_scores = scores.get_pairwise_scores(labels) mask = masks.list_mask(raw_pairwise_label_scores) # calculate delta_z gains = 2**labels - 1 raw_pairwise_scores = scores.get_pairwise_scores(predicted_scores) score_mask = masks.list_mask(raw_pairwise_scores) ranks = tf.reduce_sum(score_mask, axis=1) ranks = tf.reshape(ranks, [-1, 1]) log_2 = tf.log(tf.constant(2.0, dtype=tf.float32)) cg_discounts = tf.log(ranks + 2.0) / log_2 cg_discounts = tf.reshape(cg_discounts, [-1, 1]) raw_pairwise_discounts = tf.abs(scores.get_pairwise_scores(cg_discounts)) raw_pairwise_gains = tf.abs(scores.get_pairwise_scores(gains)) delta_z = raw_pairwise_gains delta_z = tf.multiply(raw_pairwise_discounts, raw_pairwise_gains) abs_pairwise_scores = tf.abs(raw_pairwise_scores) delta_z = tf.divide(delta_z, abs_pairwise_scores + 1e-7) # max_dcg = tf.reduce_sum(math_fns.cal_idcg_ks(labels, 10)) max_dcg = tf.reduce_sum( math_fns.cal_idcg_ks(labels, array_ops.size(predicted_scores))) delta_z = tf.divide(delta_z, max_dcg) delta_mask = tf.multiply(mask, delta_z) delta_mask = tf.stop_gradient(delta_mask) tmp_denominator = tf.matmul(delta_mask, tf.exp(predicted_scores)) multi_label_loss = tf.log(1 + tmp_denominator / tf.exp(predicted_scores)) permutation_loss = tf.reduce_sum(multi_label_loss) permutation_loss /= which_rank return permutation_loss
def get_lambda_residual(labels, predicted_scores): tmp_labels = tf.cast(tf.squeeze(labels), tf.int32) y, idx = tf.unique(tmp_labels) which_rank = tf.shape(y)[0] - 1 which_rank = tf.cast(which_rank, tf.float32) exp_predicted_scores = tf.exp(predicted_scores) raw_pairwise_label_scores = scores.get_pairwise_scores(labels) # gradient with respect to d d_mask = masks.list_mask(raw_pairwise_label_scores) # calculate delta_z gains = 2**labels - 1 raw_pairwise_scores = scores.get_pairwise_scores(predicted_scores) score_mask = masks.list_mask(raw_pairwise_scores) ranks = tf.reduce_sum(score_mask, axis=1) ranks = tf.reshape(ranks, [-1, 1]) log_2 = tf.log(tf.constant(2.0, dtype=tf.float32)) cg_discounts = tf.log(ranks + 2.0) / log_2 cg_discounts = tf.reshape(cg_discounts, [-1, 1]) raw_pairwise_discounts = tf.abs(scores.get_pairwise_scores(cg_discounts)) raw_pairwise_gains = tf.abs(scores.get_pairwise_scores(gains)) delta_z = raw_pairwise_gains delta_z = tf.multiply(raw_pairwise_discounts, raw_pairwise_gains) abs_pairwise_scores = tf.abs(raw_pairwise_scores) delta_z = tf.divide(delta_z, abs_pairwise_scores+1e-7) # max_dcg = tf.reduce_sum(math_fns.cal_idcg_ks(labels, 10)) max_dcg = tf.reduce_sum(math_fns.cal_idcg_ks(labels, array_ops.size(predicted_scores))) delta_z = tf.divide(delta_z, max_dcg) delta_d_mask = tf.multiply(d_mask, delta_z) delta_d_mask = tf.stop_gradient(delta_d_mask) tmp_denominator = tf.matmul(d_mask, exp_predicted_scores) # loss_score_gradients residuals = 1 / (1 + exp_predicted_scores / tmp_denominator) gains = 2**labels - 1 # gain_sum = tf.reduce_sum(gains) # residuals /= gain_sum # residuals /= which_rank # residuals /= num_not_min # gradient with respect to d' _tmp_denominator = tmp_denominator + exp_predicted_scores _d_mask= masks.list_negative_mask(raw_pairwise_label_scores) delta__d_mask = tf.multiply(_d_mask, delta_z) delta__d_mask = tf.stop_gradient(delta__d_mask) _residuals = tf.matmul(delta__d_mask, 1 / _tmp_denominator) _residuals *= exp_predicted_scores residuals += _residuals residuals /= which_rank return residuals
def get_permutation_loss(labels, predicted_scores): tmp_labels = tf.cast(tf.squeeze(labels), tf.int32) y, idx = tf.unique(tmp_labels) which_rank = tf.shape(y)[0] - 1 which_rank = tf.cast(which_rank, tf.float32) raw_pairwise_label_scores = scores.get_pairwise_scores(labels) mask = masks.list_mask(raw_pairwise_label_scores) tmp_denominator = tf.matmul(mask, tf.exp(predicted_scores)) multi_label_loss = tf.log(1 + tmp_denominator / tf.exp(predicted_scores)) multi_label_loss *= 2**labels - 1 permutation_loss = tf.reduce_sum(multi_label_loss) permutation_loss /= which_rank return permutation_loss
def get_residual(labels, predicted_scores): tmp_labels = tf.cast(tf.squeeze(labels), tf.int32) y, idx = tf.unique(tmp_labels) which_rank = tf.shape(y)[0] - 1 which_rank = tf.cast(which_rank, tf.float32) exp_predicted_scores = tf.exp(predicted_scores) raw_pairwise_label_scores = scores.get_pairwise_scores(labels) # gradient with respect to d d_mask = masks.list_mask(raw_pairwise_label_scores) tmp_denominator = tf.matmul(d_mask, exp_predicted_scores) # loss_score_gradients residuals = 1 / (1 + exp_predicted_scores / tmp_denominator) gains = 2**labels - 1 residuals *= gains # gradient with respect to d' _tmp_denominator = tmp_denominator + exp_predicted_scores _d_mask= masks.list_negative_mask(raw_pairwise_label_scores) _residuals = tf.matmul(_d_mask, gains / _tmp_denominator) _residuals *= exp_predicted_scores residuals += _residuals residuals /= which_rank return residuals