def test_training_set_challenges(self): """The TrainingSet should generate the same challenges for equal seeds.""" n = 8 k = 1 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor N = 1000 instance_prng = RandomState(0x4EFEA) weight_array = LTFArray.normal_weights(n, k, random_instance=instance_prng) instance = LTFArray( weight_array=weight_array, transform=transformation, combiner=combiner, ) challenge_seed = 0xAB17D training_set_1 = TrainingSet( instance=instance, N=N, random_instance=RandomState(challenge_seed)) training_set_2 = TrainingSet( instance=instance, N=N, random_instance=RandomState(challenge_seed)) self.assertTrue( array_equal(training_set_1.challenges, training_set_2.challenges), 'The challenges are not equal.', )
def run(self): self.instance = LTFArray(weight_array=LTFArray.normal_weights( self.n, self.k, random_instance=self.instance_prng), transform=self.transformation, combiner=self.combiner, bias=0.0) validation_size = int((self.N / 1.1) // 10) self.training_set = TrainingSet(instance=self.instance, N=self.N - validation_size, random_instance=self.challenge_prng) self.validation_set = TrainingSet(instance=self.instance, N=validation_size, random_instance=self.distance_prng) self.learner = CorrelationAttack( n=self.n, k=self.k, training_set=self.training_set, validation_set=self.validation_set, weights_prng=self.model_prng, lr_iteration_limit=self.lr_iteration_limit, mini_batch_size=self.mini_batch_size, convergence_decimals=self.convergence_decimals, shuffle=self.shuffle, logger=self.progress_logger, ) self.model = self.learner.learn()
def test_learn_ip_mod2(self): """" Stupid test which gains code coverage """ instance_prng = RandomState(seed=TestLogisticRegression.seed_instance) model_prng = RandomState(seed=TestLogisticRegression.seed_model) instance = LTFArray( weight_array=LTFArray.normal_weights( TestLogisticRegression.n, TestLogisticRegression.k, random_instance=instance_prng), transform=LTFArray.transform_id, combiner=LTFArray.combiner_ip_mod2, ) lr_learner = LogisticRegression( TrainingSet(instance=instance, N=TestLogisticRegression.N), TestLogisticRegression.n, TestLogisticRegression.k, transformation=LTFArray.transform_id, combiner=LTFArray.combiner_xor, weights_prng=model_prng, ) lr_learner.learn()
def prepare(self): super().prepare() self.learner = LogisticRegression( t_set=TrainingSet(instance=self.ltf_array, N=self.parameters.N), n=self.parameters.n, k=self.parameters.k, transformation=self.ltf_array.transform, combiner=self.ltf_array.combiner, convergence_decimals=100, # never converge iteration_limit=20, )
def test_parse_file(self): """This method checks reading challenge-response pairs from a file.""" n, k, N = 128, 1, 10 instance = LTFArray(LTFArray.normal_weights(n, k), LTFArray.transform_atf, LTFArray.combiner_xor) original = TrainingSet(instance, N) f = NamedTemporaryFile('w') for vals in column_stack((original.challenges, original.responses)): f.write(' '.join(map(str, vals)) + '\n') f.flush() loaded = parse_file(f.name, n, in_11_notation=True) assert_array_equal(original.challenges, loaded.challenges) assert_array_equal(original.responses, loaded.responses) f.close()
def test_learn_xor(self): """" Stupid test which gains code coverage """ instance_prng = RandomState(seed=TestLowDegree.seed_instance) instance = LTFArray( weight_array=LTFArray.normal_weights( TestLowDegree.n, TestLowDegree.k, random_instance=instance_prng), transform=LTFArray.transform_id, combiner=LTFArray.combiner_xor, ) low_degree_learner = LowDegreeAlgorithm(TrainingSet(instance=instance, N=TestLowDegree.N), degree=TestLowDegree.degree) low_degree_learner.learn()