예제 #1
0
    def test_true_positive_rate(self):
        """Checks that `true_positive_rate` calculates the right quantity."""
        # For the penalty, the default loss is hinge.
        expected_penalty_numerator = np.sum(
            np.maximum(0.0, 1.0 + self._penalty_predictions) *
            (self._penalty_labels > 0.0) * self._penalty_weights *
            self._penalty_predicate)
        expected_penalty_denominator = np.sum(
            (self._penalty_labels > 0.0) * self._penalty_weights *
            self._penalty_predicate)
        expected_penalty_value = (expected_penalty_numerator /
                                  expected_penalty_denominator)

        # For the constraint, the default loss is zero-one.
        expected_constraint_numerator = np.sum(
            (0.5 * (1.0 + np.sign(self._constraint_predictions))) *
            (self._constraint_labels > 0.0) * self._constraint_weights *
            self._constraint_predicate)
        expected_constraint_denominator = np.sum(
            (self._constraint_labels > 0.0) * self._constraint_weights *
            self._constraint_predicate)
        expected_constraint_value = (expected_constraint_numerator /
                                     expected_constraint_denominator)

        actual_expression = binary_rates.true_positive_rate(
            self._split_context)
        self._check_rates(expected_penalty_value, expected_constraint_value,
                          actual_expression)
 def problem_fn(logits, labels, features, weight_column=None):
     # Returns a `RateMinimizationProblem` minimizing error rate subject to
     # recall >= recall_target.
     del features
     del weight_column
     context = subsettable_context.rate_context(logits, labels)
     objective = binary_rates.error_rate(context)
     constraints = [
         binary_rates.true_positive_rate(context) >= recall_target
     ]
     return rate_minimization_problem.RateMinimizationProblem(
         objective, constraints)