Пример #1
0
    def test_bias_influence_array(self):
        """
        This method tests the influence of the bias array. The results should be different.
        """
        n = 8
        k = 4
        mu = 1
        sigma = 0.5

        challenges = tools.all_inputs(n)

        weight_array = NoisyLTFArray.normal_weights(
            n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556))
        bias_array = NoisyLTFArray.normal_weights(
            1,
            k,
            mu=mu,
            sigma=sigma * 2,
            random_instance=RandomState(0xBADAFF1))

        biased_ltf_array = NoisyLTFArray(
            weight_array=weight_array,
            transform=NoisyLTFArray.transform_id,
            combiner=NoisyLTFArray.combiner_xor,
            sigma_noise=sigma,
            bias=bias_array,
        )
        ltf_array = NoisyLTFArray(
            weight_array=weight_array,
            transform=NoisyLTFArray.transform_id,
            combiner=NoisyLTFArray.combiner_xor,
            sigma_noise=sigma,
            bias=None,
        )
        self.assertEqual(ltf_array.weight_array.shape,
                         biased_ltf_array.weight_array.shape)

        bias_array = biased_ltf_array.weight_array[:, -1]
        bias_array_compared = [
            bias == bias_array[0] for bias in bias_array[1:]
        ]
        # the bias values should be different for this test. It is possible that they are all equal but this chance is
        # low.
        self.assertFalse(array(bias_array_compared).all())

        biased_responses = biased_ltf_array.eval(challenges)
        responses = ltf_array.eval(challenges)

        # The arithmetic mean of the res
        self.assertFalse(array_equal(biased_responses, responses))
Пример #2
0
    def test_bias_influence_value(self):
        """
        This method tests the influence of the bias value. The results should be different.
        """
        n = 8
        k = 4
        mu = 1
        sigma = 0.5

        challenges = array(list(tools.all_inputs(n)))

        weight_array = NoisyLTFArray.normal_weights(n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556))
        bias_value = 2.5

        biased_ltf_array = NoisyLTFArray(
            weight_array=weight_array,
            transform=NoisyLTFArray.transform_id,
            combiner=NoisyLTFArray.combiner_xor,
            sigma_noise=sigma,
            bias=bias_value,
        )
        ltf_array = NoisyLTFArray(
            weight_array=weight_array,
            transform=NoisyLTFArray.transform_id,
            combiner=NoisyLTFArray.combiner_xor,
            sigma_noise=sigma,
            bias=None,
        )
        # the second dimension of the weight_array plus one must be the number of elements in biased weight_array
        self.assertEqual(shape(ltf_array.weight_array)[1]+1, shape(biased_ltf_array.weight_array)[1])

        bias_array = biased_ltf_array.weight_array[:, -1]
        bias_array_compared = [bias == bias_array[0] for bias in bias_array]
        # the bias values should be equal for this test.
        self.assertTrue(array(list(bias_array_compared)).all())

        biased_responses = biased_ltf_array.eval(challenges)
        responses = ltf_array.eval(challenges)

        # The arithmetic mean of the res
        self.assertFalse(array_equal(biased_responses, responses))