Пример #1
0
    def test_bias_influence(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 = SimulationMajorityLTFArray.normal_weights(
            n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556))
        bias_array = SimulationMajorityLTFArray.normal_weights(
            1,
            k,
            mu=mu,
            sigma=sigma * 2,
            random_instance=RandomState(0xBADAFF1))

        biased_ltf_array = SimulationMajorityLTFArray(
            weight_array=weight_array,
            transform=SimulationMajorityLTFArray.transform_id,
            combiner=SimulationMajorityLTFArray.combiner_xor,
            sigma_noise=sigma,
            random_instance_noise=RandomState(0xCCABAD),
            bias=bias_array,
        )
        ltf_array = SimulationMajorityLTFArray(
            weight_array=weight_array,
            transform=SimulationMajorityLTFArray.transform_id,
            combiner=SimulationMajorityLTFArray.combiner_xor,
            sigma_noise=sigma,
            random_instance_noise=RandomState(0xCCABAD),
            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_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 = LTFArray.normal_weights(
            n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556))
        bias_array = LTFArray.normal_weights(
            1,
            k,
            mu=mu,
            sigma=sigma * 2,
            random_instance=RandomState(0xBADAFF1))

        biased_ltf_array = LTFArray(
            weight_array=weight_array,
            transform=LTFArray.transform_id,
            combiner=LTFArray.combiner_xor,
            bias=bias_array,
        )
        ltf_array = LTFArray(
            weight_array=weight_array,
            transform=LTFArray.transform_id,
            combiner=LTFArray.combiner_xor,
            bias=None,
        )
        # the second dimension of the weight_array must be the number of elements in biased weight_array
        self.assertEqual(
            shape(ltf_array.weight_array)[1],
            shape(biased_ltf_array.weight_array)[1])
        assert_array_equal(biased_ltf_array.weight_array[:, :n],
                           ltf_array.weight_array[:, :n])
        assert_array_equal(biased_ltf_array.weight_array[:, :n], weight_array)
        assert_array_equal(biased_ltf_array.weight_array[:, n],
                           reshape(bias_array, (k, )))
        assert_array_equal(ltf_array.weight_array[:, n], zeros((k, )))

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

        self.assertFalse(array_equal(biased_responses, responses))
Пример #3
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 = SimulationMajorityLTFArray.normal_weights(n, k, mu=mu, sigma=sigma,
                                                                 random_instance=RandomState(0xBADA556))
        bias_value = 2.5

        biased_ltf_array = SimulationMajorityLTFArray(
            weight_array=weight_array,
            transform=SimulationMajorityLTFArray.transform_id,
            combiner=SimulationMajorityLTFArray.combiner_xor,
            sigma_noise=sigma,
            random_instance_noise=RandomState(0xCCABAD),
            bias=bias_value,
        )
        ltf_array = SimulationMajorityLTFArray(
            weight_array=weight_array,
            transform=SimulationMajorityLTFArray.transform_id,
            combiner=SimulationMajorityLTFArray.combiner_xor,
            sigma_noise=sigma,
            random_instance_noise=RandomState(0xCCABAD),
            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 tests.
        self.assertTrue(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))
Пример #4
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 = 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,
        )
        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]
        # 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))
Пример #5
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 = array(list(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,
        )
        # 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[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))
Пример #6
0
 def test_all_inputs(self):
     """This checks the shape and type of the returned multidimensional array."""
     n = 8
     N = 2**n
     arr = all_inputs(n)
     self.check_multi_dimensional_array(arr, N, n, BIT_TYPE)