コード例 #1
0
    def test_equal_weights_logits_2d(self):
        """Testing equal weights logits for 2D data"""
        y_true = [[1, 2], [0, 2]]
        y_pred = [[[-0.05, 0.3, 0.19], [0.2, -0.4, 0.12]],
                  [[-0.1, 0.22, -0.73], [0.23, -0.52, 0.2]]]

        self.scce = losses.SparseCategoricalCrossentropy(from_logits=True)
        self.bscce_equal = BalancedSCCE([1, 1, 1], from_logits=True)
        scce = self.scce(y_true, y_pred).numpy()
        bscce = self.bscce_equal(y_true, y_pred).numpy()
        np.testing.assert_array_equal(scce, bscce)
コード例 #2
0
    def test_unequal_weights_reduction_1d(self):
        """Testing unequal weights reductions for 1D data"""
        y_true = [1, 2]
        y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
        sample_weight = np.take(self.class_weights, y_true)

        self.scce = losses.SparseCategoricalCrossentropy(
            reduction=losses.Reduction.SUM)
        self.bscce_unequal = BalancedSCCE(self.class_weights,
                                          reduction=losses.Reduction.SUM)
        scce = self.scce(y_true, y_pred, sample_weight=sample_weight).numpy()
        bscce = self.bscce_unequal(y_true, y_pred).numpy()
        np.testing.assert_array_equal(scce, bscce)

        self.scce = losses.SparseCategoricalCrossentropy(
            reduction=losses.Reduction.NONE)
        self.bscce_unequal = BalancedSCCE(self.class_weights,
                                          reduction=losses.Reduction.NONE)
        scce = self.scce(y_true, y_pred, sample_weight=sample_weight).numpy()
        bscce = self.bscce_unequal(y_true, y_pred).numpy()
        np.testing.assert_array_equal(scce, bscce)
コード例 #3
0
    def test_equal_weights_reduction_2d(self):
        """Testing equal weights reductions for 2D data"""
        y_true = [[1, 2], [0, 2]]
        y_pred = [[[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
                  [[0.1, 0.2, 0.7], [0.3, 0.5, 0.2]]]

        self.scce = losses.SparseCategoricalCrossentropy(
            reduction=losses.Reduction.SUM)
        self.bscce_equal = BalancedSCCE([1, 1, 1],
                                        reduction=losses.Reduction.SUM)
        scce = self.scce(y_true, y_pred).numpy()
        bscce = self.bscce_equal(y_true, y_pred).numpy()
        np.testing.assert_array_equal(scce, bscce)

        self.scce = losses.SparseCategoricalCrossentropy(
            reduction=losses.Reduction.NONE)
        self.bscce_equal = BalancedSCCE([1, 1, 1],
                                        reduction=losses.Reduction.NONE)
        scce = self.scce(y_true, y_pred).numpy()
        bscce = self.bscce_equal(y_true, y_pred).numpy()
        np.testing.assert_array_equal(scce, bscce)