Example #1
0
def test_sparse_top_k_categorical_accuracy():
    y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
    y_true = K.variable(np.array([[1], [0]]))
    success_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))
    assert success_result == 1
    partial_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))
    assert partial_result == 0.5
    failure_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))
    assert failure_result == 0
Example #2
0
def test_sparse_top_k_categorical_accuracy():
    y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
    y_true = K.variable(np.array([[1], [0]]))
    success_result = K.eval(metrics.sparse_top_k_categorical_accuracy(y_true, y_pred,
                                                                      k=3))
    assert success_result == 1
    partial_result = K.eval(metrics.sparse_top_k_categorical_accuracy(y_true, y_pred,
                                                                      k=2))
    assert partial_result == 0.5
    failure_result = K.eval(metrics.sparse_top_k_categorical_accuracy(y_true, y_pred,
                                                                      k=1))
    assert failure_result == 0
Example #3
0
def test_sparse_top_k_categorical_accuracy(y_pred, y_true):
    y_pred = K.variable(y_pred)
    y_true = K.variable(y_true)
    success_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))

    assert np.mean(success_result) == 1
    partial_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))

    assert np.mean(partial_result) == 0.5
    failure_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))

    assert np.mean(failure_result) == 0
Example #4
0
def test_sparse_top_k_categorical_accuracy(y_pred, y_true):
    y_pred = K.variable(y_pred)
    y_true = K.variable(y_true)
    success_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))

    assert success_result == 1
    partial_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))

    assert partial_result == 0.5
    failure_result = K.eval(
        metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))

    assert failure_result == 0
Example #5
0
    def test_sparse_top_k_categorical_accuracy(self):
        with self.cached_session():
            # Test correctness if the shape of y_true is (num_samples, 1)
            y_pred = backend.variable(
                np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
            y_true = backend.variable(np.array([[1], [0]]))
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))
            self.assertEqual(np.mean(result), 1)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))
            self.assertEqual(np.mean(result), 0.5)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))
            self.assertEqual(np.mean(result), 0.0)

            # Test correctness if the shape of y_true is (num_samples,)
            y_pred = backend.variable(
                np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
            y_true = backend.variable(np.array([1, 0]))
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))
            self.assertEqual(np.mean(result), 1)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))
            self.assertEqual(np.mean(result), 0.5)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))
            self.assertEqual(np.mean(result), 0.0)

            # Test correctness if the shape of y_true is (batch_size,
            # seq_length) and y_pred is (batch_size, seq_length, num_classes)
            y_pred = backend.variable(
                np.array([
                    [[0.3, 0.2, 0.1], [0.1, 0.2, 0.7], [0.1, 0.2, 0.7]],
                    [[0.3, 0.2, 0.1], [0.1, 0.2, 0.7], [0.3, 0.2, 0.1]],
                ]))
            y_true = backend.variable(np.array([[1, 0, 0], [1, 0, 1]]))
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3))
            self.assertEqual(np.mean(result), 1)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=2))
            self.assertEqual(np.mean(result), 0.5)
            result = backend.eval(
                metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=1))
            self.assertEqual(np.mean(result), 0.0)
Example #6
0
def top_10_categorical_accuracy(y_true, y_pred):
    return sparse_top_k_categorical_accuracy(y_true, y_pred, k=10)
Example #7
0
def top_2_error(y_true, y_pred):
    return 1 - sparse_top_k_categorical_accuracy(y_true, y_pred, k=2)
Example #8
0
def top_3_accuracy(y_true, y_pred):
    """
        Customize keras metrics
        func
        """
    return metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=3)
Example #9
0
def sparse_top_k_categorical_accuracy_3(*args):

    return sparse_top_k_categorical_accuracy(*args, k=3)