def test_poison_ensemble(self):
        classifier_1 = DummyClassifier()
        attack_1 = DummyPoisonAttack()
        classifier_2 = DummyClassifier()
        attack_2 = DummyPoisonAttack()
        attack_pairs = AttackPairs()
        attack_pairs.add(classifier_1, attack_1, 1)
        attack_pairs.add(classifier_2, attack_2, 1)
        attack_pairs.fit_all(X, Y)

        ensemble = PoisonEnsemble(attack_pairs, X)
        defender = DummyClassifier()
        ensemble.poison(defender)
 def test_fit_predict_all(self):
     """Test that you can fit and predict on all classifiers"""
     classifier_1 = DummyClassifier()
     attack_1 = DummyPoisonAttack()
     classifier_2 = DummyClassifier()
     attack_2 = DummyPoisonAttack()
     attack_pairs = AttackPairs()
     attack_pairs.add(classifier_1, attack_1, 1)
     attack_pairs.add(classifier_2, attack_2, 1)
     attack_pairs.fit_all(X, Y)
     y_out = attack_pairs.predict_all(X)
     assert isinstance(y_out, np.ndarray)
     assert y_out.shape == (X.shape[0], 2)
Beispiel #3
0
 def test_classifier_output(self):
     """Test that the output is a numpy array of the correct size.
     """
     model = DummyClassifier()
     model.fit(X, Y)
     model.partial_fit(X, Y)
     assert isinstance(model.predict(X_TEST_1), np.ndarray)
     assert isinstance(model.predict(X_TEST_2), np.ndarray)
     assert model.predict(X_TEST_1).shape == (X_TEST_1.shape[0],)
     assert model.predict(X_TEST_2).shape == (X_TEST_2.shape[0],)
    def test_get_attack_point(self):
        """Test that you can get an attack point from a particular attack"""
        classifier_1 = DummyClassifier()
        attack_1 = DummyPoisonAttack()
        classifier_2 = DummyClassifier()
        attack_2 = DummyPoisonAttack()
        attack_pairs = AttackPairs()
        attack_pairs.add(classifier_1, attack_1, 1)
        attack_pairs.add(classifier_2, attack_2, 1)
        attack_pairs.fit_all(X, Y)

        (x_attack_1, y_attack_1) = attack_pairs.get_attack_point(0)
        assert isinstance(x_attack_1, np.ndarray)
        assert x_attack_1.shape == (1, 2)

        (x_attack_2, y_attack_2) = attack_pairs.get_attack_point(1)
        assert isinstance(x_attack_1, np.ndarray)
        assert x_attack_1.shape == (1, 2)
 def test_get_beliefs(self):
     """Test that you can get the current beliefs as a numpy array"""
     classifier = DummyClassifier()
     attack = DummyPoisonAttack()
     attack_pairs = AttackPairs()
     assert np.array_equal(attack_pairs.get_beliefs(), np.zeros((0, )))
     attack_pairs.add(classifier, attack, 1)
     assert np.array_equal(attack_pairs.get_beliefs(), np.array([1]))
     attack_pairs.add(classifier, attack, 1)
     assert np.array_equal(attack_pairs.get_beliefs(), np.array([1, 1]))
 def test_add_attack(self):
     """Test that an attack can be added"""
     classifier = DummyClassifier()
     attack = DummyPoisonAttack()
     attack_pairs = AttackPairs()
     attack_pairs.add(classifier, attack, 1)
     assert attack_pairs.attack_pairs == [{
         'classifier': classifier,
         'attack': attack,
         'belief': 1
     }]
    def test_normalize_belief(self):
        """Test that the beliefs can be normalized"""
        classifier = DummyClassifier()
        attack = DummyPoisonAttack()
        attack_pairs = AttackPairs()

        attack_pairs.add(classifier, attack, 1)
        attack_pairs.add(classifier, attack, 1)
        attack_pairs.add(classifier, attack, 1)

        attack_pairs.normalize_beliefs()
        assert np.array_equal(attack_pairs.get_beliefs(),
                              np.array([1.0, 1.0, 1.0]) / 3)

        rand_beliefs_3 = np.random.rand(3)
        attack_pairs.set_beliefs(rand_beliefs_3)
        attack_pairs.normalize_beliefs()
        assert np.array_equal(attack_pairs.get_beliefs(),
                              rand_beliefs_3 / np.sum(rand_beliefs_3))
    def test_set_beliefs(self):
        """Test that you can set the current beliefs as a numpy array"""
        classifier = DummyClassifier()
        attack = DummyPoisonAttack()
        attack_pairs = AttackPairs()

        attack_pairs.add(classifier, attack, 1)
        rand_beliefs_1 = np.random.rand(1)
        attack_pairs.set_beliefs(rand_beliefs_1)
        assert np.array_equal(attack_pairs.get_beliefs(), rand_beliefs_1)

        attack_pairs.add(classifier, attack, 1)
        rand_beliefs_2 = np.random.rand(2)
        attack_pairs.set_beliefs(rand_beliefs_2)
        assert np.array_equal(attack_pairs.get_beliefs(), rand_beliefs_2)

        attack_pairs.add(classifier, attack, 1)
        rand_beliefs_3 = np.random.rand(3)
        attack_pairs.set_beliefs(rand_beliefs_3)
        assert np.array_equal(attack_pairs.get_beliefs(), rand_beliefs_3)