Пример #1
0
    def test_create_base_model(self):
        # Given
        architecture = CnnBaseArchitecture()
        architecture.build_from_file('test/resources/complex_architecture.txt')

        # When
        generator = ModelGenerator(architecture, 'keras')
        actual = generator.create_base_model()

        # Then
        self.assertEqual(19, len(actual.model._layers))
Пример #2
0
    def test_get_last_empty_mutation(self):
        # Given
        expected = CnnBaseArchitecture()
        expected = expected.build_from_file(
            'test/resources/complex_architecture.txt')

        architecture = CnnBaseArchitecture()
        architecture.build_from_file('test/resources/complex_architecture.txt')

        # When
        generator = ModelGenerator(architecture, 'keras')
        mutated_arch = generator.get_last_mutation()

        # Then
        self.assertEqual(str(expected), str(mutated_arch))
Пример #3
0
    def test_unknown_builder(self):
        # Given
        architecture = CnnBaseArchitecture()

        # When
        generator = ModelGenerator(architecture, 'foo')

        # Then
        self.assertRaises(Exception, generator.create_base_model)
Пример #4
0
    def test_create_mutated_model_removal(self):
        # Given
        expected = CnnBaseArchitecture()
        expected = expected.build_from_file(
            'test/resources/mutated_architecture_removal.txt')

        architecture = CnnBaseArchitecture()
        architecture.build_from_file('test/resources/complex_architecture.txt')

        # When
        generator = ModelGenerator(architecture,
                                   'keras',
                                   removal_rate=0.5,
                                   duplication_rate=0,
                                   amendment_rate=0,
                                   seed=13)
        actual = generator.create_mutated_model()
        mutated_arch = generator.get_last_mutation()

        # Then
        self.assertEqual(9, len(actual.model._layers))
        self.assertEqual(str(expected), str(mutated_arch))
Пример #5
0
    def test_real(self):
        # Given
        expected = 30.6
        expected_architecture = CnnBaseArchitecture((32, 32, 3), 10)
        expected_architecture = expected_architecture.build_from_file(
            'test/resources/expected_architecture.txt')
        seed(13)
        set_random_seed(13)

        x_train = np.load('test/resources/x_train.npy')
        y_train = np.load('test/resources/y_train.npy')
        x_test = np.load('test/resources/x_test.npy')
        y_test = np.load('test/resources/y_test.npy')

        # Creating the architecture from file
        arch = CnnBaseArchitecture((32, 32, 3), 10)
        arch = arch.build_from_file('test/resources/architecture.txt')

        # Creating the generator
        generator = ModelGenerator(arch,
                                   'keras',
                                   removal_rate=0.2,
                                   duplication_rate=0.2,
                                   amendment_rate=0.5,
                                   seed=13)

        # Performing Predictive Hyper Opt
        hp_searcher = PredictiveHyperOpt(total_models_count=10,
                                         full_models_count=1,
                                         final_epoch=3,
                                         partial_epochs=[2, 1],
                                         final_models_count=2,
                                         batch_size=250)

        actual, architecture = hp_searcher.run(generator, x_train, y_train,
                                               x_test, y_test)

        # Then
        self.assertEquals(expected, actual)
        self.assertEquals(str(expected_architecture), architecture)
Пример #6
0
    def test_populate_builder_history(self):
        # Given
        expected = CnnBaseArchitecture()
        expected = expected.build_from_file(
            'test/resources/mutated_architecture_history_duplicate.txt')

        architecture = CnnBaseArchitecture()
        architecture.build_from_file('test/resources/complex_architecture.txt')

        # When
        generator = ModelGenerator(architecture,
                                   'keras',
                                   removal_rate=0,
                                   duplication_rate=0.8,
                                   amendment_rate=0,
                                   seed=14)
        generator.populate_history('test/resources/history', 'test-arch-', 3)
        actual = generator.create_mutated_model()
        mutated_arch = generator.get_last_mutation()

        # Then
        self.assertEqual(24, len(actual.model._layers))
        self.assertEqual(str(expected), str(mutated_arch))