def test_initialize_with_search_space_list_04(): """Test initialize Tuun.""" config = {'acqfunction_config': {'name': 'default', 'acq_str': 'ucb'}} tu = Tuun(config) search_space_list = [('real', [0.0, 2.0]), ('real', [-1.0, 3.0])] tu.set_config_from_list(search_space_list) assert tu.config.domain_config.get('min_max') == [[0.0, 2.0], [-1.0, 3.0]]
def test_suggest_to_minimize(): """Test Tuun suggest_to_minimize on a dataset.""" config = {'acqfunction_config': {'name': 'default', 'acq_str': 'ucb'}} tu = Tuun(config) search_space_list = ('real', [0.0, 2.0]) tu.set_config_from_list(search_space_list) data = { 'x': [[0.25], [0.5], [0.75], [1.0], [1.25], [1.5], [1.75]], 'y': [8.0, 7.0, 6.0, 1.0, 4.0, 5.0, 6.0], } suggestion = tu.suggest_to_minimize(data) assert 0.75 < suggestion[0] < 1.25
from tuun.main import Tuun # instantiate Tuun config = { 'seed': 12, 'acqfunction_config': { 'name': 'default', 'acq_str': 'ts' }, } tu = Tuun(config) # set search space search_space = [('list', ['a', 'b', 'c', 'd', 'e'])] * 2 tu.set_config_from_list(search_space) # define function to optimize def f(x): str_map = {'a': 4, 'b': 5, 'c': 0, 'd': 0.7, 'e': -0.7} f_s = lambda x: x**4 - x**2 + 0.1 * x return f_s(str_map[x[0]]) + f_s(str_map[x[1]]) # minimize function over search space result = tu.minimize_function(f, 20)
class TuunTuner(Tuner): """ Tuun as a custom Tuner for NNI. """ def __init__(self, tuun_config, optimize_mode="maximize", initial_data=None): """ Parameters ---------- tuun_config : dict Config to specify Tuun options. initial_data : dict Dictionary with keys x (list) and y (1D numpy ndarray). """ self._set_tuun(tuun_config) self._set_data(initial_data) assert isinstance(tuun_config, dict) assert optimize_mode in ['minimize', 'maximize'] self._optimize_mode = OptimizeMode(optimize_mode) def _set_tuun(self, tuun_config): """Configure and instantiate self.tuun.""" self.tuun = Tuun(tuun_config) def _set_data(self, initial_data): """Set self.data.""" if initial_data is None: self.data = Namespace(x=[], y=[]) else: initial_data = copy.deepcopy(initial_data) if isinstance(initial_data, dict): initial_data = Namespace(**initial_data) self.data = initial_data elif isinstance(initial_data, Namespace): self.data = initial_data else: raise TypeError( 'initial_data must be either a dict, Namespace, or None') def update_search_space(self, search_space): """ Update search space. Input search_space contains information that the user pre-defines. The search space is set before generating first hyper-parameters. Parameters ---------- search_space : dict Information to define a search space. """ self.parameter_keys = list(search_space.keys()) dom_config_list = json2space(search_space) self.tuun.set_config_from_list(dom_config_list) def generate_parameters(self, parameter_id, **kwargs): """ Return a dict of trial (hyper-)parameters, as a serializable object. Parameters ---------- parameter_id : int ID number for given (hyper-)parameters. Returns ------- dict A set of (hyper-)parameters suggested by Tuun. """ if self._optimize_mode == OptimizeMode.Minimize: suggestion = self.tuun.suggest_to_minimize(self.data) else: # self._optimize_mode is guaranteed as 'min' or 'max'. suggestion = self.tuun.suggest_to_maximize(self.data) parsed_dict = self._parse_suggestion_into_dict(suggestion) return parsed_dict def receive_trial_result(self, parameter_id, parameters, value, **kwargs): """ Record the result from a trial. Parameters ---------- parameter_id : int ID number for given (hyper-)parameters. parameters : dict A set of (hyper-)parameters, same format returned by generate_parameters(). value : dict_or_float Final evaluation/objective metric of the trial. If value is dict, it should have "default" key. """ # Define y if isinstance(value, dict): y = float(value['default']) elif isinstance(value, numbers.Number): y = float(value) else: raise TypeError( 'value must be a Number or dict with "default" key') # Define x x = list(parameters.values()) # Update self.data self.data.x.append(x) self.data.y.append(y) def _parse_suggestion_into_dict(self, suggestion): """Parse suggestion from Tuun into dict for NNI.""" # Keep things simple for now parsed_dict = dict(zip(self.parameter_keys, suggestion)) return parsed_dict