コード例 #1
0
    def test_breed3(self):
        delta = 0.3
        n_parents = 2
        traits = dict(layer_dropout={1})

        model_p = KerasPackageWrapper.make_flat_sequential_model()
        model_p.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_p.add(keras.layers.Dropout(0.1))

        model_c1 = KerasPackageWrapper.make_flat_sequential_model()
        model_c1.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_c1.add(keras.layers.Dropout(0.3))

        model_c2 = KerasPackageWrapper.make_flat_sequential_model()
        model_c2.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_c2.add(keras.layers.Dropout(0.7))

        model_c3 = KerasPackageWrapper.make_flat_sequential_model()
        model_c3.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_c3.add(keras.layers.Dropout(0.9))

        new_generation = GeneticOptimizer.breed([model_p, model_c1, model_c2, model_c3],
                                                n_parents, traits, delta)

        # Check the networks layer-wise
        for layer_i in traits["layer_dropout"]:
            for network in new_generation[n_parents:]:
                self.assertEquals(model_p.layers[layer_i - 1].units, network.layers[layer_i - 1].units)
                self._assertAlmostEqualsMultiple(network.layers[layer_i].rate, new_generation[:n_parents], delta)
コード例 #2
0
    def test_breed1(self):
        delta = 0.0
        n_parents = 1
        traits = dict(layer_dropout={1})

        model_p = KerasPackageWrapper.make_flat_sequential_model()
        model_p.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_p.add(keras.layers.Dropout(0.1))

        model_c1 = KerasPackageWrapper.make_flat_sequential_model()
        model_c1.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_c1.add(keras.layers.Dropout(0.3))

        model_c2 = KerasPackageWrapper.make_flat_sequential_model()
        model_c2.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model_c2.add(keras.layers.Dropout(0.7))

        new_generation = GeneticOptimizer.breed([model_p, model_c1, model_c2], n_parents,
                                                traits, delta)

        # Check the networks layer-wise
        for layer_i in traits["layer_dropout"]:
            self.assertEqual(model_p.layers[layer_i - 1].units, new_generation[1].layers[layer_i - 1].units)
            self.assertEqual(model_p.layers[layer_i].rate, new_generation[1].layers[layer_i].rate)

            self.assertEqual(model_p.layers[layer_i - 1].units, new_generation[2].layers[layer_i - 1].units)
            self.assertEqual(model_p.layers[layer_i].rate, new_generation[2].layers[layer_i].rate)
コード例 #3
0
    def test_inherit_to_child4(self):
        # TODO keras assumed!
        delta = 0.1

        model1 = KerasPackageWrapper.make_flat_sequential_model()
        model1.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model1.add(keras.layers.Dropout(0.2))

        model1.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])

        model2 = KerasPackageWrapper.make_flat_sequential_model()
        model2.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model2.add(keras.layers.Dropout(0.7))

        model2.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])

        parents = [model1, model2]
        child = GeneticOptimizer.inherit_to_child(parents, dict(layer_dropout={1}), delta)
        # Assert that the structure stays the same as that of parents but the rate changes
        self.assertEqual(child.layers[0].units, model1.layers[0].units)
        self.assertEqual(child.layers[0].units, model2.layers[0].units)

        self._assertAlmostEqualsMultiple(child.layers[1].rate, TestGeneticOptimizer._extract_rates(parents, 1), delta)
コード例 #4
0
    def test_train_models1(self):
        # TODO keras assumed!
        model1 = KerasPackageWrapper.make_flat_sequential_model()
        model1.add(keras.layers.Dense(300, activation="relu", input_dim=784))
        model1.add(keras.layers.Dropout(0.2))
        model1.add(keras.layers.Dense(10, activation="softmax"))
        model1.add(keras.layers.Dropout(0.2))

        model1.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])

        instance = GeneticOptimizer(model1, (get_clean_mnist_with_cold_labels()[:2]),
                                    (get_clean_mnist_with_cold_labels()[2:]), n_categories=10,
                                    traits=dict(layer_dropout={1, 3}))

        print(GeneticOptimizer.train_models(instance, [model1]))
コード例 #5
0
    def test_inherit_to_child1(self):
        # TODO keras assumed!
        delta = 0.0

        model = KerasPackageWrapper.make_flat_sequential_model()
        model.add(keras.layers.Dense(10, activation="relu", input_dim=10))
        model.add(keras.layers.Dropout(0.5))

        model.compile(optimizer='rmsprop',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        child = GeneticOptimizer.inherit_to_child([model], dict(layer_dropout={1}), delta)
        self.assertEqual(model.layers[1].rate, child.layers[1].rate)
        self.assertEqual(model.layers[0].units, child.layers[0].units)