Exemple #1
0
    def test_accuracy(self):
        y_pred = tf.constant([[1.0, 2.0, 3.0, 4.0, 6.0, 5.0]])
        loss_fn = losses.SoftErrorLoss(topk=1.0, power=1.0)
        acc = loss_fn.accuracy([2], y_pred)
        self.assertGreaterEqual(acc, 0.0)
        self.assertLessEqual(acc, 1.0)
        acc2 = loss_fn.accuracy([4], y_pred)
        self.assertGreater(acc2, acc)

        # Test that topk gives the same accuracy for all top 3.
        loss_fn = losses.SoftErrorLoss(topk=3, power=1.0)
        acc = loss_fn.accuracy([3], y_pred)
        acc2 = loss_fn.accuracy([4], y_pred)
        self.assertAllClose(acc, acc2)
        self.assertAllClose(acc, tf.constant([1.0]))

        acc3 = loss_fn.accuracy([0], y_pred)
        self.assertLess(acc3, 1.0)
Exemple #2
0
    def test_softerror(self):
        y_pred = tf.constant([[1.0, 2.0, 3.0, 4.0, 6.0, 5.0]])
        loss_fn = losses.SoftErrorLoss(topk=1.0, power=1.0)

        loss = loss_fn([2], y_pred)
        self.assertGreaterEqual(loss, 0.0)
        self.assertLessEqual(loss, 1.0)

        # A better prediction should lead to a lower loss.
        loss2 = loss_fn([4], y_pred)
        self.assertLessEqual(loss2, loss)