def test_id(self): """ This method test the identity function for the predefined inputs (challenges). If every challenge is duplicated k times the function works correct. """ test_challenges = array([[ [-1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [1, 1, -1, -1, -1], ], [ [1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [-1, 1, -1, -1, -1], ], [ [-1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [-1, 1, -1, -1, -1], ]], dtype=tools.BIT_TYPE) for challenges in test_challenges: assert_array_equal( LTFArray.transform_id(challenges, k=4), [[challenges[0], challenges[0], challenges[0], challenges[0]], [challenges[1], challenges[1], challenges[1], challenges[1]], [challenges[2], challenges[2], challenges[2], challenges[2]]])
def _preprocess(transformation, kind='full'): if kind == 'no': return lambda challenges, _: LTFArray.transform_id(challenges, 1)[:, 0, :] if kind == 'short': def short_transformation(challenges, k): return transformation(challenges, k)[:, 0, :] return short_transformation if kind == 'full': return transformation else: raise Exception('preprocess() only has the options: "no", "short", and "full"')
def test_ltf_eval(self): """ Test ltf_eval for correct evaluation of LTFs. """ #random.normal(loc=0, scale=self.sigma_noise, size=(1, self.k)) weight_prng_1 = RandomState(seed=0xBADA55) weight_prng_2 = RandomState(seed=0xBADA55) noise_prng_1 = RandomState(seed=0xC0FFEE) noise_prng_2 = RandomState(seed=0xC0FFEE) N = 100 # number of random inputs per test set for test_parameters in self.test_set: n = test_parameters[0] k = test_parameters[1] mu = test_parameters[2] sigma = test_parameters[3] transformed_inputs = LTFArray.transform_id( RandomState(seed=0xBAADA555).choice([-1, +1], (N, n)), # bad ass testing k) ltf_array = LTFArray( weight_array=LTFArray.normal_weights(n, k, mu, sigma, weight_prng_1), transform=LTFArray.transform_id, combiner=LTFArray.combiner_xor, ) noisy_ltf_array = NoisyLTFArray( weight_array=LTFArray.normal_weights( n, k, mu, sigma, weight_prng_2 ), # weight_prng_2 was seeded identically to weight_prng_1 transform=LTFArray.transform_id, combiner=LTFArray.combiner_xor, sigma_noise=1, random_instance=noise_prng_1, ) evaled_ltf_array = ltf_array.ltf_eval(transformed_inputs) assert_array_equal( around(evaled_ltf_array + noise_prng_2.normal( loc=0, scale=1, size=(len(evaled_ltf_array), k)), decimals=10), around(noisy_ltf_array.ltf_eval(transformed_inputs), decimals=10))
def test_ltf_eval(self): """ Test ltf_eval for correct evaluation of LTFs. """ N = 100 # number of random inputs per test set def ltf_eval_slow(challenge, weights): """ evaluate a single challenge with a single ltf specified by weights """ assert len(challenge) == len(weights) - 1 return dot(challenge, weights[:n]) + weights[n] # weights[n] is the bias for test_parameters in self.test_set: n = test_parameters[0] k = test_parameters[1] mu = test_parameters[2] sigma = test_parameters[3] inputs = tools.random_inputs(n, N, random_instance=RandomState(0xA1)) ltf_array = LTFArray( weight_array=LTFArray.normal_weights(n, k, mu, sigma), transform=LTFArray.transform_id, combiner=LTFArray.combiner_xor, ) fast_evaluation_result = around(ltf_array.ltf_eval( LTFArray.transform_id(inputs, k)), decimals=8) slow_evaluation_result = [] for challenge in inputs: slow_evaluation_result.append([ ltf_eval_slow(challenge, ltf_array.weight_array[l]) for l in range(k) ]) slow_evaluation_result = around(slow_evaluation_result, decimals=8) self.assertTupleEqual(shape(slow_evaluation_result), (N, k)) self.assertTupleEqual(shape(fast_evaluation_result), (N, k)) assert_array_equal(slow_evaluation_result, fast_evaluation_result)
def test_ltf_eval(self): """ Test ltf_eval for correct evaluation of LTFs. """ N = 100 # number of random inputs per test set def ltf_eval_slow(x, w): """ evaluate a single input x with a single ltf specified by weights w """ assert len(x) == len(w) return dot(x, w) for test_parameters in self.test_set: n = test_parameters[0] k = test_parameters[1] mu = test_parameters[2] sigma = test_parameters[3] inputs = RandomState(seed=0xCAFED00D).choice([-1, +1], (N, n)) ltf_array = LTFArray( weight_array=LTFArray.normal_weights(n, k, mu, sigma), transform=LTFArray.transform_id, combiner=LTFArray.combiner_xor, ) fast_evaluation_result = around(ltf_array.ltf_eval( LTFArray.transform_id(inputs, k)), decimals=10) slow_evaluation_result = [] for c in inputs: slow_evaluation_result.append([ ltf_eval_slow(c, ltf_array.weight_array[l]) for l in range(k) ]) slow_evaluation_result = around(slow_evaluation_result, decimals=10) self.assertTupleEqual(shape(slow_evaluation_result), (N, k)) self.assertTupleEqual(shape(fast_evaluation_result), (N, k)) assert_array_equal(slow_evaluation_result, fast_evaluation_result)
def test_id(self): test_cs = [[ [-1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [1, 1, -1, -1, -1], ], [ [1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [-1, 1, -1, -1, -1], ], [ [-1, 1, 1, -1, -1], [-1, -1, -1, -1, -1], [-1, 1, -1, -1, -1], ]] for cs in test_cs: assert_array_equal( LTFArray.transform_id(cs, k=4), [[cs[0], cs[0], cs[0], cs[0]], [cs[1], cs[1], cs[1], cs[1]], [cs[2], cs[2], cs[2], cs[2]]])