def _ga_maximise(self, obj, num_evals, mutation_op, init_pool, init_pool_vals=None, expects_inputs_to_be_iterable=True): """ Maximise with genetic algorithms. if expects_inputs_as_list is True it means the function expects the inputs to be iterable by default. """ # Prep necessary variables obj, options, reporter, mutation_op = self._get_ga_optimiser_args( obj, num_evals, mutation_op, init_pool, init_pool_vals, expects_inputs_to_be_iterable) worker_manager = SyntheticWorkerManager(1, time_distro='const') func_caller = FunctionCaller(obj, self) opt_val, opt_pt, _ = ga_optimise_from_args(func_caller, worker_manager, num_evals, 'asy', mutation_op, options=options, reporter=reporter) return opt_val, opt_pt
def main(): """ Main function. """ # Obtain the search space nn_domain = get_nn_domain_from_constraints('cnn', MAX_NUM_LAYERS, MIN_NUM_LAYERS, MAX_MASS, MIN_MASS, MAX_IN_DEGREE, MAX_OUT_DEGREE, MAX_NUM_EDGES, MAX_NUM_UNITS_PER_LAYER, MIN_NUM_UNITS_PER_LAYER) # Obtain a worker manager: A worker manager (defined in opt/worker_manager.py) is used # to manage (possibly) multiple workers. For a synthetic experiment, we will use a # synthetic worker manager with 1 worker. worker_manager = SyntheticWorkerManager(1) # Obtain a function caller: A function_caller is used to evaluate a function defined on # neural network architectures. Here, we have obtained a function_caller from a # synthetic function, but for real experiments, you might have to write your own caller. # See the MLP/CNN demos for an example. func_caller = FunctionCaller(cnn_syn_func1, nn_domain) # Finally, specify the budget. In this case, it will be just the number of evaluations. budget = 20 # Run nasbot opt_val, opt_nn, _ = nasbot.nasbot(func_caller, worker_manager, budget) # Print the optimal value and visualise the best network. print '\nOptimum value found: ', opt_val print 'Optimal network visualised in syn_opt_network.eps.' visualise_nn(opt_nn, 'syn_opt_network')
def get_opt_problem(nn_type): """ Gets parameters for the optimisation problem. """ if nn_type == 'cnn': constraint_checker = CNNConstraintChecker(50, 1e8, 5, 5, 200, 1024, 8) init_points = get_initial_cnn_pool() func_caller = FunctionCaller(cnn_syn_func1, NNDomain(None, None)) elif nn_type.startswith('mlp'): constraint_checker = MLPConstraintChecker(50, 1e8, 5, 5, 200, 1024, 8) init_points = get_initial_mlp_pool(CLASS_OR_REG) func_caller = FunctionCaller(mlp_syn_func1, NNDomain(None, None)) else: raise ValueError('Unknown nn_type: %s.' % (nn_type)) # Common stuff mutation_op = get_nn_modifier_from_args(constraint_checker, [0.5, 0.25, 0.125, 0.075, 0.05]) init_vals = [func_caller.eval_single(nn)[0] for nn in init_points] return constraint_checker, func_caller, mutation_op, init_points, init_vals
def test_rand_optimisation_synchronous(self): """ Tests optimisation of a single point. """ self.report('Testing NNRandomBandit with 4 synchronous workers.') worker_manager = SyntheticWorkerManager(4, time_distro='halfnormal') func_caller = FunctionCaller(cnn_syn_func1, self.cnn_domain) options, options_clone, reporter, _, _ = self._get_optimiser_args('cnn') opt_val, opt_pt, history = nasbot.nnrandbandit_from_func_caller( func_caller, worker_manager, 5, 'syn', options=options, reporter=reporter) self._test_optimiser_results(opt_val, opt_pt, history, options, options_clone) self.report('')
def test_instantiation(self): """ Test Creation of NNGPBandit object. """ self.report('Testing Random Optimiser instantiation.') func_caller = FunctionCaller(cnn_syn_func1, self.cnn_domain) worker_manager = SyntheticWorkerManager(1, time_distro='const') optimiser = nasbot.NNRandomBandit(func_caller, worker_manager, reporter='silent') for attr in dir(optimiser): if not attr.startswith('_'): self.report('optimiser.%s = %s'%(attr, str(getattr(optimiser, attr))), 'test_result')
def test_instantiation(self): """ Test creation of object. """ self.report('Testing GA Optimiser instantiation.') func_caller = FunctionCaller(cnn_syn_func1, self.nn_domain) worker_manager = SyntheticWorkerManager(1, time_distro='const') optimiser = ga_optimiser.GAOptimiser(func_caller, worker_manager, self.cnn_mutation_op, reporter='silent') self.report('Instantiated GAOptimiser object.') for attr in dir(optimiser): if not attr.startswith('_'): self.report( 'optimiser.%s = %s' % (attr, str(getattr(optimiser, attr))), 'test_result')
def test_nasbot_optimisation_asynchronous(self): """ Tests optimisation of a single point. """ self.report('Testing NASBOT with 4 asynchronous workers.') worker_manager = SyntheticWorkerManager(4, time_distro='halfnormal') func_caller = FunctionCaller(mlp_syn_func1, self.mlp_domain) tp_comp = get_tp_comp('mlp-reg') options, options_clone, reporter, _, _ = self._get_optimiser_args( 'mlp-reg') opt_val, opt_pt, history = nasbot.nasbot(func_caller, worker_manager, 5, tp_comp, options=options, reporter=reporter) self._test_optimiser_results(opt_val, opt_pt, history, options, options_clone) self.report('')
def test_nasbot_optimisation_single(self): """ Tests optimisation with a single worker. """ self.report('Testing NASBOT with a single worker.') worker_manager = SyntheticWorkerManager(1, time_distro='const') func_caller = FunctionCaller(cnn_syn_func1, self.cnn_domain) tp_comp = get_tp_comp('cnn') options, options_clone, reporter, _, _ = self._get_optimiser_args( 'cnn') opt_val, opt_pt, history = nasbot.nasbot(func_caller, worker_manager, 10, tp_comp, options=options, reporter=reporter) self._test_optimiser_results(opt_val, opt_pt, history, options, options_clone) self.report('')
def test_optimisation_asynchronous(self): """ Test optimisation. """ self.report('Testing GA Optimiser with four workers asynchronously.') worker_manager = SyntheticWorkerManager(4, time_distro='halfnormal') func_caller = FunctionCaller(mlp_syn_func1, self.nn_domain) options, options_clone, reporter, _, mutation_op = self._get_optimiser_args( 'mlp') opt_val, opt_pt, history = ga_optimiser.ga_optimise_from_args( func_caller, worker_manager, 20, 'asy', mutation_op, options=options, reporter=reporter) self._test_optimiser_results(opt_val, opt_pt, history, options, options_clone) self.report('')
def test_ga_optimisation_single(self): """ Test optimisation. """ self.report('Testing GA Optimiser with just one worker.') worker_manager = SyntheticWorkerManager(1, time_distro='const') func_caller = FunctionCaller(cnn_syn_func1, self.nn_domain) options, options_clone, reporter, _, mutation_op = self._get_optimiser_args( 'cnn') opt_val, opt_pt, history = ga_optimiser.ga_optimise_from_args( func_caller, worker_manager, 20, 'asy', mutation_op, options=options, reporter=reporter) self._test_optimiser_results(opt_val, opt_pt, history, options, options_clone) self.report('')