def test_insert_conv(self): cnn1 = cga.CNN.random(input_shape=self.input_shape, num_classes=10, search_space=self.search_space_cnn_force_pool) cnn1.history = self.history1 # force a new conv layer conv_search_space = cga.ConvolutionalLayer.SearchSpace() conv_search_space.kernel_search_space = cga.Kernel.SearchSpace( depths=[64], resolutions=[(3, 3)], strides=[(1, 1)], padding_types=('V', )) cnn_search_space = cga.CNN.SearchSpace() cnn_search_space.mutate = cga.StochasticFunction({ lambda cnn: cnn.insert_layer(conv_search_space, cga.ConvolutionalLayer): 1 }) cnn_search_space.dynamic_child_search_spaces[ cga.ConvolutionalLayer] = conv_search_space cnn2 = cnn1.mutate(cnn_search_space) self.assertNotEqual(cnn1, cnn2) self.assertNotEqual(cnn1.topology, cnn2.topology) self.assertEqual(len(cnn2._layers), 2) conv_exists = isinstance(cnn2._layers[0], cga.ConvolutionalLayer) or isinstance( cnn2._layers[1], cga.ConvolutionalLayer) self.assertTrue(conv_exists) self.assertEqual(cnn2.history.parent1, cnn1) self.assertEqual(cnn2.history.parent1.history, self.history1)
def test_mutate_conv(self): cnn1 = cga.CNN.random(input_shape=self.input_shape, num_classes=10, search_space=self.search_space_cnn_force_conv) cnn1.history = self.history1 # force the conv to mutate search_space = cga.CNN.SearchSpace() search_space.dynamic_child_search_spaces[ cga. ConvolutionalLayer].kernel_search_space = cga.Kernel.SearchSpace( depths=[64], resolutions=[(3, 3)], strides=[(1, 1)], padding_types=('V', )) search_space.mutate = cga.StochasticFunction( {lambda cnn: cnn.change_layer(search_space): 1}) cnn2 = cnn1.mutate(search_space) self.assertNotEqual(cnn1, cnn2) self.assertNotEqual(cnn1.topology, cnn2.topology) self.assertEqual(len(cnn2._layers), len(cnn2._layers)) self.assertNotEqual(cnn1._layers[0]._kernel.depth, cnn2._layers[0]._kernel.depth) self.assertEqual(cnn2._layers[0]._kernel.depth, 64) self.assertEqual(cnn1.num_classes, cnn2.num_classes) self.assertEqual(cnn2.history.parent1, cnn1) self.assertEqual(cnn2.history.parent1.history, self.history1)
def test_mutate_add_layer(self): layer = cga.SkipLayer([1, 32, 32], []) # force the add conv mutation search_space = cga.SkipLayer.SearchSpace() search_space.mutate = cga.StochasticFunction( {lambda layer: layer.add_conv(self.conv_search_space): 1}) new_layer = layer.mutate(search_space) self.assertNotEqual(new_layer, layer) self.assertNotEqual(len(layer._convs), len(new_layer._convs)) self.assertArrayEqual(new_layer.output_shape, [888, 32, 32])
def test_mutate_remove_layer(self): layer = cga.SkipLayer.from_string( 'S{C[064(3x3 1x1 S)];C[032(3x3 1x1 S)];C[128(3x3 1x1 S)]}', input_shape=[1, 32, 32]) # force the remove conv mutation search_space = cga.SkipLayer.SearchSpace() search_space.mutate = cga.StochasticFunction( {lambda layer: layer.remove_conv(): 1}) new_layer = layer.mutate(search_space) self.assertNotEqual(new_layer, layer) self.assertEqual(len(new_layer._convs), 2)
def test_mutate_change_layer(self): layer = cga.SkipLayer.from_string('S{C[064(3x3 1x1 S)]}', input_shape=[1, 32, 32]) # force the change random conv mutation search_space = cga.SkipLayer.SearchSpace() search_space.mutate = cga.StochasticFunction({ lambda layer: layer.mutate_random_conv(self.conv_search_space): 1 }) new_layer = layer.mutate(search_space) self.assertNotEqual(new_layer, layer) self.assertEqual(len(layer._convs), len(new_layer._convs)) self.assertNotEqual(layer._convs[0], new_layer._convs[0]) self.assertArrayEqual(new_layer.output_shape, [888, 32, 32])
def test_mutate_pool(self): cnn1 = cga.CNN.random(input_shape=self.input_shape, num_classes=10, search_space=self.search_space_cnn_force_pool) cnn1.history = self.history1 # force the pool to mutate search_space = cga.CNN.SearchSpace() search_space.mutate = cga.StochasticFunction( {lambda cnn: cnn.change_layer(search_space): 1}) cnn2 = cnn1.mutate(search_space) self.assertNotEqual(cnn1, cnn2) self.assertNotEqual(cnn1.topology, cnn2.topology) self.assertEqual(len(cnn2._layers), len(cnn2._layers)) self.assertNotEqual(cnn1._layers[0]._pooling_type, cnn2._layers[0]._pooling_type) self.assertEqual(cnn1.num_classes, cnn2.num_classes) self.assertEqual(cnn2.history.parent1, cnn1) self.assertEqual(cnn2.history.parent1.history, self.history1)