def _mix_row(layout, row_idx, parent_rows, parameters): row = Row() blocks = random_list_element([len(p.blocks) for p in parent_rows]) for block_idx in range(blocks): parent_blocks = [p.blocks[block_idx] for p in parent_rows if block_idx < len(p.blocks)] parent = random_list_element(parent_blocks) new_block = deepcopy(parent) row.blocks.append(new_block) return row
def _instantiate_layout_block(layout, row_idx, experiment_parameters, init_layer_parameters=False): block_layout = layout.block if _is_multiple_block_layouts_defined(layout): block_layout = random_list_element(layout.block) block = Block() _setup_block_input(layout, row_idx, block, experiment_parameters) block.layers = _create_template_layers( block_layout, experiment_parameters, init_layer_parameters=init_layer_parameters) return block
def _mutate_layout_layers(layout, parameters): row = random_list_element(layout.rows) block = random_list_element(row.blocks) current_layers = len(block.get_layers()) new_layers = mutate_param( parameters.get_layout_parameter('layers'), current_layers) if new_layers > current_layers: for _ in range(current_layers, new_layers): layers = get_allowed_new_block_layers(block.layers, parameters) block.layers += _create_template_layers( [random_list_element(layers)], parameters, init_layer_parameters=True) else: while len(block.layers) > new_layers: idx = rand.randint(0, len(block.layers) - 1) block.layers = [ l for i, l in enumerate(block.layers) if i != idx]
def _mix_layouts(parent_layouts, parameters, p_mutate_param=0.05): layout = Layout( input_size=parent_layouts[0].input_size, output_size=parent_layouts[0].output_size, output_activation=parent_layouts[0].output_activation, block=parent_layouts[0].block, block_input=parent_layouts[0].block_input) if parameters.is_layout_search(): rows = random_list_element([len(p.rows) for p in parent_layouts]) for row_idx in range(rows): parent_rows = [p.rows[row_idx] for p in parent_layouts if row_idx < len(p.rows)] layout.rows.append(_mix_row(layout, row_idx, parent_rows, parameters)) _setup_block_inputs(layout, parameters) if parameters.is_parameters_search(): _mutate_layer_parameters( layout, parameters, p_mutate_param=p_mutate_param) else: layout.rows = random_list_element([ parent_layouts[0].rows, parent_layouts[1].rows]) return layout
def _mix_trainings(parents, parameters, p_mutate_param=0.05): training = Training( objective=parents[0].objective, optimizer=None, metric=parents[0].metric, stopping=parents[0].stopping, batch_size=parents[0].batch_size) parent_optimizers = [p.optimizer for p in parents] if parameters.is_optimizer_search(): training.optimizer = _mix_optimizers(parent_optimizers) _mutate_optimizer(training.optimizer, parameters, p_mutate_param) else: training.optimizer = random_list_element(parent_optimizers) return training
def _mutate_layout_blocks(layout, parameters): row = random_list_element(layout.rows) row_idx = layout.rows.index(row) current_blocks = len(row.get_blocks()) new_blocks = mutate_param( parameters.get_layout_parameter('blocks'), current_blocks) if new_blocks > current_blocks: for _ in range(current_blocks, new_blocks): row.blocks.append( _random_layout_block( layout, row_idx, parameters, init_layer_parameters=True)) else: while len(row.blocks) > new_blocks: idx = rand.randint(0, len(row.blocks) - 1) row.blocks = [ b for i, b in enumerate(row.blocks) if i != idx]
def _random_layout_block(layout, row_idx, experiment_parameters, init_layer_parameters=False): if layout.block: return _instantiate_layout_block( layout, row_idx, experiment_parameters, init_layer_parameters=init_layer_parameters) layers = random_initial_param_value(experiment_parameters.get_layout_parameter('layers')) template = [] for _ in range(layers): allowed_layers = get_allowed_new_block_layers(template, experiment_parameters) if len(allowed_layers) == 0: break new_layer = random_list_element(allowed_layers) template.append(new_layer) block = Block() _setup_block_input(layout, row_idx, block, experiment_parameters) block.layers += _create_template_layers( template, experiment_parameters, init_layer_parameters) return block
def _mix_optimizers(parents): return deepcopy(random_list_element(parents))