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)
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)