diff --git a/README.md b/README.md index 4fa03c7..161106c 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Some examples of BBopt in action: - [`choice`](#choice) - [`randbool`](#randbool) - [`sample`](#sample) - - [`shuffled`](#shuffled) + - [`shuffle`](#shuffle) - [`random`](#random) - [`uniform`](#uniform) - [`loguniform`](#loguniform) @@ -426,15 +426,17 @@ BlackBoxOptimizer.**sample**(_name_, _population_, _k_, **_kwargs_) Create a new parameter modeled by [`random.sample(population, k)`](https://docs.python.org/3/library/random.html#random.sample), which chooses _k_ elements from _population_. +By default, the ordering of elements in the result is random. If random ordering is not important and you're happy to have the same ordering as in _population_, `BlackBoxOptimizer.unshuffled_sample` is recommended instead. + _Backends which support **sample**: `scikit-optimize`, `hyperopt`, `bayes-skopt`, `pySOT`, `random`._ -#### `shuffled` +#### `shuffle` -BlackBoxOptimizer.**shuffled**(_name_, _population_, **_kwargs_) +BlackBoxOptimizer.**shuffle**(_name_, _population_, **_kwargs_) -Create a new parameter modeled by [`random.shuffle(population)`](https://docs.python.org/3/library/random.html#random.shuffle) except that it returns the shuffled list instead of shuffling it in place. An in-place version as `BlackBoxOptimizer.shuffle` is also supported. +Create a new parameter modeled by [`random.shuffle(population)`](https://docs.python.org/3/library/random.html#random.shuffle). A version that returns the shuffled list instead of shuffling it in place is also supported as `BlackBoxOptimizer.shuffled`. -_Backends which support **shuffled**: `scikit-optimize`, `hyperopt`, `bayes-skopt`, `pySOT`, `random`._ +_Backends which support **shuffle**: `scikit-optimize`, `hyperopt`, `bayes-skopt`, `pySOT`, `random`._ #### `random` diff --git a/bbopt-source/backends/openai.coco b/bbopt-source/backends/openai.coco index 0c1d4bc..f9adb7c 100644 --- a/bbopt-source/backends/openai.coco +++ b/bbopt-source/backends/openai.coco @@ -3,21 +3,20 @@ The OpenAI backend. Uses large language models for black box optimization. """ import os -import random from ast import literal_eval import openai from bbopt import constants -from bbopt.util import printerr, stdev +from bbopt.util import printerr, stdev, mean from bbopt.params import param_processor -from bbopt.backends.util import StandardBackend +from bbopt.backends.util import StandardBackend, sorted_params # Utilities: def get_prompt(params, data_points, losses, hoped_for_loss) = - """Get the OpenAI API prompt to use.""" + """Get the base OpenAI API prompt.""" '''# black box function to be minimized def f({func_params}) -> float: """ @@ -49,7 +48,7 @@ def f({func_params}) -> float: " {name}: in {func}({args})".format( name=name, func="range" if func == "randrange" else func, - args=", ".join(args |> map$(repr)), + args=", ".join((args[:2] if func == "randrange" and args[-1] == 1 else args) |> map$(repr)), ) for name, (func, args, _) in params.items() ), @@ -58,7 +57,7 @@ def f({func_params}) -> float: "{name}: {func}({args})".format( name=name, func="range" if func == "randrange" else func, - args=", ".join(args |> map$(repr)), + args=", ".join((args[:2] if func == "randrange" and args[-1] == 1 else args) |> map$(repr)), ) for name, (func, args, _) in params.items() ), @@ -69,7 +68,7 @@ def f({func_params}) -> float: ) for point, loss in zip(data_points, losses) ), - hoped_for_loss=hoped_for_loss, + hoped_for_loss=int(hoped_for_loss) if int(hoped_for_loss) == hoped_for_loss else hoped_for_loss, ) @@ -92,9 +91,9 @@ def to_python(completion, params): return completion -def get_loss_eps(min_loss): - """Get a reasonably-sized expected loss improvement.""" - a, b = float(abs(min_loss)).as_integer_ratio() +def get_loss_eps(typical_loss): + """Get a reasonably-sized hoped for loss improvement.""" + a, b = float(abs(typical_loss)).as_integer_ratio() little_a = int("1" * len(str(a))) return little_a / b @@ -113,8 +112,8 @@ class OpenAIBackend(StandardBackend): max_prompt_len = float("inf") - def setup_backend(self, params, engine=None, temperature=None, max_retries=None, api_key=None, debug=True): - self.params = params + def setup_backend(self, params, engine=None, temperature=None, max_retries=None, api_key=None, debug=False): + self.params = sorted_params(params) self.engine = engine ?? constants.openai_default_engine self.temp = temperature ?? constants.openai_default_temp @@ -127,21 +126,29 @@ class OpenAIBackend(StandardBackend): self.cached_values = () def tell_data(self, new_data, new_losses): - self.data_points += new_data - self.losses += new_losses - - def get_prompt(self) = ( - get_prompt( - self.params, - self.data_points, - self.losses, - hoped_for_loss=min_loss - random.uniform(0, stdev(self.losses) + get_loss_eps(min_loss)), + for point, loss in zip(new_data, new_losses): + # avoid (point, loss) duplicates since they cause GPT to repeat itself + try: + existing_index = self.data_points.index(point) + except ValueError: + existing_index = None + if existing_index is None or self.losses[existing_index] != loss: + self.data_points.append(point) + self.losses.append(loss) + + def get_prompt(self) = + """Get the OpenAI API prompt to use.""" + ( + get_prompt( + self.params, + self.data_points, + self.losses, + hoped_for_loss=min(self.losses) - stdev(self.losses) - get_loss_eps(mean(self.losses)), + ) + + ", ".join(self.cached_values |> map$(repr)) + # only "," not ", " since the prompt shouldn't end in a space + + ("," if self.cached_values else "") ) - + ", ".join(self.cached_values |> map$(repr)) - # only "," not ", " since the prompt shouldn't end in a space - + ("," if self.cached_values else "") - ) where: - min_loss = min(self.losses) def get_completion_len(self) = """Get the maximum number of characters in a completion.""" @@ -218,7 +225,7 @@ class OpenAIBackend(StandardBackend): if values in self.data_points: if self.debug: print(f"ERROR: got duplicate point: {legal_values!r}") - return self.retry_get_values(temp=self.temp + (constants.openai_max_temp - self.temp) / 2) + return self.retry_get_values(temp=self.temp + (constants.openai_max_temp - self.temp) / 2, cached_values=()) return values def retry_get_values(self, temp=None, cached_values=None): @@ -236,7 +243,7 @@ class OpenAIBackend(StandardBackend): old_temp, self.temp = self.temp, temp if cached_values is not None: if self.debug: - print(f"CACHING values: {cached_values[:len(self.cached_values)]} + {cached_values[len(self.cached_values):]}") + print(f"CACHING values: {self.cached_values} -> {cached_values}") self.cached_values = cached_values try: return self.get_next_values() @@ -252,3 +259,4 @@ class OpenAIBackend(StandardBackend): OpenAIBackend.register() OpenAIBackend.register_alg("openai") +OpenAIBackend.register_alg("openai_debug", debug=True) diff --git a/bbopt-source/backends/util.coco b/bbopt-source/backends/util.coco index bf319af..3aacc89 100644 --- a/bbopt-source/backends/util.coco +++ b/bbopt-source/backends/util.coco @@ -4,6 +4,7 @@ Utilities for use in BBopt backends. import random +from collections import OrderedDict from collections.abc import Iterable from bbopt import constants @@ -24,6 +25,11 @@ from bbopt.registry import ( # Utilities: +def sorted_params(params) = + """Get an OrderedDict of params in sorted order.""" + params |> sorted_items |> OrderedDict + + @convert_match_errors match def _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=None, _on_new_backend=None, **options): """Create a backend object with the given data (backend can be backend name or class).""" diff --git a/bbopt-source/benchmarking.coco b/bbopt-source/benchmarking.coco index df88099..c122bae 100644 --- a/bbopt-source/benchmarking.coco +++ b/bbopt-source/benchmarking.coco @@ -42,7 +42,7 @@ OPT_FUNCS.append(numpy_func) def sample_func(bb): - xs = bb.sample("xs", range(10), 5, guess=[3,4,5,6,7]) + xs = bb.unshuffled_sample("xs", range(10), 5, guess=[3,4,5,6,7]) y = bb.choice("y", [1, 10, 100], guess=10) loss = abs(sum(xs) - y) bb.minimize(loss) @@ -115,5 +115,5 @@ if __name__ == "__main__": "tpe_or_gp", "tree_structured_parzen_estimator", "safe_gaussian_process", - ("openai", "safe_gaussian_process"), + ("openai_debug", "safe_gaussian_process"), )) diff --git a/bbopt-source/constants.coco b/bbopt-source/constants.coco index 8280e79..ed502f4 100644 --- a/bbopt-source/constants.coco +++ b/bbopt-source/constants.coco @@ -5,7 +5,7 @@ Constants for use across all of BBopt. # Installation constants: name = "bbopt" -version = "1.4.1" +version = "1.4.2" description = "The easiest hyperparameter optimization you'll ever do." long_description = """ See BBopt's GitHub_ for more information. diff --git a/bbopt-source/optimizer.coco b/bbopt-source/optimizer.coco index 26cbdbb..0d1e60b 100644 --- a/bbopt-source/optimizer.coco +++ b/bbopt-source/optimizer.coco @@ -541,7 +541,8 @@ class BlackBoxOptimizer: return bool(self.choice(name, [False, True], **kwargs)) def sample(self, name, population, k, **kwargs): - """Create a new parameter with the given name modeled by random.sample(population, k).""" + """Create a new parameter with the given name modeled by random.sample(population, k). + Ordering of elements in the result is random.""" if not isinstance(name, Str): raise TypeError(f"name must be string, not {name}") sampling_population = [x for x in population] @@ -559,13 +560,40 @@ class BlackBoxOptimizer: sample.append(sampling_population.pop(ind)) return sample + def unshuffled_sample(self, name, population, k, **kwargs): + """Create a new parameter with the given name modeled by random.sample(population, k). + Ordering of elements in the result is the same as in population.""" + if not isinstance(name, Str): + raise TypeError(f"name must be string, not {name}") + population = tuple(population) + sample = [] + for i, x in enumerate(population): + if len(sample) == k: + break + if len(population) - i == k - len(sample): + sample += population[i:] + break + proc_kwargs = kwargs |> param_processor.modify_kwargs$( + val -> 1 if x in val else 0 + ) + if "placeholder_when_missing" not in proc_kwargs: + proc_kwargs["placeholder_when_missing"] = 0 + if self.uniform( + f"{name}[{i}]", + 0, + 1, + **proc_kwargs, + ) >= 1 - (k - len(sample))/(len(population) - i): + sample.append(x) + return sample + def samples_with_replacement(self, name, population, **kwargs): """An infinite iterator of samples with replacement from population.""" if not isinstance(name, Str): raise TypeError(f"name must be string, not {name}") - sampling_population = tuple(population) + population = tuple(population) for i in count(): - yield self.choice(f"{name}[{i}]", sampling_population, **kwargs) + yield self.choice(f"{name}[{i}]", population, **kwargs) def shuffled(self, name, population, **kwargs): """Create a new parameter with the given name modeled by diff --git a/bbopt/backends/openai.py b/bbopt/backends/openai.py index d428b6e..133a2e0 100644 --- a/bbopt/backends/openai.py +++ b/bbopt/backends/openai.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x8ce35b4c +# __coconut_hash__ = 0xac1cd345 # Compiled with Coconut version 2.0.0-a_dev53 [How Not to Be Seen] @@ -40,22 +40,23 @@ import os #5 (line num in coconut source) -import random #6 (line num in coconut source) -from ast import literal_eval #7 (line num in coconut source) +from ast import literal_eval #6 (line num in coconut source) -import openai #9 (line num in coconut source) +import openai #8 (line num in coconut source) -from bbopt import constants #11 (line num in coconut source) -from bbopt.util import printerr #12 (line num in coconut source) -from bbopt.util import stdev #12 (line num in coconut source) -from bbopt.params import param_processor #13 (line num in coconut source) -from bbopt.backends.util import StandardBackend #14 (line num in coconut source) +from bbopt import constants #10 (line num in coconut source) +from bbopt.util import printerr #11 (line num in coconut source) +from bbopt.util import stdev #11 (line num in coconut source) +from bbopt.util import mean #11 (line num in coconut source) +from bbopt.params import param_processor #12 (line num in coconut source) +from bbopt.backends.util import StandardBackend #13 (line num in coconut source) +from bbopt.backends.util import sorted_params #13 (line num in coconut source) # Utilities: -def get_prompt(params, data_points, losses, hoped_for_loss): #19 (line num in coconut source) - """Get the OpenAI API prompt to use.""" #20 (line num in coconut source) +def get_prompt(params, data_points, losses, hoped_for_loss): #18 (line num in coconut source) + """Get the base OpenAI API prompt.""" #19 (line num in coconut source) return '''# black box function to be minimized def f({func_params}) -> float: """ @@ -70,186 +71,194 @@ def f({func_params}) -> float: # experimentally observed data # new experiments MUST stay within the bounds, SHOULD fully explore the bounds, and SHOULD converge to minimum # bounds: f({bounds}) -{values}{hoped_for_loss} == f('''.format(func_params=", ".join(("{name}: {type}".format(name=name, type=("int" if func == "randrange" else type(args[0][0]).__name__ if func == "choice" and all_equal(map(type, args[0])) else "typing.Any" if func == "choice" else "float")) for name, (func, args, _) in params.items())), docstring="\n".join((" {name}: in {func}({args})".format(name=name, func="range" if func == "randrange" else func, args=", ".join((map)(repr, args))) for name, (func, args, _) in params.items())), names=", ".join(params), bounds=", ".join(("{name}: {func}({args})".format(name=name, func="range" if func == "randrange" else func, args=", ".join((map)(repr, args))) for name, (func, args, _) in params.items())), values="".join(("{loss} == f({args})\n".format(args=", ".join((map)(repr, (map)(_coconut.functools.partial(_coconut.operator.getitem, point), params))), loss=loss) for point, loss in zip(data_points, losses))), hoped_for_loss=hoped_for_loss) #35 (line num in coconut source) +{values}{hoped_for_loss} == f('''.format(func_params=", ".join(("{name}: {type}".format(name=name, type=("int" if func == "randrange" else type(args[0][0]).__name__ if func == "choice" and all_equal(map(type, args[0])) else "typing.Any" if func == "choice" else "float")) for name, (func, args, _) in params.items())), docstring="\n".join((" {name}: in {func}({args})".format(name=name, func="range" if func == "randrange" else func, args=", ".join((map)(repr, (args[:2] if func == "randrange" and args[-1] == 1 else args)))) for name, (func, args, _) in params.items())), names=", ".join(params), bounds=", ".join(("{name}: {func}({args})".format(name=name, func="range" if func == "randrange" else func, args=", ".join((map)(repr, (args[:2] if func == "randrange" and args[-1] == 1 else args)))) for name, (func, args, _) in params.items())), values="".join(("{loss} == f({args})\n".format(args=", ".join((map)(repr, (map)(_coconut.functools.partial(_coconut.operator.getitem, point), params))), loss=loss) for point, loss in zip(data_points, losses))), hoped_for_loss=int(hoped_for_loss) if int(hoped_for_loss) == hoped_for_loss else hoped_for_loss) #34 (line num in coconut source) -def to_python(completion, params): #76 (line num in coconut source) - """Convert a completion to Python code as best as possible.""" #77 (line num in coconut source) - completion = completion.strip("(,") #78 (line num in coconut source) - for repl, to in _coconut.itertools.chain.from_iterable(_coconut_reiterable(_coconut_func() for _coconut_func in (lambda: (("\u2212", "-"), ("\u2018", "'"), ("\u2019", "'"), ("\u201c", '"'), ("\u201d", '"'), ("\u221e", 'float("inf")')), lambda: (("{_coconut_format_0}=".format(_coconut_format_0=(name)), "") for name in params), lambda: (("{_coconut_format_0}:".format(_coconut_format_0=(name)), "") for name in params)))): #79 (line num in coconut source) - completion = completion.replace(repl, to) #91 (line num in coconut source) - return completion #92 (line num in coconut source) +def to_python(completion, params): #75 (line num in coconut source) + """Convert a completion to Python code as best as possible.""" #76 (line num in coconut source) + completion = completion.strip("(,") #77 (line num in coconut source) + for repl, to in _coconut.itertools.chain.from_iterable(_coconut_reiterable(_coconut_func() for _coconut_func in (lambda: (("\u2212", "-"), ("\u2018", "'"), ("\u2019", "'"), ("\u201c", '"'), ("\u201d", '"'), ("\u221e", 'float("inf")')), lambda: (("{_coconut_format_0}=".format(_coconut_format_0=(name)), "") for name in params), lambda: (("{_coconut_format_0}:".format(_coconut_format_0=(name)), "") for name in params)))): #78 (line num in coconut source) + completion = completion.replace(repl, to) #90 (line num in coconut source) + return completion #91 (line num in coconut source) -def get_loss_eps(min_loss): #95 (line num in coconut source) - """Get a reasonably-sized expected loss improvement.""" #96 (line num in coconut source) - a, b = float(abs(min_loss)).as_integer_ratio() #97 (line num in coconut source) - little_a = int("1" * len(str(a))) #98 (line num in coconut source) - return little_a / b #99 (line num in coconut source) +def get_loss_eps(typical_loss): #94 (line num in coconut source) + """Get a reasonably-sized hoped for loss improvement.""" #95 (line num in coconut source) + a, b = float(abs(typical_loss)).as_integer_ratio() #96 (line num in coconut source) + little_a = int("1" * len(str(a))) #97 (line num in coconut source) + return little_a / b #98 (line num in coconut source) # Backend: -class OpenAIBackend(StandardBackend): #104 (line num in coconut source) - """OpenAI large language model BBopt backend.""" #105 (line num in coconut source) - backend_name = "openai" #106 (line num in coconut source) - implemented_funcs = ("randrange", "uniform", "normalvariate", "choice") #107 (line num in coconut source) +class OpenAIBackend(StandardBackend): #103 (line num in coconut source) + """OpenAI large language model BBopt backend.""" #104 (line num in coconut source) + backend_name = "openai" #105 (line num in coconut source) + implemented_funcs = ("randrange", "uniform", "normalvariate", "choice") #106 (line num in coconut source) - max_prompt_len = float("inf") #114 (line num in coconut source) + max_prompt_len = float("inf") #113 (line num in coconut source) - def setup_backend(self, params, engine=None, temperature=None, max_retries=None, api_key=None, debug=True): #116 (line num in coconut source) - self.params = params #117 (line num in coconut source) + def setup_backend(self, params, engine=None, temperature=None, max_retries=None, api_key=None, debug=False): #115 (line num in coconut source) + self.params = sorted_params(params) #116 (line num in coconut source) - self.engine = (constants.openai_default_engine if engine is None else engine) #119 (line num in coconut source) - self.temp = (constants.openai_default_temp if temperature is None else temperature) #120 (line num in coconut source) - self.max_retries = (lambda _coconut_x: constants.openai_default_max_retries if _coconut_x is None else _coconut_x)(max_retries) #121 (line num in coconut source) - openai.api_key = (lambda _coconut_x: os.getenv("OPENAI_API_KEY") if _coconut_x is None else _coconut_x)(api_key) #122 (line num in coconut source) - self.debug = debug #123 (line num in coconut source) + self.engine = (constants.openai_default_engine if engine is None else engine) #118 (line num in coconut source) + self.temp = (constants.openai_default_temp if temperature is None else temperature) #119 (line num in coconut source) + self.max_retries = (lambda _coconut_x: constants.openai_default_max_retries if _coconut_x is None else _coconut_x)(max_retries) #120 (line num in coconut source) + openai.api_key = (lambda _coconut_x: os.getenv("OPENAI_API_KEY") if _coconut_x is None else _coconut_x)(api_key) #121 (line num in coconut source) + self.debug = debug #122 (line num in coconut source) - self.data_points = [] #125 (line num in coconut source) - self.losses = [] #126 (line num in coconut source) - self.cached_values = () #127 (line num in coconut source) + self.data_points = [] #124 (line num in coconut source) + self.losses = [] #125 (line num in coconut source) + self.cached_values = () #126 (line num in coconut source) - def tell_data(self, new_data, new_losses): #129 (line num in coconut source) - self.data_points += new_data #130 (line num in coconut source) - self.losses += new_losses #131 (line num in coconut source) + def tell_data(self, new_data, new_losses): #128 (line num in coconut source) + for point, loss in zip(new_data, new_losses): #129 (line num in coconut source) +# avoid (point, loss) duplicates since they cause GPT to repeat itself + try: #131 (line num in coconut source) + existing_index = self.data_points.index(point) #132 (line num in coconut source) + except ValueError: #133 (line num in coconut source) + existing_index = None #134 (line num in coconut source) + if existing_index is None or self.losses[existing_index] != loss: #135 (line num in coconut source) + self.data_points.append(point) #136 (line num in coconut source) + self.losses.append(loss) #137 (line num in coconut source) - def get_prompt(self): #133 (line num in coconut source) - min_loss = min(self.losses) #144 (line num in coconut source) + def get_prompt(self): #139 (line num in coconut source) + """Get the OpenAI API prompt to use.""" #140 (line num in coconut source) + return (get_prompt(self.params, self.data_points, self.losses, hoped_for_loss=min(self.losses) - stdev(self.losses) - get_loss_eps(mean(self.losses))) + ", ".join((map)(repr, self.cached_values)) + ("," if self.cached_values else "")) #141 (line num in coconut source) - return (get_prompt(self.params, self.data_points, self.losses, hoped_for_loss=min_loss - random.uniform(0, stdev(self.losses) + get_loss_eps(min_loss))) + ", ".join((map)(repr, self.cached_values)) + ("," if self.cached_values else "")) #146 (line num in coconut source) - def get_completion_len(self): #146 (line num in coconut source) - """Get the maximum number of characters in a completion.""" #147 (line num in coconut source) - return max((len(", ".join((map)(repr, (map)(_coconut.functools.partial(_coconut.operator.getitem, point), self.params)))) for point in self.data_points)) + 1 #148 (line num in coconut source) + def get_completion_len(self): #153 (line num in coconut source) + """Get the maximum number of characters in a completion.""" #154 (line num in coconut source) + return max((len(", ".join((map)(repr, (map)(_coconut.functools.partial(_coconut.operator.getitem, point), self.params)))) for point in self.data_points)) + 1 #155 (line num in coconut source) - def get_next_values(self): #153 (line num in coconut source) + def get_next_values(self): #160 (line num in coconut source) # generate prompt - prompt = self.get_prompt() #155 (line num in coconut source) - while len(prompt) > self.max_prompt_len: #156 (line num in coconut source) - self.data_points.pop(0) #157 (line num in coconut source) - self.losses.pop(0) #158 (line num in coconut source) - prompt = self.get_prompt() #159 (line num in coconut source) - if self.debug: #160 (line num in coconut source) - print("\n== PROMPT ==\n" + prompt) #161 (line num in coconut source) + prompt = self.get_prompt() #162 (line num in coconut source) + while len(prompt) > self.max_prompt_len: #163 (line num in coconut source) + self.data_points.pop(0) #164 (line num in coconut source) + self.losses.pop(0) #165 (line num in coconut source) + prompt = self.get_prompt() #166 (line num in coconut source) + if self.debug: #167 (line num in coconut source) + print("\n== PROMPT ==\n" + prompt) #168 (line num in coconut source) # query api - try: #164 (line num in coconut source) - response = openai.Completion.create(engine=self.engine, prompt=prompt, temperature=self.temp, max_tokens=self.get_completion_len()) #165 (line num in coconut source) - except openai.error.InvalidRequestError as api_err: #171 (line num in coconut source) - if self.debug: #172 (line num in coconut source) - print("== END ==") #173 (line num in coconut source) - if not str(api_err).startswith(constants.openai_max_context_err_prefix): #174 (line num in coconut source) - raise #175 (line num in coconut source) - if self.debug: #176 (line num in coconut source) - print("ERROR: got max context length error with self.max_prompt_len={_coconut_format_0}".format(_coconut_format_0=(self.max_prompt_len))) #177 (line num in coconut source) - if self.max_prompt_len == float("inf"): #178 (line num in coconut source) - self.max_prompt_len = len(prompt.rsplit("\n", 1)[0]) #179 (line num in coconut source) - else: #180 (line num in coconut source) - self.max_prompt_len -= self.get_completion_len() #181 (line num in coconut source) - return self.retry_get_values() #182 (line num in coconut source) + try: #171 (line num in coconut source) + response = openai.Completion.create(engine=self.engine, prompt=prompt, temperature=self.temp, max_tokens=self.get_completion_len()) #172 (line num in coconut source) + except openai.error.InvalidRequestError as api_err: #178 (line num in coconut source) + if self.debug: #179 (line num in coconut source) + print("== END ==") #180 (line num in coconut source) + if not str(api_err).startswith(constants.openai_max_context_err_prefix): #181 (line num in coconut source) + raise #182 (line num in coconut source) + if self.debug: #183 (line num in coconut source) + print("ERROR: got max context length error with self.max_prompt_len={_coconut_format_0}".format(_coconut_format_0=(self.max_prompt_len))) #184 (line num in coconut source) + if self.max_prompt_len == float("inf"): #185 (line num in coconut source) + self.max_prompt_len = len(prompt.rsplit("\n", 1)[0]) #186 (line num in coconut source) + else: #187 (line num in coconut source) + self.max_prompt_len -= self.get_completion_len() #188 (line num in coconut source) + return self.retry_get_values() #189 (line num in coconut source) # parse response - try: #185 (line num in coconut source) - completion = response["choices"][0]["text"] #186 (line num in coconut source) - if self.debug: #187 (line num in coconut source) - print("== COMPLETION ==\n" + completion) #188 (line num in coconut source) - valstr = to_python(completion.split(")", 1)[0].strip(), self.params) #189 (line num in coconut source) - valvec = literal_eval("(" + valstr + ",)") #190 (line num in coconut source) - except BaseException as parse_err: #191 (line num in coconut source) - if self.debug: #192 (line num in coconut source) - print("== END ==") #193 (line num in coconut source) + try: #192 (line num in coconut source) + completion = response["choices"][0]["text"] #193 (line num in coconut source) if self.debug: #194 (line num in coconut source) - print("ERROR: {_coconut_format_0} for API response:\n{_coconut_format_1}".format(_coconut_format_0=(parse_err), _coconut_format_1=(response))) #195 (line num in coconut source) - return self.retry_get_values(temp=(self.temp + constants.openai_default_temp) / 2) #196 (line num in coconut source) - if self.debug: #197 (line num in coconut source) - print("== END ==") #198 (line num in coconut source) - @_coconut_mark_as_match #199 (line num in coconut source) - def _coconut_lambda_0(*_coconut_match_args, **_coconut_match_kwargs): #199 (line num in coconut source) - _coconut_match_check_0 = False #199 (line num in coconut source) - _coconut_match_set_name_val = _coconut_sentinel #199 (line num in coconut source) - _coconut_match_set_name_name = _coconut_sentinel #199 (line num in coconut source) - _coconut_match_set_name_func = _coconut_sentinel #199 (line num in coconut source) - _coconut_match_set_name_args = _coconut_sentinel #199 (line num in coconut source) - _coconut_match_set_name_kwargs = _coconut_sentinel #199 (line num in coconut source) - _coconut_FunctionMatchError = _coconut_get_function_match_error() #199 (line num in coconut source) - if _coconut.len(_coconut_match_args) == 1: #199 (line num in coconut source) - if (_coconut.isinstance(_coconut_match_args[0], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0]) == 2) and (_coconut.isinstance(_coconut_match_args[0][1], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0][1]) == 2) and (_coconut.isinstance(_coconut_match_args[0][1][1], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0][1][1]) == 3): #199 (line num in coconut source) - _coconut_match_set_name_val = _coconut_match_args[0][0] #199 (line num in coconut source) - _coconut_match_set_name_name = _coconut_match_args[0][1][0] #199 (line num in coconut source) - _coconut_match_set_name_func = _coconut_match_args[0][1][1][0] #199 (line num in coconut source) - _coconut_match_set_name_args = _coconut_match_args[0][1][1][1] #199 (line num in coconut source) - _coconut_match_set_name_kwargs = _coconut_match_args[0][1][1][2] #199 (line num in coconut source) - if not _coconut_match_kwargs: #199 (line num in coconut source) - _coconut_match_check_0 = True #199 (line num in coconut source) - if _coconut_match_check_0: #199 (line num in coconut source) - if _coconut_match_set_name_val is not _coconut_sentinel: #199 (line num in coconut source) - val = _coconut_match_set_name_val #199 (line num in coconut source) - if _coconut_match_set_name_name is not _coconut_sentinel: #199 (line num in coconut source) - name = _coconut_match_set_name_name #199 (line num in coconut source) - if _coconut_match_set_name_func is not _coconut_sentinel: #199 (line num in coconut source) - func = _coconut_match_set_name_func #199 (line num in coconut source) - if _coconut_match_set_name_args is not _coconut_sentinel: #199 (line num in coconut source) - args = _coconut_match_set_name_args #199 (line num in coconut source) - if _coconut_match_set_name_kwargs is not _coconut_sentinel: #199 (line num in coconut source) - kwargs = _coconut_match_set_name_kwargs #199 (line num in coconut source) - if not _coconut_match_check_0: #199 (line num in coconut source) - raise _coconut_FunctionMatchError('|> takewhile$(def ((val, (name, (func, args, kwargs)))) ->', _coconut_match_args) #199 (line num in coconut source) - return param_processor.in_support(name, val, func, *args, **kwargs) #199 (line num in coconut source) - legal_values = ((tuple)((map)(_coconut.operator.itemgetter((0)), (takewhile)(_coconut_lambda_0, (_coconut_partial(zip, {1: self.params.items()}, 2))((self.cached_values + valvec)[_coconut.slice(None, len(self.params))]))))) #199 (line num in coconut source) - if len(legal_values) < len(self.params): #208 (line num in coconut source) - if self.debug: #209 (line num in coconut source) - if len(valvec) < len(self.params) - len(self.cached_values): #210 (line num in coconut source) - print("ERROR: insufficient values (got {_coconut_format_0}; expected {_coconut_format_1})".format(_coconut_format_0=(len(valvec)), _coconut_format_1=(len(self.params) - len(self.cached_values)))) #211 (line num in coconut source) - else: #212 (line num in coconut source) - print("ERROR: got illegal values: {_coconut_format_0!r}".format(_coconut_format_0=(valvec))) #213 (line num in coconut source) - return self.retry_get_values(temp=(self.temp + constants.openai_default_temp) / 2, cached_values=legal_values) #214 (line num in coconut source) + print("== COMPLETION ==\n" + completion) #195 (line num in coconut source) + valstr = to_python(completion.split(")", 1)[0].strip(), self.params) #196 (line num in coconut source) + valvec = literal_eval("(" + valstr + ",)") #197 (line num in coconut source) + except BaseException as parse_err: #198 (line num in coconut source) + if self.debug: #199 (line num in coconut source) + print("== END ==") #200 (line num in coconut source) + if self.debug: #201 (line num in coconut source) + print("ERROR: {_coconut_format_0} for API response:\n{_coconut_format_1}".format(_coconut_format_0=(parse_err), _coconut_format_1=(response))) #202 (line num in coconut source) + return self.retry_get_values(temp=(self.temp + constants.openai_default_temp) / 2) #203 (line num in coconut source) + if self.debug: #204 (line num in coconut source) + print("== END ==") #205 (line num in coconut source) + @_coconut_mark_as_match #206 (line num in coconut source) + def _coconut_lambda_0(*_coconut_match_args, **_coconut_match_kwargs): #206 (line num in coconut source) + _coconut_match_check_0 = False #206 (line num in coconut source) + _coconut_match_set_name_val = _coconut_sentinel #206 (line num in coconut source) + _coconut_match_set_name_name = _coconut_sentinel #206 (line num in coconut source) + _coconut_match_set_name_func = _coconut_sentinel #206 (line num in coconut source) + _coconut_match_set_name_args = _coconut_sentinel #206 (line num in coconut source) + _coconut_match_set_name_kwargs = _coconut_sentinel #206 (line num in coconut source) + _coconut_FunctionMatchError = _coconut_get_function_match_error() #206 (line num in coconut source) + if _coconut.len(_coconut_match_args) == 1: #206 (line num in coconut source) + if (_coconut.isinstance(_coconut_match_args[0], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0]) == 2) and (_coconut.isinstance(_coconut_match_args[0][1], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0][1]) == 2) and (_coconut.isinstance(_coconut_match_args[0][1][1], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0][1][1]) == 3): #206 (line num in coconut source) + _coconut_match_set_name_val = _coconut_match_args[0][0] #206 (line num in coconut source) + _coconut_match_set_name_name = _coconut_match_args[0][1][0] #206 (line num in coconut source) + _coconut_match_set_name_func = _coconut_match_args[0][1][1][0] #206 (line num in coconut source) + _coconut_match_set_name_args = _coconut_match_args[0][1][1][1] #206 (line num in coconut source) + _coconut_match_set_name_kwargs = _coconut_match_args[0][1][1][2] #206 (line num in coconut source) + if not _coconut_match_kwargs: #206 (line num in coconut source) + _coconut_match_check_0 = True #206 (line num in coconut source) + if _coconut_match_check_0: #206 (line num in coconut source) + if _coconut_match_set_name_val is not _coconut_sentinel: #206 (line num in coconut source) + val = _coconut_match_set_name_val #206 (line num in coconut source) + if _coconut_match_set_name_name is not _coconut_sentinel: #206 (line num in coconut source) + name = _coconut_match_set_name_name #206 (line num in coconut source) + if _coconut_match_set_name_func is not _coconut_sentinel: #206 (line num in coconut source) + func = _coconut_match_set_name_func #206 (line num in coconut source) + if _coconut_match_set_name_args is not _coconut_sentinel: #206 (line num in coconut source) + args = _coconut_match_set_name_args #206 (line num in coconut source) + if _coconut_match_set_name_kwargs is not _coconut_sentinel: #206 (line num in coconut source) + kwargs = _coconut_match_set_name_kwargs #206 (line num in coconut source) + if not _coconut_match_check_0: #206 (line num in coconut source) + raise _coconut_FunctionMatchError('|> takewhile$(def ((val, (name, (func, args, kwargs)))) ->', _coconut_match_args) #206 (line num in coconut source) + return param_processor.in_support(name, val, func, *args, **kwargs) #206 (line num in coconut source) + legal_values = ((tuple)((map)(_coconut.operator.itemgetter((0)), (takewhile)(_coconut_lambda_0, (_coconut_partial(zip, {1: self.params.items()}, 2))((self.cached_values + valvec)[_coconut.slice(None, len(self.params))]))))) #206 (line num in coconut source) + if len(legal_values) < len(self.params): #215 (line num in coconut source) + if self.debug: #216 (line num in coconut source) + if len(valvec) < len(self.params) - len(self.cached_values): #217 (line num in coconut source) + print("ERROR: insufficient values (got {_coconut_format_0}; expected {_coconut_format_1})".format(_coconut_format_0=(len(valvec)), _coconut_format_1=(len(self.params) - len(self.cached_values)))) #218 (line num in coconut source) + else: #219 (line num in coconut source) + print("ERROR: got illegal values: {_coconut_format_0!r}".format(_coconut_format_0=(valvec))) #220 (line num in coconut source) + return self.retry_get_values(temp=(self.temp + constants.openai_default_temp) / 2, cached_values=legal_values) #221 (line num in coconut source) # return values - values = dict(((name), (val)) for name, val in zip(self.params, legal_values)) #217 (line num in coconut source) - if values in self.data_points: #218 (line num in coconut source) - if self.debug: #219 (line num in coconut source) - print("ERROR: got duplicate point: {_coconut_format_0!r}".format(_coconut_format_0=(legal_values))) #220 (line num in coconut source) - return self.retry_get_values(temp=self.temp + (constants.openai_max_temp - self.temp) / 2) #221 (line num in coconut source) - return values #222 (line num in coconut source) - - - def retry_get_values(self, temp=None, cached_values=None): #224 (line num in coconut source) - """Used in get_next_values to keep track of recursive calls.""" #225 (line num in coconut source) - if not self.max_retries: #226 (line num in coconut source) - printerr("BBopt Warning: Maximum number of OpenAI API retries exceeded on:\n== PROMPT ==\n{_coconut_format_0}\n== END ==".format(_coconut_format_0=(self.get_prompt()))) #227 (line num in coconut source) - return {} # return empty values so that the fallback random backend will be used instead #228 (line num in coconut source) - if self.debug: #229 (line num in coconut source) - if temp is None: #230 (line num in coconut source) - print("RETRYING with: self.max_prompt_len={_coconut_format_0}".format(_coconut_format_0=(self.max_prompt_len))) #231 (line num in coconut source) - else: #232 (line num in coconut source) - print("RETRYING with new temperature: {_coconut_format_0} -> {_coconut_format_1}".format(_coconut_format_0=(self.temp), _coconut_format_1=(temp))) #233 (line num in coconut source) - old_retries, self.max_retries = self.max_retries, self.max_retries - 1 #234 (line num in coconut source) - if temp is not None: #235 (line num in coconut source) - old_temp, self.temp = self.temp, temp #236 (line num in coconut source) - if cached_values is not None: #237 (line num in coconut source) - if self.debug: #238 (line num in coconut source) - print("CACHING values: {_coconut_format_0} + {_coconut_format_1}".format(_coconut_format_0=(cached_values[:len(self.cached_values)]), _coconut_format_1=(cached_values[len(self.cached_values):]))) #239 (line num in coconut source) - self.cached_values = cached_values #240 (line num in coconut source) - try: #241 (line num in coconut source) - return self.get_next_values() #242 (line num in coconut source) - finally: #243 (line num in coconut source) - self.max_retries = old_retries #244 (line num in coconut source) - if temp is not None: #245 (line num in coconut source) - self.temp = old_temp #246 (line num in coconut source) - if cached_values is not None: #247 (line num in coconut source) - self.cached_values = () #248 (line num in coconut source) + values = dict(((name), (val)) for name, val in zip(self.params, legal_values)) #224 (line num in coconut source) + if values in self.data_points: #225 (line num in coconut source) + if self.debug: #226 (line num in coconut source) + print("ERROR: got duplicate point: {_coconut_format_0!r}".format(_coconut_format_0=(legal_values))) #227 (line num in coconut source) + return self.retry_get_values(temp=self.temp + (constants.openai_max_temp - self.temp) / 2, cached_values=()) #228 (line num in coconut source) + return values #229 (line num in coconut source) + + + def retry_get_values(self, temp=None, cached_values=None): #231 (line num in coconut source) + """Used in get_next_values to keep track of recursive calls.""" #232 (line num in coconut source) + if not self.max_retries: #233 (line num in coconut source) + printerr("BBopt Warning: Maximum number of OpenAI API retries exceeded on:\n== PROMPT ==\n{_coconut_format_0}\n== END ==".format(_coconut_format_0=(self.get_prompt()))) #234 (line num in coconut source) + return {} # return empty values so that the fallback random backend will be used instead #235 (line num in coconut source) + if self.debug: #236 (line num in coconut source) + if temp is None: #237 (line num in coconut source) + print("RETRYING with: self.max_prompt_len={_coconut_format_0}".format(_coconut_format_0=(self.max_prompt_len))) #238 (line num in coconut source) + else: #239 (line num in coconut source) + print("RETRYING with new temperature: {_coconut_format_0} -> {_coconut_format_1}".format(_coconut_format_0=(self.temp), _coconut_format_1=(temp))) #240 (line num in coconut source) + old_retries, self.max_retries = self.max_retries, self.max_retries - 1 #241 (line num in coconut source) + if temp is not None: #242 (line num in coconut source) + old_temp, self.temp = self.temp, temp #243 (line num in coconut source) + if cached_values is not None: #244 (line num in coconut source) + if self.debug: #245 (line num in coconut source) + print("CACHING values: {_coconut_format_0} -> {_coconut_format_1}".format(_coconut_format_0=(self.cached_values), _coconut_format_1=(cached_values))) #246 (line num in coconut source) + self.cached_values = cached_values #247 (line num in coconut source) + try: #248 (line num in coconut source) + return self.get_next_values() #249 (line num in coconut source) + finally: #250 (line num in coconut source) + self.max_retries = old_retries #251 (line num in coconut source) + if temp is not None: #252 (line num in coconut source) + self.temp = old_temp #253 (line num in coconut source) + if cached_values is not None: #254 (line num in coconut source) + self.cached_values = () #255 (line num in coconut source) # Registered names: -_coconut_call_set_names(OpenAIBackend) #253 (line num in coconut source) -OpenAIBackend.register() #253 (line num in coconut source) -OpenAIBackend.register_alg("openai") #254 (line num in coconut source) +_coconut_call_set_names(OpenAIBackend) #260 (line num in coconut source) +OpenAIBackend.register() #260 (line num in coconut source) +OpenAIBackend.register_alg("openai") #261 (line num in coconut source) +OpenAIBackend.register_alg("openai_debug", debug=True) #262 (line num in coconut source) diff --git a/bbopt/backends/util.py b/bbopt/backends/util.py index aec64ce..8468c65 100644 --- a/bbopt/backends/util.py +++ b/bbopt/backends/util.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x1ee25f74 +# __coconut_hash__ = 0xac50d5bb # Compiled with Coconut version 2.0.0-a_dev53 [How Not to Be Seen] @@ -41,617 +41,624 @@ import random #5 (line num in coconut source) -if _coconut_sys.version_info < (3, 3): #7 (line num in coconut source) - from collections import Iterable #7 (line num in coconut source) -else: #7 (line num in coconut source) - from collections.abc import Iterable #7 (line num in coconut source) +from collections import OrderedDict #7 (line num in coconut source) +if _coconut_sys.version_info < (3, 3): #8 (line num in coconut source) + from collections import Iterable #8 (line num in coconut source) +else: #8 (line num in coconut source) + from collections.abc import Iterable #8 (line num in coconut source) -from bbopt import constants #9 (line num in coconut source) -from bbopt.params import param_processor #10 (line num in coconut source) -from bbopt.util import sorted_items #11 (line num in coconut source) -from bbopt.util import convert_match_errors #11 (line num in coconut source) -from bbopt.util import DictProxy #11 (line num in coconut source) -from bbopt.util import ListProxy #11 (line num in coconut source) -from bbopt.util import mean #11 (line num in coconut source) -from bbopt.registry import backend_registry #18 (line num in coconut source) -from bbopt.registry import alg_registry #18 (line num in coconut source) -from bbopt.registry import meta_registry #18 (line num in coconut source) +from bbopt import constants #10 (line num in coconut source) +from bbopt.params import param_processor #11 (line num in coconut source) +from bbopt.util import sorted_items #12 (line num in coconut source) +from bbopt.util import convert_match_errors #12 (line num in coconut source) +from bbopt.util import DictProxy #12 (line num in coconut source) +from bbopt.util import ListProxy #12 (line num in coconut source) +from bbopt.util import mean #12 (line num in coconut source) +from bbopt.registry import backend_registry #19 (line num in coconut source) +from bbopt.registry import alg_registry #19 (line num in coconut source) +from bbopt.registry import meta_registry #19 (line num in coconut source) # Utilities: -@convert_match_errors #27 (line num in coconut source) -@_coconut_mark_as_match #28 (line num in coconut source) -def _init_backend(*_coconut_match_args, **_coconut_match_kwargs): #28 (line num in coconut source) - """Create a backend object with the given data (backend can be backend name or class).""" #29 (line num in coconut source) - _coconut_match_check_0 = False #30 (line num in coconut source) - _coconut_match_set_name_backend_cls = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name_examples = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name_params = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name_args = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name__attempt_to_update_backend = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name__on_new_backend = _coconut_sentinel #30 (line num in coconut source) - _coconut_match_set_name_options = _coconut_sentinel #30 (line num in coconut source) - _coconut_FunctionMatchError = _coconut_get_function_match_error() #30 (line num in coconut source) - if (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "backend_cls" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "examples" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "params" in _coconut_match_kwargs)) == 1): #30 (line num in coconut source) - _coconut_match_set_name_args = _coconut_match_args[3:] #30 (line num in coconut source) - _coconut_match_temp_3 = _coconut_match_kwargs.pop("_attempt_to_update_backend") if "_attempt_to_update_backend" in _coconut_match_kwargs else None #30 (line num in coconut source) - _coconut_match_temp_4 = _coconut_match_kwargs.pop("_on_new_backend") if "_on_new_backend" in _coconut_match_kwargs else None #30 (line num in coconut source) - _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("backend_cls") #30 (line num in coconut source) - _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("examples") #30 (line num in coconut source) - _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("params") #30 (line num in coconut source) - _coconut_match_set_name__attempt_to_update_backend = _coconut_match_temp_3 #30 (line num in coconut source) - _coconut_match_set_name__on_new_backend = _coconut_match_temp_4 #30 (line num in coconut source) - _coconut_match_set_name_backend_cls = _coconut_match_temp_0 #30 (line num in coconut source) - _coconut_match_set_name_examples = _coconut_match_temp_1 #30 (line num in coconut source) - _coconut_match_set_name_params = _coconut_match_temp_2 #30 (line num in coconut source) - _coconut_match_set_name_options = _coconut_match_kwargs #30 (line num in coconut source) - _coconut_match_check_0 = True #30 (line num in coconut source) - if _coconut_match_check_0: #30 (line num in coconut source) - if _coconut_match_set_name_backend_cls is not _coconut_sentinel: #30 (line num in coconut source) - backend_cls = _coconut_match_set_name_backend_cls #30 (line num in coconut source) - if _coconut_match_set_name_examples is not _coconut_sentinel: #30 (line num in coconut source) - examples = _coconut_match_set_name_examples #30 (line num in coconut source) - if _coconut_match_set_name_params is not _coconut_sentinel: #30 (line num in coconut source) - params = _coconut_match_set_name_params #30 (line num in coconut source) - if _coconut_match_set_name_args is not _coconut_sentinel: #30 (line num in coconut source) - args = _coconut_match_set_name_args #30 (line num in coconut source) - if _coconut_match_set_name__attempt_to_update_backend is not _coconut_sentinel: #30 (line num in coconut source) - _attempt_to_update_backend = _coconut_match_set_name__attempt_to_update_backend #30 (line num in coconut source) - if _coconut_match_set_name__on_new_backend is not _coconut_sentinel: #30 (line num in coconut source) - _on_new_backend = _coconut_match_set_name__on_new_backend #30 (line num in coconut source) - if _coconut_match_set_name_options is not _coconut_sentinel: #30 (line num in coconut source) - options = _coconut_match_set_name_options #30 (line num in coconut source) - if not _coconut_match_check_0: #30 (line num in coconut source) - raise _coconut_FunctionMatchError('match def _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=None, _on_new_backend=None, **options):', _coconut_match_args) #30 (line num in coconut source) - - backend_examples = examples[:] #30 (line num in coconut source) - backend_params = params.copy() #31 (line num in coconut source) - - new_backend = None #33 (line num in coconut source) - if isinstance(_attempt_to_update_backend, backend_cls): #34 (line num in coconut source) - updated_backend = _attempt_to_update_backend.attempt_update(backend_examples, backend_params, *args, **options) #35 (line num in coconut source) - if updated_backend is True: #36 (line num in coconut source) - new_backend = _attempt_to_update_backend #37 (line num in coconut source) - elif isinstance(updated_backend, backend_cls): #38 (line num in coconut source) - new_backend = updated_backend #39 (line num in coconut source) - else: #40 (line num in coconut source) - assert updated_backend is False, "invalid {_coconut_format_0}.attempt_update return value: {_coconut_format_1}".format(_coconut_format_0=(backend_cls), _coconut_format_1=(updated_backend)) #41 (line num in coconut source) - - if new_backend is None: #43 (line num in coconut source) - assert not _attempt_to_update_backend or isinstance(_attempt_to_update_backend, Backend), "invalid backend to attempt update on: {_coconut_format_0}".format(_coconut_format_0=(_attempt_to_update_backend)) #44 (line num in coconut source) - new_backend = backend_cls(backend_examples, backend_params, *args, **options) #45 (line num in coconut source) - if _on_new_backend is not None: #46 (line num in coconut source) - _on_new_backend(new_backend) #47 (line num in coconut source) - - return new_backend #49 (line num in coconut source) - - - -def _make_safe_backend_store(backend_store, remove_backends): #52 (line num in coconut source) - """Get a new backend_store without the given remove_backends.""" #53 (line num in coconut source) - safe_backend_store = DictProxy(old_dict=backend_store, new_dict=backend_store.copy()) #54 (line num in coconut source) - for backend_cls in backend_store: #55 (line num in coconut source) - if any((isinstance(rem_backend, backend_cls) for rem_backend in remove_backends)): #56 (line num in coconut source) - safe_specific_backends = [] #57 (line num in coconut source) - for stored_args, stored_options, stored_backend in backend_store[backend_cls]: #58 (line num in coconut source) - if stored_backend not in remove_backends: #59 (line num in coconut source) - safe_specific_backends.append((stored_args, stored_options, stored_backend)) #60 (line num in coconut source) - safe_backend_store[backend_cls] = ListProxy(old_list=backend_store[backend_cls], new_list=safe_specific_backends) #61 (line num in coconut source) - return safe_backend_store #62 (line num in coconut source) - - - -@_coconut_mark_as_match #65 (line num in coconut source) -def get_backend(*_coconut_match_args, **_coconut_match_kwargs): #65 (line num in coconut source) - """Create a backend object, attempting to update a backend from backend_store.""" #66 (line num in coconut source) - _coconut_match_check_1 = False #67 (line num in coconut source) - _coconut_match_set_name_backend_store = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name_backend = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name_examples = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name_params = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name_args = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name__current_backend = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name__on_new_backend = _coconut_sentinel #67 (line num in coconut source) - _coconut_match_set_name_options = _coconut_sentinel #67 (line num in coconut source) - _coconut_FunctionMatchError = _coconut_get_function_match_error() #67 (line num in coconut source) - if (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "backend_store" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "backend" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "examples" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 3, "params" in _coconut_match_kwargs)) == 1): #67 (line num in coconut source) - _coconut_match_set_name_args = _coconut_match_args[4:] #67 (line num in coconut source) - _coconut_match_temp_9 = _coconut_match_kwargs.pop("_current_backend") if "_current_backend" in _coconut_match_kwargs else None #67 (line num in coconut source) - _coconut_match_temp_10 = _coconut_match_kwargs.pop("_on_new_backend") if "_on_new_backend" in _coconut_match_kwargs else None #67 (line num in coconut source) - _coconut_match_temp_5 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("backend_store") #67 (line num in coconut source) - _coconut_match_temp_6 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("backend") #67 (line num in coconut source) - _coconut_match_temp_7 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("examples") #67 (line num in coconut source) - _coconut_match_temp_8 = _coconut_match_args[3] if _coconut.len(_coconut_match_args) > 3 else _coconut_match_kwargs.pop("params") #67 (line num in coconut source) - _coconut_match_set_name__current_backend = _coconut_match_temp_9 #67 (line num in coconut source) - _coconut_match_set_name__on_new_backend = _coconut_match_temp_10 #67 (line num in coconut source) - _coconut_match_set_name_backend_store = _coconut_match_temp_5 #67 (line num in coconut source) - _coconut_match_set_name_backend = _coconut_match_temp_6 #67 (line num in coconut source) - _coconut_match_set_name_examples = _coconut_match_temp_7 #67 (line num in coconut source) - _coconut_match_set_name_params = _coconut_match_temp_8 #67 (line num in coconut source) - _coconut_match_set_name_options = _coconut_match_kwargs #67 (line num in coconut source) - _coconut_match_check_1 = True #67 (line num in coconut source) - if _coconut_match_check_1: #67 (line num in coconut source) - if _coconut_match_set_name_backend_store is not _coconut_sentinel: #67 (line num in coconut source) - backend_store = _coconut_match_set_name_backend_store #67 (line num in coconut source) - if _coconut_match_set_name_backend is not _coconut_sentinel: #67 (line num in coconut source) - backend = _coconut_match_set_name_backend #67 (line num in coconut source) - if _coconut_match_set_name_examples is not _coconut_sentinel: #67 (line num in coconut source) - examples = _coconut_match_set_name_examples #67 (line num in coconut source) - if _coconut_match_set_name_params is not _coconut_sentinel: #67 (line num in coconut source) - params = _coconut_match_set_name_params #67 (line num in coconut source) - if _coconut_match_set_name_args is not _coconut_sentinel: #67 (line num in coconut source) - args = _coconut_match_set_name_args #67 (line num in coconut source) - if _coconut_match_set_name__current_backend is not _coconut_sentinel: #67 (line num in coconut source) - _current_backend = _coconut_match_set_name__current_backend #67 (line num in coconut source) - if _coconut_match_set_name__on_new_backend is not _coconut_sentinel: #67 (line num in coconut source) - _on_new_backend = _coconut_match_set_name__on_new_backend #67 (line num in coconut source) - if _coconut_match_set_name_options is not _coconut_sentinel: #67 (line num in coconut source) - options = _coconut_match_set_name_options #67 (line num in coconut source) - if not _coconut_match_check_1: #67 (line num in coconut source) - raise _coconut_FunctionMatchError('match def get_backend(backend_store, backend, examples, params, *args, _current_backend=None, _on_new_backend=None, **options):', _coconut_match_args) #67 (line num in coconut source) - - if isinstance(backend, type) and issubclass(backend, Backend): #67 (line num in coconut source) - backend_cls = backend #68 (line num in coconut source) - else: #69 (line num in coconut source) - backend_cls = backend_registry[backend] #70 (line num in coconut source) - assert issubclass(backend_cls, Backend), "invalid backend class for {_coconut_format_0}: {_coconut_format_1}".format(_coconut_format_0=(backend), _coconut_format_1=(backend_cls)) #71 (line num in coconut source) - - store_ind = None #73 (line num in coconut source) - attempt_to_update_backend = _current_backend #74 (line num in coconut source) - for i, (stored_args, stored_options, stored_backend) in enumerate(backend_store[backend_cls]): #75 (line num in coconut source) - attempt_to_update_backend = stored_backend #76 (line num in coconut source) - if stored_args == args and stored_options == options: #77 (line num in coconut source) - store_ind = i #78 (line num in coconut source) - break #79 (line num in coconut source) - - if backend_cls.request_backend_store: #81 (line num in coconut source) - init_options = options.copy() #82 (line num in coconut source) - init_options["_backend_store"] = _make_safe_backend_store(backend_store, (attempt_to_update_backend,)) #83 (line num in coconut source) - else: #84 (line num in coconut source) - init_options = options #85 (line num in coconut source) - - new_backend = _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=attempt_to_update_backend, _on_new_backend=_on_new_backend, **init_options) #87 (line num in coconut source) - - if store_ind is None: #97 (line num in coconut source) - backend_store[backend_cls].append((args, options, new_backend)) #98 (line num in coconut source) - else: #99 (line num in coconut source) - backend_store[backend_cls][store_ind] = (args, options, new_backend) #100 (line num in coconut source) - - return new_backend #102 (line num in coconut source) - - - -def negate_objective(objective): #105 (line num in coconut source) - """Take the negative of the given objective (converts a gain into a loss and vice versa).""" #106 (line num in coconut source) - if isinstance(objective, Iterable): #107 (line num in coconut source) - return (list)((map)(negate_objective, objective)) #108 (line num in coconut source) - else: #109 (line num in coconut source) - return -objective #110 (line num in coconut source) - - - -def get_names_and_features(values, params, fallback_func=param_processor.choose_default_placeholder, converters={}, convert_fallback=True,): #113 (line num in coconut source) +def sorted_params(params): #28 (line num in coconut source) + """Get an OrderedDict of params in sorted order.""" #29 (line num in coconut source) + return (OrderedDict)((sorted_items)(params)) #30 (line num in coconut source) + + + +@convert_match_errors #33 (line num in coconut source) +@_coconut_mark_as_match #34 (line num in coconut source) +def _init_backend(*_coconut_match_args, **_coconut_match_kwargs): #34 (line num in coconut source) + """Create a backend object with the given data (backend can be backend name or class).""" #35 (line num in coconut source) + _coconut_match_check_0 = False #36 (line num in coconut source) + _coconut_match_set_name_backend_cls = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name_examples = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name_params = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name_args = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name__attempt_to_update_backend = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name__on_new_backend = _coconut_sentinel #36 (line num in coconut source) + _coconut_match_set_name_options = _coconut_sentinel #36 (line num in coconut source) + _coconut_FunctionMatchError = _coconut_get_function_match_error() #36 (line num in coconut source) + if (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "backend_cls" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "examples" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "params" in _coconut_match_kwargs)) == 1): #36 (line num in coconut source) + _coconut_match_set_name_args = _coconut_match_args[3:] #36 (line num in coconut source) + _coconut_match_temp_3 = _coconut_match_kwargs.pop("_attempt_to_update_backend") if "_attempt_to_update_backend" in _coconut_match_kwargs else None #36 (line num in coconut source) + _coconut_match_temp_4 = _coconut_match_kwargs.pop("_on_new_backend") if "_on_new_backend" in _coconut_match_kwargs else None #36 (line num in coconut source) + _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("backend_cls") #36 (line num in coconut source) + _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("examples") #36 (line num in coconut source) + _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("params") #36 (line num in coconut source) + _coconut_match_set_name__attempt_to_update_backend = _coconut_match_temp_3 #36 (line num in coconut source) + _coconut_match_set_name__on_new_backend = _coconut_match_temp_4 #36 (line num in coconut source) + _coconut_match_set_name_backend_cls = _coconut_match_temp_0 #36 (line num in coconut source) + _coconut_match_set_name_examples = _coconut_match_temp_1 #36 (line num in coconut source) + _coconut_match_set_name_params = _coconut_match_temp_2 #36 (line num in coconut source) + _coconut_match_set_name_options = _coconut_match_kwargs #36 (line num in coconut source) + _coconut_match_check_0 = True #36 (line num in coconut source) + if _coconut_match_check_0: #36 (line num in coconut source) + if _coconut_match_set_name_backend_cls is not _coconut_sentinel: #36 (line num in coconut source) + backend_cls = _coconut_match_set_name_backend_cls #36 (line num in coconut source) + if _coconut_match_set_name_examples is not _coconut_sentinel: #36 (line num in coconut source) + examples = _coconut_match_set_name_examples #36 (line num in coconut source) + if _coconut_match_set_name_params is not _coconut_sentinel: #36 (line num in coconut source) + params = _coconut_match_set_name_params #36 (line num in coconut source) + if _coconut_match_set_name_args is not _coconut_sentinel: #36 (line num in coconut source) + args = _coconut_match_set_name_args #36 (line num in coconut source) + if _coconut_match_set_name__attempt_to_update_backend is not _coconut_sentinel: #36 (line num in coconut source) + _attempt_to_update_backend = _coconut_match_set_name__attempt_to_update_backend #36 (line num in coconut source) + if _coconut_match_set_name__on_new_backend is not _coconut_sentinel: #36 (line num in coconut source) + _on_new_backend = _coconut_match_set_name__on_new_backend #36 (line num in coconut source) + if _coconut_match_set_name_options is not _coconut_sentinel: #36 (line num in coconut source) + options = _coconut_match_set_name_options #36 (line num in coconut source) + if not _coconut_match_check_0: #36 (line num in coconut source) + raise _coconut_FunctionMatchError('match def _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=None, _on_new_backend=None, **options):', _coconut_match_args) #36 (line num in coconut source) + + backend_examples = examples[:] #36 (line num in coconut source) + backend_params = params.copy() #37 (line num in coconut source) + + new_backend = None #39 (line num in coconut source) + if isinstance(_attempt_to_update_backend, backend_cls): #40 (line num in coconut source) + updated_backend = _attempt_to_update_backend.attempt_update(backend_examples, backend_params, *args, **options) #41 (line num in coconut source) + if updated_backend is True: #42 (line num in coconut source) + new_backend = _attempt_to_update_backend #43 (line num in coconut source) + elif isinstance(updated_backend, backend_cls): #44 (line num in coconut source) + new_backend = updated_backend #45 (line num in coconut source) + else: #46 (line num in coconut source) + assert updated_backend is False, "invalid {_coconut_format_0}.attempt_update return value: {_coconut_format_1}".format(_coconut_format_0=(backend_cls), _coconut_format_1=(updated_backend)) #47 (line num in coconut source) + + if new_backend is None: #49 (line num in coconut source) + assert not _attempt_to_update_backend or isinstance(_attempt_to_update_backend, Backend), "invalid backend to attempt update on: {_coconut_format_0}".format(_coconut_format_0=(_attempt_to_update_backend)) #50 (line num in coconut source) + new_backend = backend_cls(backend_examples, backend_params, *args, **options) #51 (line num in coconut source) + if _on_new_backend is not None: #52 (line num in coconut source) + _on_new_backend(new_backend) #53 (line num in coconut source) + + return new_backend #55 (line num in coconut source) + + + +def _make_safe_backend_store(backend_store, remove_backends): #58 (line num in coconut source) + """Get a new backend_store without the given remove_backends.""" #59 (line num in coconut source) + safe_backend_store = DictProxy(old_dict=backend_store, new_dict=backend_store.copy()) #60 (line num in coconut source) + for backend_cls in backend_store: #61 (line num in coconut source) + if any((isinstance(rem_backend, backend_cls) for rem_backend in remove_backends)): #62 (line num in coconut source) + safe_specific_backends = [] #63 (line num in coconut source) + for stored_args, stored_options, stored_backend in backend_store[backend_cls]: #64 (line num in coconut source) + if stored_backend not in remove_backends: #65 (line num in coconut source) + safe_specific_backends.append((stored_args, stored_options, stored_backend)) #66 (line num in coconut source) + safe_backend_store[backend_cls] = ListProxy(old_list=backend_store[backend_cls], new_list=safe_specific_backends) #67 (line num in coconut source) + return safe_backend_store #68 (line num in coconut source) + + + +@_coconut_mark_as_match #71 (line num in coconut source) +def get_backend(*_coconut_match_args, **_coconut_match_kwargs): #71 (line num in coconut source) + """Create a backend object, attempting to update a backend from backend_store.""" #72 (line num in coconut source) + _coconut_match_check_1 = False #73 (line num in coconut source) + _coconut_match_set_name_backend_store = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name_backend = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name_examples = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name_params = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name_args = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name__current_backend = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name__on_new_backend = _coconut_sentinel #73 (line num in coconut source) + _coconut_match_set_name_options = _coconut_sentinel #73 (line num in coconut source) + _coconut_FunctionMatchError = _coconut_get_function_match_error() #73 (line num in coconut source) + if (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "backend_store" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "backend" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "examples" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 3, "params" in _coconut_match_kwargs)) == 1): #73 (line num in coconut source) + _coconut_match_set_name_args = _coconut_match_args[4:] #73 (line num in coconut source) + _coconut_match_temp_9 = _coconut_match_kwargs.pop("_current_backend") if "_current_backend" in _coconut_match_kwargs else None #73 (line num in coconut source) + _coconut_match_temp_10 = _coconut_match_kwargs.pop("_on_new_backend") if "_on_new_backend" in _coconut_match_kwargs else None #73 (line num in coconut source) + _coconut_match_temp_5 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("backend_store") #73 (line num in coconut source) + _coconut_match_temp_6 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("backend") #73 (line num in coconut source) + _coconut_match_temp_7 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("examples") #73 (line num in coconut source) + _coconut_match_temp_8 = _coconut_match_args[3] if _coconut.len(_coconut_match_args) > 3 else _coconut_match_kwargs.pop("params") #73 (line num in coconut source) + _coconut_match_set_name__current_backend = _coconut_match_temp_9 #73 (line num in coconut source) + _coconut_match_set_name__on_new_backend = _coconut_match_temp_10 #73 (line num in coconut source) + _coconut_match_set_name_backend_store = _coconut_match_temp_5 #73 (line num in coconut source) + _coconut_match_set_name_backend = _coconut_match_temp_6 #73 (line num in coconut source) + _coconut_match_set_name_examples = _coconut_match_temp_7 #73 (line num in coconut source) + _coconut_match_set_name_params = _coconut_match_temp_8 #73 (line num in coconut source) + _coconut_match_set_name_options = _coconut_match_kwargs #73 (line num in coconut source) + _coconut_match_check_1 = True #73 (line num in coconut source) + if _coconut_match_check_1: #73 (line num in coconut source) + if _coconut_match_set_name_backend_store is not _coconut_sentinel: #73 (line num in coconut source) + backend_store = _coconut_match_set_name_backend_store #73 (line num in coconut source) + if _coconut_match_set_name_backend is not _coconut_sentinel: #73 (line num in coconut source) + backend = _coconut_match_set_name_backend #73 (line num in coconut source) + if _coconut_match_set_name_examples is not _coconut_sentinel: #73 (line num in coconut source) + examples = _coconut_match_set_name_examples #73 (line num in coconut source) + if _coconut_match_set_name_params is not _coconut_sentinel: #73 (line num in coconut source) + params = _coconut_match_set_name_params #73 (line num in coconut source) + if _coconut_match_set_name_args is not _coconut_sentinel: #73 (line num in coconut source) + args = _coconut_match_set_name_args #73 (line num in coconut source) + if _coconut_match_set_name__current_backend is not _coconut_sentinel: #73 (line num in coconut source) + _current_backend = _coconut_match_set_name__current_backend #73 (line num in coconut source) + if _coconut_match_set_name__on_new_backend is not _coconut_sentinel: #73 (line num in coconut source) + _on_new_backend = _coconut_match_set_name__on_new_backend #73 (line num in coconut source) + if _coconut_match_set_name_options is not _coconut_sentinel: #73 (line num in coconut source) + options = _coconut_match_set_name_options #73 (line num in coconut source) + if not _coconut_match_check_1: #73 (line num in coconut source) + raise _coconut_FunctionMatchError('match def get_backend(backend_store, backend, examples, params, *args, _current_backend=None, _on_new_backend=None, **options):', _coconut_match_args) #73 (line num in coconut source) + + if isinstance(backend, type) and issubclass(backend, Backend): #73 (line num in coconut source) + backend_cls = backend #74 (line num in coconut source) + else: #75 (line num in coconut source) + backend_cls = backend_registry[backend] #76 (line num in coconut source) + assert issubclass(backend_cls, Backend), "invalid backend class for {_coconut_format_0}: {_coconut_format_1}".format(_coconut_format_0=(backend), _coconut_format_1=(backend_cls)) #77 (line num in coconut source) + + store_ind = None #79 (line num in coconut source) + attempt_to_update_backend = _current_backend #80 (line num in coconut source) + for i, (stored_args, stored_options, stored_backend) in enumerate(backend_store[backend_cls]): #81 (line num in coconut source) + attempt_to_update_backend = stored_backend #82 (line num in coconut source) + if stored_args == args and stored_options == options: #83 (line num in coconut source) + store_ind = i #84 (line num in coconut source) + break #85 (line num in coconut source) + + if backend_cls.request_backend_store: #87 (line num in coconut source) + init_options = options.copy() #88 (line num in coconut source) + init_options["_backend_store"] = _make_safe_backend_store(backend_store, (attempt_to_update_backend,)) #89 (line num in coconut source) + else: #90 (line num in coconut source) + init_options = options #91 (line num in coconut source) + + new_backend = _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=attempt_to_update_backend, _on_new_backend=_on_new_backend, **init_options) #93 (line num in coconut source) + + if store_ind is None: #103 (line num in coconut source) + backend_store[backend_cls].append((args, options, new_backend)) #104 (line num in coconut source) + else: #105 (line num in coconut source) + backend_store[backend_cls][store_ind] = (args, options, new_backend) #106 (line num in coconut source) + + return new_backend #108 (line num in coconut source) + + + +def negate_objective(objective): #111 (line num in coconut source) + """Take the negative of the given objective (converts a gain into a loss and vice versa).""" #112 (line num in coconut source) + if isinstance(objective, Iterable): #113 (line num in coconut source) + return (list)((map)(negate_objective, objective)) #114 (line num in coconut source) + else: #115 (line num in coconut source) + return -objective #116 (line num in coconut source) + + + +def get_names_and_features(values, params, fallback_func=param_processor.choose_default_placeholder, converters={}, convert_fallback=True,): #119 (line num in coconut source) """Return an iterator of (name, feature) for the parameters in sorted order with the given fallback function. If passed, converters must map funcs to functions from (value, *args) -> new_value which will be run - on the resulting value for that func (but only on fallbacks if convert_fallback).""" #122 (line num in coconut source) - for name, (func, args, kwargs) in sorted_items(params): #123 (line num in coconut source) + on the resulting value for that func (but only on fallbacks if convert_fallback).""" #128 (line num in coconut source) + for name, (func, args, kwargs) in sorted_items(params): #129 (line num in coconut source) # determine feature - fallback = False #125 (line num in coconut source) - _coconut_match_to_1 = values #126 (line num in coconut source) - _coconut_match_check_3 = False #126 (line num in coconut source) - _coconut_match_set_name_feature = _coconut_sentinel #126 (line num in coconut source) - if _coconut.isinstance(_coconut_match_to_1, _coconut.abc.Mapping): #126 (line num in coconut source) - _coconut_match_temp_12 = _coconut_match_to_1.get(name, _coconut_sentinel) #126 (line num in coconut source) - if _coconut_match_temp_12 is not _coconut_sentinel: #126 (line num in coconut source) - _coconut_match_set_name_feature = _coconut_match_temp_12 #126 (line num in coconut source) - _coconut_match_check_3 = True #126 (line num in coconut source) - if _coconut_match_check_3: #126 (line num in coconut source) - if _coconut_match_set_name_feature is not _coconut_sentinel: #126 (line num in coconut source) - feature = _coconut_match_set_name_feature #126 (line num in coconut source) - if _coconut_match_check_3: #126 (line num in coconut source) - pass #127 (line num in coconut source) - else: #128 (line num in coconut source) - _coconut_match_to_0 = kwargs #128 (line num in coconut source) - _coconut_match_check_2 = False #128 (line num in coconut source) - _coconut_match_set_name_placeholder_value = _coconut_sentinel #128 (line num in coconut source) - if _coconut.isinstance(_coconut_match_to_0, _coconut.abc.Mapping): #128 (line num in coconut source) - _coconut_match_temp_11 = _coconut_match_to_0.get("placeholder_when_missing", _coconut_sentinel) #128 (line num in coconut source) - if _coconut_match_temp_11 is not _coconut_sentinel: #128 (line num in coconut source) - _coconut_match_set_name_placeholder_value = _coconut_match_temp_11 #128 (line num in coconut source) - _coconut_match_check_2 = True #128 (line num in coconut source) - if _coconut_match_check_2: #128 (line num in coconut source) - if _coconut_match_set_name_placeholder_value is not _coconut_sentinel: #128 (line num in coconut source) - placeholder_value = _coconut_match_set_name_placeholder_value #128 (line num in coconut source) - if _coconut_match_check_2: #128 (line num in coconut source) - feature = placeholder_value #129 (line num in coconut source) - else: #130 (line num in coconut source) - fallback = True #131 (line num in coconut source) - feature = fallback_func(name, func, *args, **kwargs) #132 (line num in coconut source) + fallback = False #131 (line num in coconut source) + _coconut_match_to_1 = values #132 (line num in coconut source) + _coconut_match_check_3 = False #132 (line num in coconut source) + _coconut_match_set_name_feature = _coconut_sentinel #132 (line num in coconut source) + if _coconut.isinstance(_coconut_match_to_1, _coconut.abc.Mapping): #132 (line num in coconut source) + _coconut_match_temp_12 = _coconut_match_to_1.get(name, _coconut_sentinel) #132 (line num in coconut source) + if _coconut_match_temp_12 is not _coconut_sentinel: #132 (line num in coconut source) + _coconut_match_set_name_feature = _coconut_match_temp_12 #132 (line num in coconut source) + _coconut_match_check_3 = True #132 (line num in coconut source) + if _coconut_match_check_3: #132 (line num in coconut source) + if _coconut_match_set_name_feature is not _coconut_sentinel: #132 (line num in coconut source) + feature = _coconut_match_set_name_feature #132 (line num in coconut source) + if _coconut_match_check_3: #132 (line num in coconut source) + pass #133 (line num in coconut source) + else: #134 (line num in coconut source) + _coconut_match_to_0 = kwargs #134 (line num in coconut source) + _coconut_match_check_2 = False #134 (line num in coconut source) + _coconut_match_set_name_placeholder_value = _coconut_sentinel #134 (line num in coconut source) + if _coconut.isinstance(_coconut_match_to_0, _coconut.abc.Mapping): #134 (line num in coconut source) + _coconut_match_temp_11 = _coconut_match_to_0.get("placeholder_when_missing", _coconut_sentinel) #134 (line num in coconut source) + if _coconut_match_temp_11 is not _coconut_sentinel: #134 (line num in coconut source) + _coconut_match_set_name_placeholder_value = _coconut_match_temp_11 #134 (line num in coconut source) + _coconut_match_check_2 = True #134 (line num in coconut source) + if _coconut_match_check_2: #134 (line num in coconut source) + if _coconut_match_set_name_placeholder_value is not _coconut_sentinel: #134 (line num in coconut source) + placeholder_value = _coconut_match_set_name_placeholder_value #134 (line num in coconut source) + if _coconut_match_check_2: #134 (line num in coconut source) + feature = placeholder_value #135 (line num in coconut source) + else: #136 (line num in coconut source) + fallback = True #137 (line num in coconut source) + feature = fallback_func(name, func, *args, **kwargs) #138 (line num in coconut source) # run converters - if not fallback or convert_fallback: #135 (line num in coconut source) - _coconut_match_to_2 = converters #136 (line num in coconut source) - _coconut_match_check_4 = False #136 (line num in coconut source) - _coconut_match_set_name_converter_func = _coconut_sentinel #136 (line num in coconut source) - if _coconut.isinstance(_coconut_match_to_2, _coconut.abc.Mapping): #136 (line num in coconut source) - _coconut_match_temp_13 = _coconut_match_to_2.get(func, _coconut_sentinel) #136 (line num in coconut source) - if _coconut_match_temp_13 is not _coconut_sentinel: #136 (line num in coconut source) - _coconut_match_set_name_converter_func = _coconut_match_temp_13 #136 (line num in coconut source) - _coconut_match_check_4 = True #136 (line num in coconut source) - if _coconut_match_check_4: #136 (line num in coconut source) - if _coconut_match_set_name_converter_func is not _coconut_sentinel: #136 (line num in coconut source) - converter_func = _coconut_match_set_name_converter_func #136 (line num in coconut source) - if _coconut_match_check_4: #136 (line num in coconut source) - feature = converter_func(feature, *args) #137 (line num in coconut source) + if not fallback or convert_fallback: #141 (line num in coconut source) + _coconut_match_to_2 = converters #142 (line num in coconut source) + _coconut_match_check_4 = False #142 (line num in coconut source) + _coconut_match_set_name_converter_func = _coconut_sentinel #142 (line num in coconut source) + if _coconut.isinstance(_coconut_match_to_2, _coconut.abc.Mapping): #142 (line num in coconut source) + _coconut_match_temp_13 = _coconut_match_to_2.get(func, _coconut_sentinel) #142 (line num in coconut source) + if _coconut_match_temp_13 is not _coconut_sentinel: #142 (line num in coconut source) + _coconut_match_set_name_converter_func = _coconut_match_temp_13 #142 (line num in coconut source) + _coconut_match_check_4 = True #142 (line num in coconut source) + if _coconut_match_check_4: #142 (line num in coconut source) + if _coconut_match_set_name_converter_func is not _coconut_sentinel: #142 (line num in coconut source) + converter_func = _coconut_match_set_name_converter_func #142 (line num in coconut source) + if _coconut_match_check_4: #142 (line num in coconut source) + feature = converter_func(feature, *args) #143 (line num in coconut source) - yield name, feature #139 (line num in coconut source) + yield name, feature #145 (line num in coconut source) -def make_features(*args, **kwargs): #142 (line num in coconut source) - """Same as get_names_and_features but just yields the features.""" #143 (line num in coconut source) - _coconut_yield_from_1 = _coconut.iter((starmap)(lambda name, feature: feature, get_names_and_features(*args, **kwargs))) #144 (line num in coconut source) - while True: #144 (line num in coconut source) - try: #144 (line num in coconut source) - yield _coconut.next(_coconut_yield_from_1) #144 (line num in coconut source) - except _coconut.StopIteration as _coconut_yield_err_0: #144 (line num in coconut source) - _coconut_yield_from_0 = _coconut_yield_err_0.args[0] if _coconut.len(_coconut_yield_err_0.args) > 0 else None #144 (line num in coconut source) - break #144 (line num in coconut source) +def make_features(*args, **kwargs): #148 (line num in coconut source) + """Same as get_names_and_features but just yields the features.""" #149 (line num in coconut source) + _coconut_yield_from_1 = _coconut.iter((starmap)(lambda name, feature: feature, get_names_and_features(*args, **kwargs))) #150 (line num in coconut source) + while True: #150 (line num in coconut source) + try: #150 (line num in coconut source) + yield _coconut.next(_coconut_yield_from_1) #150 (line num in coconut source) + except _coconut.StopIteration as _coconut_yield_err_0: #150 (line num in coconut source) + _coconut_yield_from_0 = _coconut_yield_err_0.args[0] if _coconut.len(_coconut_yield_err_0.args) > 0 else None #150 (line num in coconut source) + break #150 (line num in coconut source) - _coconut_yield_from_0 #144 (line num in coconut source) + _coconut_yield_from_0 #150 (line num in coconut source) -def split_examples(examples, params, fallback_func=param_processor.choose_default_placeholder, converters={}, convert_fallback=True,): #147 (line num in coconut source) - """Split examples into a list of data points and a list of losses with the given fallback function.""" #154 (line num in coconut source) - data_points, losses = [], [] #155 (line num in coconut source) - for example in examples: #156 (line num in coconut source) +def split_examples(examples, params, fallback_func=param_processor.choose_default_placeholder, converters={}, convert_fallback=True,): #153 (line num in coconut source) + """Split examples into a list of data points and a list of losses with the given fallback function.""" #160 (line num in coconut source) + data_points, losses = [], [] #161 (line num in coconut source) + for example in examples: #162 (line num in coconut source) # extract values, loss - _coconut_case_match_to_0 = example #159 (line num in coconut source) - _coconut_case_match_check_0 = False #159 (line num in coconut source) - _coconut_match_set_name_values = _coconut_sentinel #159 (line num in coconut source) - _coconut_match_set_name_gain = _coconut_sentinel #159 (line num in coconut source) - if _coconut.isinstance(_coconut_case_match_to_0, _coconut.abc.Mapping): #159 (line num in coconut source) - _coconut_match_temp_14 = _coconut_case_match_to_0.get("values", _coconut_sentinel) #159 (line num in coconut source) - _coconut_match_temp_15 = _coconut_case_match_to_0.get("gain", _coconut_sentinel) #159 (line num in coconut source) - if (_coconut_match_temp_14 is not _coconut_sentinel) and (_coconut_match_temp_15 is not _coconut_sentinel): #159 (line num in coconut source) - _coconut_match_set_name_values = _coconut_match_temp_14 #159 (line num in coconut source) - _coconut_match_set_name_gain = _coconut_match_temp_15 #159 (line num in coconut source) - _coconut_case_match_check_0 = True #159 (line num in coconut source) - if _coconut_case_match_check_0: #159 (line num in coconut source) - if _coconut_match_set_name_values is not _coconut_sentinel: #159 (line num in coconut source) - values = _coconut_match_set_name_values #159 (line num in coconut source) - if _coconut_match_set_name_gain is not _coconut_sentinel: #159 (line num in coconut source) - gain = _coconut_match_set_name_gain #159 (line num in coconut source) - if _coconut_case_match_check_0: #159 (line num in coconut source) - loss = negate_objective(gain) #161 (line num in coconut source) - if not _coconut_case_match_check_0: #162 (line num in coconut source) - _coconut_match_set_name_values = _coconut_sentinel #162 (line num in coconut source) - _coconut_match_set_name_loss = _coconut_sentinel #162 (line num in coconut source) - if _coconut.isinstance(_coconut_case_match_to_0, _coconut.abc.Mapping): #162 (line num in coconut source) - _coconut_match_temp_16 = _coconut_case_match_to_0.get("values", _coconut_sentinel) #162 (line num in coconut source) - _coconut_match_temp_17 = _coconut_case_match_to_0.get("loss", _coconut_sentinel) #162 (line num in coconut source) - if (_coconut_match_temp_16 is not _coconut_sentinel) and (_coconut_match_temp_17 is not _coconut_sentinel): #162 (line num in coconut source) - _coconut_match_set_name_values = _coconut_match_temp_16 #162 (line num in coconut source) - _coconut_match_set_name_loss = _coconut_match_temp_17 #162 (line num in coconut source) - _coconut_case_match_check_0 = True #162 (line num in coconut source) - if _coconut_case_match_check_0: #162 (line num in coconut source) - if _coconut_match_set_name_values is not _coconut_sentinel: #162 (line num in coconut source) - values = _coconut_match_set_name_values #162 (line num in coconut source) - if _coconut_match_set_name_loss is not _coconut_sentinel: #162 (line num in coconut source) - loss = _coconut_match_set_name_loss #162 (line num in coconut source) - if _coconut_case_match_check_0: #162 (line num in coconut source) - pass #163 (line num in coconut source) - if not _coconut_case_match_check_0: #164 (line num in coconut source) - raise ValueError("invalid example {_coconut_format_0}".format(_coconut_format_0=(example))) #165 (line num in coconut source) + _coconut_case_match_to_0 = example #165 (line num in coconut source) + _coconut_case_match_check_0 = False #165 (line num in coconut source) + _coconut_match_set_name_values = _coconut_sentinel #165 (line num in coconut source) + _coconut_match_set_name_gain = _coconut_sentinel #165 (line num in coconut source) + if _coconut.isinstance(_coconut_case_match_to_0, _coconut.abc.Mapping): #165 (line num in coconut source) + _coconut_match_temp_14 = _coconut_case_match_to_0.get("values", _coconut_sentinel) #165 (line num in coconut source) + _coconut_match_temp_15 = _coconut_case_match_to_0.get("gain", _coconut_sentinel) #165 (line num in coconut source) + if (_coconut_match_temp_14 is not _coconut_sentinel) and (_coconut_match_temp_15 is not _coconut_sentinel): #165 (line num in coconut source) + _coconut_match_set_name_values = _coconut_match_temp_14 #165 (line num in coconut source) + _coconut_match_set_name_gain = _coconut_match_temp_15 #165 (line num in coconut source) + _coconut_case_match_check_0 = True #165 (line num in coconut source) + if _coconut_case_match_check_0: #165 (line num in coconut source) + if _coconut_match_set_name_values is not _coconut_sentinel: #165 (line num in coconut source) + values = _coconut_match_set_name_values #165 (line num in coconut source) + if _coconut_match_set_name_gain is not _coconut_sentinel: #165 (line num in coconut source) + gain = _coconut_match_set_name_gain #165 (line num in coconut source) + if _coconut_case_match_check_0: #165 (line num in coconut source) + loss = negate_objective(gain) #167 (line num in coconut source) + if not _coconut_case_match_check_0: #168 (line num in coconut source) + _coconut_match_set_name_values = _coconut_sentinel #168 (line num in coconut source) + _coconut_match_set_name_loss = _coconut_sentinel #168 (line num in coconut source) + if _coconut.isinstance(_coconut_case_match_to_0, _coconut.abc.Mapping): #168 (line num in coconut source) + _coconut_match_temp_16 = _coconut_case_match_to_0.get("values", _coconut_sentinel) #168 (line num in coconut source) + _coconut_match_temp_17 = _coconut_case_match_to_0.get("loss", _coconut_sentinel) #168 (line num in coconut source) + if (_coconut_match_temp_16 is not _coconut_sentinel) and (_coconut_match_temp_17 is not _coconut_sentinel): #168 (line num in coconut source) + _coconut_match_set_name_values = _coconut_match_temp_16 #168 (line num in coconut source) + _coconut_match_set_name_loss = _coconut_match_temp_17 #168 (line num in coconut source) + _coconut_case_match_check_0 = True #168 (line num in coconut source) + if _coconut_case_match_check_0: #168 (line num in coconut source) + if _coconut_match_set_name_values is not _coconut_sentinel: #168 (line num in coconut source) + values = _coconut_match_set_name_values #168 (line num in coconut source) + if _coconut_match_set_name_loss is not _coconut_sentinel: #168 (line num in coconut source) + loss = _coconut_match_set_name_loss #168 (line num in coconut source) + if _coconut_case_match_check_0: #168 (line num in coconut source) + pass #169 (line num in coconut source) + if not _coconut_case_match_check_0: #170 (line num in coconut source) + raise ValueError("invalid example {_coconut_format_0}".format(_coconut_format_0=(example))) #171 (line num in coconut source) # extract features - features = (list)(make_features(values, params, fallback_func, converters, convert_fallback)) #168 (line num in coconut source) + features = (list)(make_features(values, params, fallback_func, converters, convert_fallback)) #174 (line num in coconut source) # add to data_points, losses - (data_points.append)(features) #171 (line num in coconut source) - (losses.append)(loss) #172 (line num in coconut source) + (data_points.append)(features) #177 (line num in coconut source) + (losses.append)(loss) #178 (line num in coconut source) - return data_points, losses #174 (line num in coconut source) + return data_points, losses #180 (line num in coconut source) -def get_named_data_points_and_losses(examples, params, *args, **kwargs): #177 (line num in coconut source) - """Same as split_examples but returns named_data_points instead of data_points.""" #178 (line num in coconut source) - data_points, losses = split_examples(examples, params, *args, **kwargs) #179 (line num in coconut source) - named_data_points = [] #180 (line num in coconut source) - sorted_names = list(sorted(params)) #181 (line num in coconut source) - for point in data_points: #182 (line num in coconut source) - pt_val = {} #183 (line num in coconut source) - for name, item in zip(sorted_names, point): #184 (line num in coconut source) - pt_val[name] = item #185 (line num in coconut source) - named_data_points.append(pt_val) #186 (line num in coconut source) - return named_data_points, losses #187 (line num in coconut source) +def get_named_data_points_and_losses(examples, params, *args, **kwargs): #183 (line num in coconut source) + """Same as split_examples but returns named_data_points instead of data_points.""" #184 (line num in coconut source) + data_points, losses = split_examples(examples, params, *args, **kwargs) #185 (line num in coconut source) + named_data_points = [] #186 (line num in coconut source) + sorted_names = list(sorted(params)) #187 (line num in coconut source) + for point in data_points: #188 (line num in coconut source) + pt_val = {} #189 (line num in coconut source) + for name, item in zip(sorted_names, point): #190 (line num in coconut source) + pt_val[name] = item #191 (line num in coconut source) + named_data_points.append(pt_val) #192 (line num in coconut source) + return named_data_points, losses #193 (line num in coconut source) -def marginalize(named_data_points, losses, param_name, ave_func=mean): #190 (line num in coconut source) - """Get an average loss for each prior value of param_name.""" #191 (line num in coconut source) - losses_for_vals = [] # we can't use a dict since vals might not be hashable #192 (line num in coconut source) - for point, loss in zip(named_data_points, losses): #193 (line num in coconut source) - val = point[param_name] #194 (line num in coconut source) - for check_val, check_losses in losses_for_vals: #195 (line num in coconut source) - if check_val == val: #196 (line num in coconut source) - check_losses.append(loss) #197 (line num in coconut source) - break #198 (line num in coconut source) - else: # no break #199 (line num in coconut source) - losses_for_vals.append((val, [loss,])) #200 (line num in coconut source) +def marginalize(named_data_points, losses, param_name, ave_func=mean): #196 (line num in coconut source) + """Get an average loss for each prior value of param_name.""" #197 (line num in coconut source) + losses_for_vals = [] # we can't use a dict since vals might not be hashable #198 (line num in coconut source) + for point, loss in zip(named_data_points, losses): #199 (line num in coconut source) + val = point[param_name] #200 (line num in coconut source) + for check_val, check_losses in losses_for_vals: #201 (line num in coconut source) + if check_val == val: #202 (line num in coconut source) + check_losses.append(loss) #203 (line num in coconut source) + break #204 (line num in coconut source) + else: # no break #205 (line num in coconut source) + losses_for_vals.append((val, [loss,])) #206 (line num in coconut source) - marginals = [] #202 (line num in coconut source) - for val, all_losses in losses_for_vals: #203 (line num in coconut source) - marginals.append((val, ave_func(all_losses))) #204 (line num in coconut source) - return marginals #205 (line num in coconut source) + marginals = [] #208 (line num in coconut source) + for val, all_losses in losses_for_vals: #209 (line num in coconut source) + marginals.append((val, ave_func(all_losses))) #210 (line num in coconut source) + return marginals #211 (line num in coconut source) -def get_cum_probs_for(distribution): #208 (line num in coconut source) - """Generate cumulative probabilities from the given distribution.""" #209 (line num in coconut source) - cum_probs = [] #210 (line num in coconut source) - total_weight = sum((weight for elem, weight in distribution)) #211 (line num in coconut source) - prev_cutoff = 0 #212 (line num in coconut source) - for elem, weight in distribution: #213 (line num in coconut source) - if weight == float("inf"): #214 (line num in coconut source) - cutoff = 1 #215 (line num in coconut source) - elif weight in (float("-inf"), float("nan")) or total_weight == float("nan"): #216 (line num in coconut source) - cutoff = prev_cutoff #217 (line num in coconut source) - else: #218 (line num in coconut source) - cutoff = prev_cutoff + weight / total_weight #219 (line num in coconut source) - cum_probs.append((elem, cutoff)) #220 (line num in coconut source) - prev_cutoff = cutoff #221 (line num in coconut source) - return cum_probs #222 (line num in coconut source) +def get_cum_probs_for(distribution): #214 (line num in coconut source) + """Generate cumulative probabilities from the given distribution.""" #215 (line num in coconut source) + cum_probs = [] #216 (line num in coconut source) + total_weight = sum((weight for elem, weight in distribution)) #217 (line num in coconut source) + prev_cutoff = 0 #218 (line num in coconut source) + for elem, weight in distribution: #219 (line num in coconut source) + if weight == float("inf"): #220 (line num in coconut source) + cutoff = 1 #221 (line num in coconut source) + elif weight in (float("-inf"), float("nan")) or total_weight == float("nan"): #222 (line num in coconut source) + cutoff = prev_cutoff #223 (line num in coconut source) + else: #224 (line num in coconut source) + cutoff = prev_cutoff + weight / total_weight #225 (line num in coconut source) + cum_probs.append((elem, cutoff)) #226 (line num in coconut source) + prev_cutoff = cutoff #227 (line num in coconut source) + return cum_probs #228 (line num in coconut source) -def random_from_cum_probs(cum_probs): #225 (line num in coconut source) - """Randomly choose an element using cum_probs.""" #226 (line num in coconut source) - rand_val = random.random() #227 (line num in coconut source) - for elem, cutoff in cum_probs: #228 (line num in coconut source) - if rand_val <= cutoff: #229 (line num in coconut source) - return elem #230 (line num in coconut source) - return None #231 (line num in coconut source) +def random_from_cum_probs(cum_probs): #231 (line num in coconut source) + """Randomly choose an element using cum_probs.""" #232 (line num in coconut source) + rand_val = random.random() #233 (line num in coconut source) + for elem, cutoff in cum_probs: #234 (line num in coconut source) + if rand_val <= cutoff: #235 (line num in coconut source) + return elem #236 (line num in coconut source) + return None #237 (line num in coconut source) -def make_values(params, point): #234 (line num in coconut source) +def make_values(params, point): #240 (line num in coconut source) """Return a dictionary with the values replaced by the values in point, - where point is a list of the values corresponding to the sorted params.""" #236 (line num in coconut source) - values = {} #237 (line num in coconut source) - for i, k in (enumerate)((sorted)(params)): #238 (line num in coconut source) - values[k] = point[i] #239 (line num in coconut source) - return values #240 (line num in coconut source) + where point is a list of the values corresponding to the sorted params.""" #242 (line num in coconut source) + values = {} #243 (line num in coconut source) + for i, k in (enumerate)((sorted)(params)): #244 (line num in coconut source) + values[k] = point[i] #245 (line num in coconut source) + return values #246 (line num in coconut source) -def serve_values(name, func, args, kwargs, serving_values, fallback_func, backend_name=None, implemented_funcs=None, supported_kwargs=None,): #243 (line num in coconut source) +def serve_values(name, func, args, kwargs, serving_values, fallback_func, backend_name=None, implemented_funcs=None, supported_kwargs=None,): #249 (line num in coconut source) """Determines the parameter value to serve for the given parameter name and kwargs. First checks for unsupported funcs or kwargs, then uses the following algorithm: 1. if name in serving_values, use serving_values[name], else 2. if guess in kwargs, use the guess, else - 3. call fallback_func(name, func, *args, **kwargs).""" #259 (line num in coconut source) + 3. call fallback_func(name, func, *args, **kwargs).""" #265 (line num in coconut source) # validate arguments - if implemented_funcs is not None: #261 (line num in coconut source) - assert backend_name is not None, "serve_values expects a backend_name argument when doing func validation" #262 (line num in coconut source) - if func not in implemented_funcs: #263 (line num in coconut source) - raise ValueError("the {_coconut_format_0} backend does not implement the {_coconut_format_1} function".format(_coconut_format_0=(backend_name), _coconut_format_1=(func))) #264 (line num in coconut source) - if supported_kwargs is not None: #265 (line num in coconut source) - assert backend_name is not None, "serve_values expects a backend_name argument when doing kwargs validation" #266 (line num in coconut source) - unsupported_kwargs = set(kwargs) - set(supported_kwargs) #267 (line num in coconut source) - if unsupported_kwargs: #268 (line num in coconut source) - raise ValueError("the {_coconut_format_0} backend does not support {_coconut_format_1} option(s)".format(_coconut_format_0=(backend_name), _coconut_format_1=(unsupported_kwargs))) #269 (line num in coconut source) + if implemented_funcs is not None: #267 (line num in coconut source) + assert backend_name is not None, "serve_values expects a backend_name argument when doing func validation" #268 (line num in coconut source) + if func not in implemented_funcs: #269 (line num in coconut source) + raise ValueError("the {_coconut_format_0} backend does not implement the {_coconut_format_1} function".format(_coconut_format_0=(backend_name), _coconut_format_1=(func))) #270 (line num in coconut source) + if supported_kwargs is not None: #271 (line num in coconut source) + assert backend_name is not None, "serve_values expects a backend_name argument when doing kwargs validation" #272 (line num in coconut source) + unsupported_kwargs = set(kwargs) - set(supported_kwargs) #273 (line num in coconut source) + if unsupported_kwargs: #274 (line num in coconut source) + raise ValueError("the {_coconut_format_0} backend does not support {_coconut_format_1} option(s)".format(_coconut_format_0=(backend_name), _coconut_format_1=(unsupported_kwargs))) #275 (line num in coconut source) # determine value - _coconut_match_to_4 = serving_values #272 (line num in coconut source) - _coconut_match_check_6 = False #272 (line num in coconut source) - _coconut_match_set_name_value = _coconut_sentinel #272 (line num in coconut source) - if _coconut.isinstance(_coconut_match_to_4, _coconut.abc.Mapping): #272 (line num in coconut source) - _coconut_match_temp_19 = _coconut_match_to_4.get(name, _coconut_sentinel) #272 (line num in coconut source) - if _coconut_match_temp_19 is not _coconut_sentinel: #272 (line num in coconut source) - _coconut_match_set_name_value = _coconut_match_temp_19 #272 (line num in coconut source) - _coconut_match_check_6 = True #272 (line num in coconut source) - if _coconut_match_check_6: #272 (line num in coconut source) - if _coconut_match_set_name_value is not _coconut_sentinel: #272 (line num in coconut source) - value = _coconut_match_set_name_value #272 (line num in coconut source) - if _coconut_match_check_6: #272 (line num in coconut source) - return value #273 (line num in coconut source) - else: #274 (line num in coconut source) - _coconut_match_to_3 = kwargs #274 (line num in coconut source) - _coconut_match_check_5 = False #274 (line num in coconut source) - _coconut_match_set_name_guess = _coconut_sentinel #274 (line num in coconut source) - if _coconut.isinstance(_coconut_match_to_3, _coconut.abc.Mapping): #274 (line num in coconut source) - _coconut_match_temp_18 = _coconut_match_to_3.get("guess", _coconut_sentinel) #274 (line num in coconut source) - if _coconut_match_temp_18 is not _coconut_sentinel: #274 (line num in coconut source) - _coconut_match_set_name_guess = _coconut_match_temp_18 #274 (line num in coconut source) - _coconut_match_check_5 = True #274 (line num in coconut source) - if _coconut_match_check_5: #274 (line num in coconut source) - if _coconut_match_set_name_guess is not _coconut_sentinel: #274 (line num in coconut source) - guess = _coconut_match_set_name_guess #274 (line num in coconut source) - if _coconut_match_check_5: #274 (line num in coconut source) - return guess #275 (line num in coconut source) - else: #276 (line num in coconut source) - return fallback_func(name, func, *args, **kwargs) #277 (line num in coconut source) + _coconut_match_to_4 = serving_values #278 (line num in coconut source) + _coconut_match_check_6 = False #278 (line num in coconut source) + _coconut_match_set_name_value = _coconut_sentinel #278 (line num in coconut source) + if _coconut.isinstance(_coconut_match_to_4, _coconut.abc.Mapping): #278 (line num in coconut source) + _coconut_match_temp_19 = _coconut_match_to_4.get(name, _coconut_sentinel) #278 (line num in coconut source) + if _coconut_match_temp_19 is not _coconut_sentinel: #278 (line num in coconut source) + _coconut_match_set_name_value = _coconut_match_temp_19 #278 (line num in coconut source) + _coconut_match_check_6 = True #278 (line num in coconut source) + if _coconut_match_check_6: #278 (line num in coconut source) + if _coconut_match_set_name_value is not _coconut_sentinel: #278 (line num in coconut source) + value = _coconut_match_set_name_value #278 (line num in coconut source) + if _coconut_match_check_6: #278 (line num in coconut source) + return value #279 (line num in coconut source) + else: #280 (line num in coconut source) + _coconut_match_to_3 = kwargs #280 (line num in coconut source) + _coconut_match_check_5 = False #280 (line num in coconut source) + _coconut_match_set_name_guess = _coconut_sentinel #280 (line num in coconut source) + if _coconut.isinstance(_coconut_match_to_3, _coconut.abc.Mapping): #280 (line num in coconut source) + _coconut_match_temp_18 = _coconut_match_to_3.get("guess", _coconut_sentinel) #280 (line num in coconut source) + if _coconut_match_temp_18 is not _coconut_sentinel: #280 (line num in coconut source) + _coconut_match_set_name_guess = _coconut_match_temp_18 #280 (line num in coconut source) + _coconut_match_check_5 = True #280 (line num in coconut source) + if _coconut_match_check_5: #280 (line num in coconut source) + if _coconut_match_set_name_guess is not _coconut_sentinel: #280 (line num in coconut source) + guess = _coconut_match_set_name_guess #280 (line num in coconut source) + if _coconut_match_check_5: #280 (line num in coconut source) + return guess #281 (line num in coconut source) + else: #282 (line num in coconut source) + return fallback_func(name, func, *args, **kwargs) #283 (line num in coconut source) # Backend base classes: -class Backend(_coconut.object): #282 (line num in coconut source) - """Base class for all BBopt backends.""" #283 (line num in coconut source) +class Backend(_coconut.object): #288 (line num in coconut source) + """Base class for all BBopt backends.""" #289 (line num in coconut source) # derived classes should always set this - backend_name = None #285 (line num in coconut source) + backend_name = None #291 (line num in coconut source) # derived classes can modify these if they want to further # restrict the set of supported funcs and/or kwargs - implemented_funcs = None #289 (line num in coconut source) - supported_kwargs = ("guess", "placeholder_when_missing") #290 (line num in coconut source) + implemented_funcs = None #295 (line num in coconut source) + supported_kwargs = ("guess", "placeholder_when_missing") #296 (line num in coconut source) # derived classes must set this on each run if they want to # use the default param implementation - current_values = None #297 (line num in coconut source) + current_values = None #303 (line num in coconut source) # derived classes must set this if they want to use the # default fallback_func implementation - fallback_backend = None #301 (line num in coconut source) + fallback_backend = None #307 (line num in coconut source) # derived classes can implement tell_examples(new_examples) # to allow fast updating on new data - tell_examples = None #305 (line num in coconut source) + tell_examples = None #311 (line num in coconut source) # derived classes can set this to True to have a _backend_store keyword # argument passed to __init__ with an object usable in get_backend - request_backend_store = False #309 (line num in coconut source) + request_backend_store = False #315 (line num in coconut source) - def __new__(cls, examples=None, params=None, *args, **kwargs): #311 (line num in coconut source) - __class__ = Backend #312 (line num in coconut source) + def __new__(cls, examples=None, params=None, *args, **kwargs): #317 (line num in coconut source) + __class__ = Backend #318 (line num in coconut source) - self = super().__new__(cls) #312 (line num in coconut source) - if self.tell_examples is not None: #313 (line num in coconut source) - self._examples = examples #314 (line num in coconut source) - self._params = params #315 (line num in coconut source) - self._args = args #316 (line num in coconut source) - self._kwargs = kwargs #317 (line num in coconut source) - return self #318 (line num in coconut source) + self = super().__new__(cls) #318 (line num in coconut source) + if self.tell_examples is not None: #319 (line num in coconut source) + self._examples = examples #320 (line num in coconut source) + self._params = params #321 (line num in coconut source) + self._args = args #322 (line num in coconut source) + self._kwargs = kwargs #323 (line num in coconut source) + return self #324 (line num in coconut source) - def __init__(self, examples=None, params=None, *args, **kwargs): #320 (line num in coconut source) - """Just call attempt_update by default.""" #321 (line num in coconut source) - self._examples = [] #322 (line num in coconut source) - result = self.attempt_update(examples, params, *args, **kwargs) #323 (line num in coconut source) - assert result, "Backend.__init__: {_coconut_format_0}.attempt_update(*{_coconut_format_1}, **{_coconut_format_2}) failed with result {_coconut_format_3!r}".format(_coconut_format_0=(self.__class__.__name__), _coconut_format_1=(args), _coconut_format_2=(kwargs), _coconut_format_3=(result)) #324 (line num in coconut source) + def __init__(self, examples=None, params=None, *args, **kwargs): #326 (line num in coconut source) + """Just call attempt_update by default.""" #327 (line num in coconut source) + self._examples = [] #328 (line num in coconut source) + result = self.attempt_update(examples, params, *args, **kwargs) #329 (line num in coconut source) + assert result, "Backend.__init__: {_coconut_format_0}.attempt_update(*{_coconut_format_1}, **{_coconut_format_2}) failed with result {_coconut_format_3!r}".format(_coconut_format_0=(self.__class__.__name__), _coconut_format_1=(args), _coconut_format_2=(kwargs), _coconut_format_3=(result)) #330 (line num in coconut source) - def attempt_update(self, examples=None, params=None, *args, **kwargs): #326 (line num in coconut source) + def attempt_update(self, examples=None, params=None, *args, **kwargs): #332 (line num in coconut source) """Attempt to update this backend with new arguments. False indicates that the - update failed while True indicates a successful update.""" #328 (line num in coconut source) - if (self.tell_examples is None or not self._params or params != self._params or args != self._args or kwargs != self._kwargs): #329 (line num in coconut source) - return False #334 (line num in coconut source) - old_examples, new_examples = examples[:len(self._examples)], examples[len(self._examples):] #335 (line num in coconut source) - if old_examples != self._examples: #336 (line num in coconut source) - return False #337 (line num in coconut source) - if new_examples: #338 (line num in coconut source) - try: #339 (line num in coconut source) - self.tell_examples(new_examples) #340 (line num in coconut source) - except NotImplementedError: #341 (line num in coconut source) - return False #342 (line num in coconut source) - self._examples = examples #343 (line num in coconut source) - return True #344 (line num in coconut source) + update failed while True indicates a successful update.""" #334 (line num in coconut source) + if (self.tell_examples is None or not self._params or params != self._params or args != self._args or kwargs != self._kwargs): #335 (line num in coconut source) + return False #340 (line num in coconut source) + old_examples, new_examples = examples[:len(self._examples)], examples[len(self._examples):] #341 (line num in coconut source) + if old_examples != self._examples: #342 (line num in coconut source) + return False #343 (line num in coconut source) + if new_examples: #344 (line num in coconut source) + try: #345 (line num in coconut source) + self.tell_examples(new_examples) #346 (line num in coconut source) + except NotImplementedError: #347 (line num in coconut source) + return False #348 (line num in coconut source) + self._examples = examples #349 (line num in coconut source) + return True #350 (line num in coconut source) - def init_fallback_backend(self): #346 (line num in coconut source) - """Set fallback_backend to a new random backend instance.""" #347 (line num in coconut source) - self.fallback_backend = backend_registry[constants.default_fallback_backend]() #348 (line num in coconut source) + def init_fallback_backend(self): #352 (line num in coconut source) + """Set fallback_backend to a new random backend instance.""" #353 (line num in coconut source) + self.fallback_backend = backend_registry[constants.default_fallback_backend]() #354 (line num in coconut source) - def fallback_func(self, name, func, *args, **kwargs): #350 (line num in coconut source) - """Default fallback_func calls self.fallback_backend.param.""" #351 (line num in coconut source) - assert self.fallback_backend is not None, "Backend subclasses using Backend.fallback_func must set fallback_backend" #352 (line num in coconut source) - return self.fallback_backend.param(name, func, *args, **kwargs) #353 (line num in coconut source) + def fallback_func(self, name, func, *args, **kwargs): #356 (line num in coconut source) + """Default fallback_func calls self.fallback_backend.param.""" #357 (line num in coconut source) + assert self.fallback_backend is not None, "Backend subclasses using Backend.fallback_func must set fallback_backend" #358 (line num in coconut source) + return self.fallback_backend.param(name, func, *args, **kwargs) #359 (line num in coconut source) - def param(self, name, func, *args, **kwargs): #355 (line num in coconut source) - """Default param calls serve_values with self.current_values and self.fallback_func.""" #356 (line num in coconut source) - assert self.current_values is not None and (isinstance)(self.current_values, dict), "Backend subclasses using Backend.param must set current_values" #357 (line num in coconut source) - return serve_values(name, func, args, kwargs, serving_values=self.current_values, fallback_func=self.fallback_func, backend_name=self.backend_name, implemented_funcs=self.implemented_funcs, supported_kwargs=self.supported_kwargs) #358 (line num in coconut source) + def param(self, name, func, *args, **kwargs): #361 (line num in coconut source) + """Default param calls serve_values with self.current_values and self.fallback_func.""" #362 (line num in coconut source) + assert self.current_values is not None and (isinstance)(self.current_values, dict), "Backend subclasses using Backend.param must set current_values" #363 (line num in coconut source) + return serve_values(name, func, args, kwargs, serving_values=self.current_values, fallback_func=self.fallback_func, backend_name=self.backend_name, implemented_funcs=self.implemented_funcs, supported_kwargs=self.supported_kwargs) #364 (line num in coconut source) - registered_algs = None #370 (line num in coconut source) + registered_algs = None #376 (line num in coconut source) - @classmethod #372 (line num in coconut source) - def register(cls): #373 (line num in coconut source) - """Register this backend to the backend registry.""" #374 (line num in coconut source) - assert cls.backend_name is not None, "Backend subclasses using Backend.register must set backend_name on the class" #375 (line num in coconut source) - backend_registry.register(cls.backend_name, cls) #376 (line num in coconut source) + @classmethod #378 (line num in coconut source) + def register(cls): #379 (line num in coconut source) + """Register this backend to the backend registry.""" #380 (line num in coconut source) + assert cls.backend_name is not None, "Backend subclasses using Backend.register must set backend_name on the class" #381 (line num in coconut source) + backend_registry.register(cls.backend_name, cls) #382 (line num in coconut source) # clear out registered_algs when register is called, since that # probably indicates a subclass is trying to register new algs - cls.registered_algs = [] #380 (line num in coconut source) + cls.registered_algs = [] #386 (line num in coconut source) - @classmethod #382 (line num in coconut source) - def register_alias(cls, alias): #383 (line num in coconut source) - """Register an alias for this backend.""" #384 (line num in coconut source) - assert cls.backend_name is not None, "Backend subclasses using Backend.register_alias must set backend_name on the class" #385 (line num in coconut source) - backend_registry.register_alias(cls.backend_name, alias) #386 (line num in coconut source) + @classmethod #388 (line num in coconut source) + def register_alias(cls, alias): #389 (line num in coconut source) + """Register an alias for this backend.""" #390 (line num in coconut source) + assert cls.backend_name is not None, "Backend subclasses using Backend.register_alias must set backend_name on the class" #391 (line num in coconut source) + backend_registry.register_alias(cls.backend_name, alias) #392 (line num in coconut source) - @classmethod #388 (line num in coconut source) - def register_alg(cls, alg_name, **options): #389 (line num in coconut source) - """Register an algorithm under the given name that calls this backend with the given options.""" #390 (line num in coconut source) - assert cls.backend_name is not None, "Backend subclasses using Backend.register_alg must set backend_name on the class" #391 (line num in coconut source) - alg_registry.register(alg_name, (cls.backend_name, options)) #392 (line num in coconut source) + @classmethod #394 (line num in coconut source) + def register_alg(cls, alg_name, **options): #395 (line num in coconut source) + """Register an algorithm under the given name that calls this backend with the given options.""" #396 (line num in coconut source) + assert cls.backend_name is not None, "Backend subclasses using Backend.register_alg must set backend_name on the class" #397 (line num in coconut source) + alg_registry.register(alg_name, (cls.backend_name, options)) #398 (line num in coconut source) - assert cls.registered_algs is not None, "Backend.register_alg must come after Backend.register" #394 (line num in coconut source) - cls.registered_algs.append(alg_name) #395 (line num in coconut source) + assert cls.registered_algs is not None, "Backend.register_alg must come after Backend.register" #400 (line num in coconut source) + cls.registered_algs.append(alg_name) #401 (line num in coconut source) - @classmethod #397 (line num in coconut source) - def register_meta_for_all_algs(cls, alg_name, meta_alg=constants.default_alg_sentinel): #398 (line num in coconut source) - """Register a meta algorithm for all the algs registered on this class.""" #399 (line num in coconut source) - assert cls.registered_algs is not None, "register_meta_for_all_algs requires prior register_alg calls" #400 (line num in coconut source) - cls.register_meta(alg_name, cls.registered_algs, meta_alg) #401 (line num in coconut source) + @classmethod #403 (line num in coconut source) + def register_meta_for_all_algs(cls, alg_name, meta_alg=constants.default_alg_sentinel): #404 (line num in coconut source) + """Register a meta algorithm for all the algs registered on this class.""" #405 (line num in coconut source) + assert cls.registered_algs is not None, "register_meta_for_all_algs requires prior register_alg calls" #406 (line num in coconut source) + cls.register_meta(alg_name, cls.registered_algs, meta_alg) #407 (line num in coconut source) - @staticmethod #403 (line num in coconut source) - def register_meta(alg_name, algs, meta_alg=constants.default_alg_sentinel): #404 (line num in coconut source) - """Register an algorithm that defers to run_meta.""" #405 (line num in coconut source) - meta_registry.register(alg_name, (algs, meta_alg)) #406 (line num in coconut source) + @staticmethod #409 (line num in coconut source) + def register_meta(alg_name, algs, meta_alg=constants.default_alg_sentinel): #410 (line num in coconut source) + """Register an algorithm that defers to run_meta.""" #411 (line num in coconut source) + meta_registry.register(alg_name, (algs, meta_alg)) #412 (line num in coconut source) - @staticmethod #408 (line num in coconut source) - def register_param_func(func_name, handler, placeholder_generator, support_check_func): #409 (line num in coconut source) - """Register a new parameter definition function. See bbopt.params for examples.""" #410 (line num in coconut source) - param_processor.register(func_name, handler, placeholder_generator, support_check_func) #411 (line num in coconut source) + @staticmethod #414 (line num in coconut source) + def register_param_func(func_name, handler, placeholder_generator, support_check_func): #415 (line num in coconut source) + """Register a new parameter definition function. See bbopt.params for examples.""" #416 (line num in coconut source) + param_processor.register(func_name, handler, placeholder_generator, support_check_func) #417 (line num in coconut source) -_coconut_call_set_names(Backend) #414 (line num in coconut source) -class StandardBackend(Backend): #414 (line num in coconut source) - """Base class for standard BBopt backends.""" #415 (line num in coconut source) +_coconut_call_set_names(Backend) #420 (line num in coconut source) +class StandardBackend(Backend): #420 (line num in coconut source) + """Base class for standard BBopt backends.""" #421 (line num in coconut source) - def __init__(self, examples, params, *args, **kwargs): #417 (line num in coconut source) - """Implement __init__ using setup_backend and tell_examples.""" #418 (line num in coconut source) - self.init_fallback_backend() #419 (line num in coconut source) + def __init__(self, examples, params, *args, **kwargs): #423 (line num in coconut source) + """Implement __init__ using setup_backend and tell_examples.""" #424 (line num in coconut source) + self.init_fallback_backend() #425 (line num in coconut source) - if not params: #421 (line num in coconut source) - self.current_values = {} #422 (line num in coconut source) - return #423 (line num in coconut source) + if not params: #427 (line num in coconut source) + self.current_values = {} #428 (line num in coconut source) + return #429 (line num in coconut source) - self.setup_backend(params, *args, **kwargs) #425 (line num in coconut source) + self.setup_backend(params, *args, **kwargs) #431 (line num in coconut source) - if examples: #427 (line num in coconut source) - self.tell_examples(examples) #428 (line num in coconut source) - else: #429 (line num in coconut source) - self.current_values = {} #430 (line num in coconut source) + if examples: #433 (line num in coconut source) + self.tell_examples(examples) #434 (line num in coconut source) + else: #435 (line num in coconut source) + self.current_values = {} #436 (line num in coconut source) - @override #432 (line num in coconut source) - def tell_examples(self, new_examples): #433 (line num in coconut source) - """Implements tell_examples by calling tell_data.""" #434 (line num in coconut source) - new_data, new_losses = get_named_data_points_and_losses(new_examples, self._params) #435 (line num in coconut source) - self.tell_data(new_data, new_losses) #436 (line num in coconut source) - self.current_values = self.get_next_values() #437 (line num in coconut source) + @override #438 (line num in coconut source) + def tell_examples(self, new_examples): #439 (line num in coconut source) + """Implements tell_examples by calling tell_data.""" #440 (line num in coconut source) + new_data, new_losses = get_named_data_points_and_losses(new_examples, self._params) #441 (line num in coconut source) + self.tell_data(new_data, new_losses) #442 (line num in coconut source) + self.current_values = self.get_next_values() #443 (line num in coconut source) - def setup_backend(self, params, *args, **kwargs): #439 (line num in coconut source) - """Override setup_backend with any setup work that needs to be done.""" #440 (line num in coconut source) - raise NotImplementedError("StandardBackend subclasses using StandardBackend.__init__ must define a setup_backend(params, *args, **kwargs) method") #441 (line num in coconut source) + def setup_backend(self, params, *args, **kwargs): #445 (line num in coconut source) + """Override setup_backend with any setup work that needs to be done.""" #446 (line num in coconut source) + raise NotImplementedError("StandardBackend subclasses using StandardBackend.__init__ must define a setup_backend(params, *args, **kwargs) method") #447 (line num in coconut source) - def tell_data(self, new_data, new_losses): #443 (line num in coconut source) - """Override tell_data with any work that needs to be done to add the given data and losses.""" #444 (line num in coconut source) - raise NotImplementedError("StandardBackend subclasses using StandardBackend.tell_examples must define a tell_data(new_data, new_losses) method") #445 (line num in coconut source) + def tell_data(self, new_data, new_losses): #449 (line num in coconut source) + """Override tell_data with any work that needs to be done to add the given data and losses.""" #450 (line num in coconut source) + raise NotImplementedError("StandardBackend subclasses using StandardBackend.tell_examples must define a tell_data(new_data, new_losses) method") #451 (line num in coconut source) - def get_next_values(self): #447 (line num in coconut source) - """Override get_next_values to produce the next set of values that should be evaluated.""" #448 (line num in coconut source) - raise NotImplementedError("StandardBackend subclasses using StandardBackend.tell_examples must define a get_next_values() method") #449 (line num in coconut source) + def get_next_values(self): #453 (line num in coconut source) + """Override get_next_values to produce the next set of values that should be evaluated.""" #454 (line num in coconut source) + raise NotImplementedError("StandardBackend subclasses using StandardBackend.tell_examples must define a get_next_values() method") #455 (line num in coconut source) -_coconut_call_set_names(StandardBackend) #451 (line num in coconut source) +_coconut_call_set_names(StandardBackend) #457 (line num in coconut source) diff --git a/bbopt/benchmarking.py b/bbopt/benchmarking.py index cad4b19..2ba7e03 100644 --- a/bbopt/benchmarking.py +++ b/bbopt/benchmarking.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x64d1a213 +# __coconut_hash__ = 0x2666b168 # Compiled with Coconut version 2.0.0-a_dev53 [How Not to Be Seen] @@ -80,7 +80,7 @@ def numpy_func(bb): #35 (line num in coconut source) def sample_func(bb): #44 (line num in coconut source) - xs = bb.sample("xs", range(10), 5, guess=[3, 4, 5, 6, 7]) #45 (line num in coconut source) + xs = bb.unshuffled_sample("xs", range(10), 5, guess=[3, 4, 5, 6, 7]) #45 (line num in coconut source) y = bb.choice("y", [1, 10, 100], guess=10) #46 (line num in coconut source) loss = abs(sum(xs) - y) #47 (line num in coconut source) bb.minimize(loss) #48 (line num in coconut source) @@ -155,4 +155,4 @@ def benchmark(algs, plot_func="plot_convergence", n=10): #92 (line num in cocon if __name__ == "__main__": #113 (line num in coconut source) - benchmark(("tpe_or_gp", "tree_structured_parzen_estimator", "safe_gaussian_process", ("openai", "safe_gaussian_process"))) #114 (line num in coconut source) + benchmark(("tpe_or_gp", "tree_structured_parzen_estimator", "safe_gaussian_process", ("openai_debug", "safe_gaussian_process"))) #114 (line num in coconut source) diff --git a/bbopt/constants.py b/bbopt/constants.py index 4e92e4c..ffc3544 100644 --- a/bbopt/constants.py +++ b/bbopt/constants.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x9dd11956 +# __coconut_hash__ = 0xec6da173 # Compiled with Coconut version 2.0.0-a_dev53 [How Not to Be Seen] @@ -42,7 +42,7 @@ # Installation constants: name = "bbopt" #7 (line num in coconut source) -version = "1.4.1" #8 (line num in coconut source) +version = "1.4.2" #8 (line num in coconut source) description = "The easiest hyperparameter optimization you'll ever do." #9 (line num in coconut source) long_description = """ See BBopt's GitHub_ for more information. diff --git a/bbopt/optimizer.py b/bbopt/optimizer.py index 3221dc6..a9834c7 100644 --- a/bbopt/optimizer.py +++ b/bbopt/optimizer.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x9446acdc +# __coconut_hash__ = 0x639ac126 # Compiled with Coconut version 2.0.0-a_dev53 [How Not to Be Seen] @@ -764,59 +764,81 @@ def randbool(self, name, **kwargs): #539 (line num in coconut source) def sample(self, name, population, k, **kwargs): #543 (line num in coconut source) - """Create a new parameter with the given name modeled by random.sample(population, k).""" #544 (line num in coconut source) - if not isinstance(name, Str): #545 (line num in coconut source) - raise TypeError("name must be string, not {_coconut_format_0}".format(_coconut_format_0=(name))) #546 (line num in coconut source) - sampling_population = [x for x in population] #547 (line num in coconut source) - sample = [] #548 (line num in coconut source) - for i in range(k): #549 (line num in coconut source) - if len(sampling_population) <= 1: #550 (line num in coconut source) - sample.append(sampling_population[0]) #551 (line num in coconut source) - else: #552 (line num in coconut source) - def _coconut_lambda_1(val): #553 (line num in coconut source) - elem = _coconut_iter_getitem(val, i) #553 (line num in coconut source) - return sampling_population.index(elem) if elem in sampling_population else 0 #553 (line num in coconut source) - proc_kwargs = (param_processor.modify_kwargs)(_coconut_lambda_1, kwargs) #553 (line num in coconut source) - ind = self.randrange("{_coconut_format_0}[{_coconut_format_1}]".format(_coconut_format_0=(name), _coconut_format_1=(i)), len(sampling_population), **proc_kwargs) #558 (line num in coconut source) - sample.append(sampling_population.pop(ind)) #559 (line num in coconut source) - return sample #560 (line num in coconut source) - - - def samples_with_replacement(self, name, population, **kwargs): #562 (line num in coconut source) - """An infinite iterator of samples with replacement from population.""" #563 (line num in coconut source) - if not isinstance(name, Str): #564 (line num in coconut source) - raise TypeError("name must be string, not {_coconut_format_0}".format(_coconut_format_0=(name))) #565 (line num in coconut source) - sampling_population = tuple(population) #566 (line num in coconut source) - for i in count(): #567 (line num in coconut source) - yield self.choice("{_coconut_format_0}[{_coconut_format_1}]".format(_coconut_format_0=(name), _coconut_format_1=(i)), sampling_population, **kwargs) #568 (line num in coconut source) - - - def shuffled(self, name, population, **kwargs): #570 (line num in coconut source) + """Create a new parameter with the given name modeled by random.sample(population, k). + Ordering of elements in the result is random.""" #545 (line num in coconut source) + if not isinstance(name, Str): #546 (line num in coconut source) + raise TypeError("name must be string, not {_coconut_format_0}".format(_coconut_format_0=(name))) #547 (line num in coconut source) + sampling_population = [x for x in population] #548 (line num in coconut source) + sample = [] #549 (line num in coconut source) + for i in range(k): #550 (line num in coconut source) + if len(sampling_population) <= 1: #551 (line num in coconut source) + sample.append(sampling_population[0]) #552 (line num in coconut source) + else: #553 (line num in coconut source) + def _coconut_lambda_1(val): #554 (line num in coconut source) + elem = _coconut_iter_getitem(val, i) #554 (line num in coconut source) + return sampling_population.index(elem) if elem in sampling_population else 0 #554 (line num in coconut source) + proc_kwargs = (param_processor.modify_kwargs)(_coconut_lambda_1, kwargs) #554 (line num in coconut source) + ind = self.randrange("{_coconut_format_0}[{_coconut_format_1}]".format(_coconut_format_0=(name), _coconut_format_1=(i)), len(sampling_population), **proc_kwargs) #559 (line num in coconut source) + sample.append(sampling_population.pop(ind)) #560 (line num in coconut source) + return sample #561 (line num in coconut source) + + + def unshuffled_sample(self, name, population, k, **kwargs): #563 (line num in coconut source) + """Create a new parameter with the given name modeled by random.sample(population, k). + Ordering of elements in the result is the same as in population.""" #565 (line num in coconut source) + if not isinstance(name, Str): #566 (line num in coconut source) + raise TypeError("name must be string, not {_coconut_format_0}".format(_coconut_format_0=(name))) #567 (line num in coconut source) + population = tuple(population) #568 (line num in coconut source) + sample = [] #569 (line num in coconut source) + for i, x in enumerate(population): #570 (line num in coconut source) + if len(sample) == k: #571 (line num in coconut source) + break #572 (line num in coconut source) + if len(population) - i == k - len(sample): #573 (line num in coconut source) + sample += population[i:] #574 (line num in coconut source) + break #575 (line num in coconut source) + proc_kwargs = (param_processor.modify_kwargs)(lambda val: 1 if x in val else 0, kwargs) #576 (line num in coconut source) + if "placeholder_when_missing" not in proc_kwargs: #579 (line num in coconut source) + proc_kwargs["placeholder_when_missing"] = 0 #580 (line num in coconut source) + if self.uniform("{_coconut_format_0}[{_coconut_format_1}]".format(_coconut_format_0=(name), _coconut_format_1=(i)), 0, 1, **proc_kwargs) >= 1 - (k - len(sample)) / (len(population) - i): #581 (line num in coconut source) + sample.append(x) #587 (line num in coconut source) + return sample #588 (line num in coconut source) + + + def samples_with_replacement(self, name, population, **kwargs): #590 (line num in coconut source) + """An infinite iterator of samples with replacement from population.""" #591 (line num in coconut source) + if not isinstance(name, Str): #592 (line num in coconut source) + raise TypeError("name must be string, not {_coconut_format_0}".format(_coconut_format_0=(name))) #593 (line num in coconut source) + population = tuple(population) #594 (line num in coconut source) + for i in count(): #595 (line num in coconut source) + yield self.choice("{_coconut_format_0}[{_coconut_format_1}]".format(_coconut_format_0=(name), _coconut_format_1=(i)), population, **kwargs) #596 (line num in coconut source) + + + def shuffled(self, name, population, **kwargs): #598 (line num in coconut source) """Create a new parameter with the given name modeled by - random.shuffle(population) except returned instead of modified in place.""" #572 (line num in coconut source) - return self.sample(name, population, len(population), **kwargs) #573 (line num in coconut source) + random.shuffle(population) except returned instead of modified in place.""" #600 (line num in coconut source) + return self.sample(name, population, len(population), **kwargs) #601 (line num in coconut source) - def shuffle(self, name, population, **kwargs): #575 (line num in coconut source) - """Create a new parameter with the given name modeled by random.shuffle(population).""" #576 (line num in coconut source) - population[:] = self.shuffled(name, population, **kwargs) #577 (line num in coconut source) + def shuffle(self, name, population, **kwargs): #603 (line num in coconut source) + """Create a new parameter with the given name modeled by random.shuffle(population).""" #604 (line num in coconut source) + population[:] = self.shuffled(name, population, **kwargs) #605 (line num in coconut source) - def stdnormal(self, name, **kwargs): #579 (line num in coconut source) - """Equivalent to bb.normalvariate(name, 0, 1).""" #580 (line num in coconut source) - return self.normalvariate(name, 0, 1, **kwargs) #581 (line num in coconut source) + def stdnormal(self, name, **kwargs): #607 (line num in coconut source) + """Equivalent to bb.normalvariate(name, 0, 1).""" #608 (line num in coconut source) + return self.normalvariate(name, 0, 1, **kwargs) #609 (line num in coconut source) # Array-based random functions: - def rand(self, name, *shape, **kwargs): #585 (line num in coconut source) - """Create a new array parameter for the given name and shape modeled by np.random.rand.""" #586 (line num in coconut source) - return array_param(self.random, name, shape, kwargs) #587 (line num in coconut source) + def rand(self, name, *shape, **kwargs): #613 (line num in coconut source) + """Create a new array parameter for the given name and shape modeled by np.random.rand.""" #614 (line num in coconut source) + return array_param(self.random, name, shape, kwargs) #615 (line num in coconut source) - def randn(self, name, *shape, **kwargs): #589 (line num in coconut source) - """Create a new array parameter for the given name and shape modeled by np.random.randn.""" #590 (line num in coconut source) - return array_param(self.stdnormal, name, shape, kwargs) #591 (line num in coconut source) + def randn(self, name, *shape, **kwargs): #617 (line num in coconut source) + """Create a new array parameter for the given name and shape modeled by np.random.randn.""" #618 (line num in coconut source) + return array_param(self.stdnormal, name, shape, kwargs) #619 (line num in coconut source) -_coconut_call_set_names(BlackBoxOptimizer) #593 (line num in coconut source) +_coconut_call_set_names(BlackBoxOptimizer) #621 (line num in coconut source)