From fd425c27c9a6b5b49fbfa4f81a94add6103cccb8 Mon Sep 17 00:00:00 2001 From: "Timur M. Carstensen" Date: Mon, 21 Oct 2024 15:50:04 +0200 Subject: [PATCH 1/4] chore: removing redundant code --- neps/__init__.py | 3 -- neps/search_spaces/__init__.py | 3 -- neps/search_spaces/architecture/api.py | 51 +------------------ neps_examples/basic_usage/hyperparameters.py | 2 +- ...rs_for_architecture_and_hyperparameters.py | 2 +- .../experimental/hierarchical_architecture.py | 2 +- 6 files changed, 5 insertions(+), 58 deletions(-) diff --git a/neps/__init__.py b/neps/__init__.py index 4e59493f..260b1a9c 100644 --- a/neps/__init__.py +++ b/neps/__init__.py @@ -3,15 +3,12 @@ from neps.plot.tensorboard_eval import tblogger from neps.search_spaces import ( Architecture, - ArchitectureParameter, Categorical, CategoricalParameter, Constant, ConstantParameter, Float, FloatParameter, - Function, - FunctionParameter, GraphGrammar, Integer, IntegerParameter, diff --git a/neps/search_spaces/__init__.py b/neps/search_spaces/__init__.py index b726b8ae..461efbb9 100644 --- a/neps/search_spaces/__init__.py +++ b/neps/search_spaces/__init__.py @@ -1,8 +1,5 @@ from neps.search_spaces.architecture.api import ( Architecture, - ArchitectureParameter, - Function, - FunctionParameter, ) from neps.search_spaces.architecture.graph_grammar import ( CoreGraphGrammar, diff --git a/neps/search_spaces/architecture/api.py b/neps/search_spaces/architecture/api.py index 9521bd7a..6315f2ca 100644 --- a/neps/search_spaces/architecture/api.py +++ b/neps/search_spaces/architecture/api.py @@ -61,14 +61,8 @@ def Architecture(**kwargs): class _FunctionParameter(base): def __init__( self, - structure: Grammar - | list[Grammar] - | ConstrainedGrammar - | list[ConstrainedGrammar] - | str - | list[str] - | dict - | list[dict], + structure: Grammar | ConstrainedGrammar | str | dict + | list[Grammar | ConstrainedGrammar | str | dict], primitives: dict, # TODO: Follow this rabbit hole for `constraint_kwargs`, # it can all be deleted my friend @@ -162,44 +156,3 @@ def create_new_instance_from_id(self, identifier: str): return _FunctionParameter(**kwargs) -def ArchitectureParameter(**kwargs): - """Deprecated: Use `Architecture` instead of `ArchitectureParameter`. - - This function remains for backward compatibility and will raise a deprecation - warning if used. - """ - import warnings - - warnings.warn( - ( - "Usage of 'neps.ArchitectureParameter' is deprecated and will be removed in" - " future releases. Please use 'neps.Architecture' instead." - ), - DeprecationWarning, - stacklevel=2, - ) - - return Architecture(**kwargs) - - -Function = Architecture - - -def FunctionParameter(**kwargs): - """Deprecated: Use `Function` instead of `FunctionParameter`. - - This function remains for backward compatibility and will raise a deprecation - warning if used. - """ - import warnings - - warnings.warn( - ( - "Usage of 'neps.FunctionParameter' is deprecated and will be removed in" - " future releases. Please use 'neps.Function' instead." - ), - DeprecationWarning, - stacklevel=2, - ) - - return Function(**kwargs) diff --git a/neps_examples/basic_usage/hyperparameters.py b/neps_examples/basic_usage/hyperparameters.py index 724974ae..990ed2fc 100644 --- a/neps_examples/basic_usage/hyperparameters.py +++ b/neps_examples/basic_usage/hyperparameters.py @@ -21,7 +21,7 @@ def run_pipeline(float1, float2, categorical, integer1, integer2): ) logging.basicConfig(level=logging.INFO) -neps.run( +neps.ru0335n( run_pipeline=run_pipeline, pipeline_space=pipeline_space, root_directory="results/hyperparameters_example", diff --git a/neps_examples/experimental/expert_priors_for_architecture_and_hyperparameters.py b/neps_examples/experimental/expert_priors_for_architecture_and_hyperparameters.py index c0d1f5f6..d0c1622a 100644 --- a/neps_examples/experimental/expert_priors_for_architecture_and_hyperparameters.py +++ b/neps_examples/experimental/expert_priors_for_architecture_and_hyperparameters.py @@ -106,7 +106,7 @@ def run_pipeline(some_architecture, some_float, some_integer, some_cat): pipeline_space = dict( - some_architecture=neps.Function( + some_architecture=neps.Architecture( set_recursive_attribute=set_recursive_attribute, structure=structure, primitives=primitives, diff --git a/neps_examples/experimental/hierarchical_architecture.py b/neps_examples/experimental/hierarchical_architecture.py index 440b116a..d41396b2 100644 --- a/neps_examples/experimental/hierarchical_architecture.py +++ b/neps_examples/experimental/hierarchical_architecture.py @@ -86,7 +86,7 @@ def run_pipeline(architecture: neps.Function): pipeline_space = dict( - architecture=neps.Function( + architecture=neps.Architecture( set_recursive_attribute=set_recursive_attribute, structure=structure, primitives=primitives, From cc9cb042d63870e0aaab16f1e87116ff62f70cf5 Mon Sep 17 00:00:00 2001 From: "Timur M. Carstensen" Date: Mon, 21 Oct 2024 16:02:52 +0200 Subject: [PATCH 2/4] fix: typo --- neps_examples/basic_usage/hyperparameters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neps_examples/basic_usage/hyperparameters.py b/neps_examples/basic_usage/hyperparameters.py index 990ed2fc..724974ae 100644 --- a/neps_examples/basic_usage/hyperparameters.py +++ b/neps_examples/basic_usage/hyperparameters.py @@ -21,7 +21,7 @@ def run_pipeline(float1, float2, categorical, integer1, integer2): ) logging.basicConfig(level=logging.INFO) -neps.ru0335n( +neps.run( run_pipeline=run_pipeline, pipeline_space=pipeline_space, root_directory="results/hyperparameters_example", From 876c3cb9f068ae668b3a8c080f55412140454622 Mon Sep 17 00:00:00 2001 From: "Timur M. Carstensen" Date: Fri, 25 Oct 2024 15:29:49 +0200 Subject: [PATCH 3/4] resolving merge conflicts --- neps/search_spaces/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neps/search_spaces/__init__.py b/neps/search_spaces/__init__.py index 461efbb9..c2275b0e 100644 --- a/neps/search_spaces/__init__.py +++ b/neps/search_spaces/__init__.py @@ -23,7 +23,6 @@ __all__ = [ "Architecture", - "ArchitectureParameter", "Categorical", "CategoricalParameter", "Constant", From d4bef357f03e0225b269c99ab2fe7f40cb1dd456 Mon Sep 17 00:00:00 2001 From: "Timur M. Carstensen" Date: Fri, 25 Oct 2024 15:38:58 +0200 Subject: [PATCH 4/4] chore: removing class function wrapper --- neps/__init__.py | 1 - neps/search_spaces/architecture/api.py | 192 ++++++++++++------------- 2 files changed, 91 insertions(+), 102 deletions(-) diff --git a/neps/__init__.py b/neps/__init__.py index 260b1a9c..9f37d131 100644 --- a/neps/__init__.py +++ b/neps/__init__.py @@ -22,7 +22,6 @@ "Categorical", "Constant", "Function", - "ArchitectureParameter", "CategoricalParameter", "ConstantParameter", "FloatParameter", diff --git a/neps/search_spaces/architecture/api.py b/neps/search_spaces/architecture/api.py index 6315f2ca..e3461a37 100644 --- a/neps/search_spaces/architecture/api.py +++ b/neps/search_spaces/architecture/api.py @@ -51,108 +51,98 @@ def _build(graph, set_recursive_attribute): graph.edges[e].update(set_recursive_attribute(op_name, predecessor_values)) -def Architecture(**kwargs): - """Factory function.""" - if "structure" not in kwargs: - raise ValueError("Factory function requires structure") - if not isinstance(kwargs["structure"], list) or len(kwargs["structure"]) == 1: - base = GraphGrammar - - class _FunctionParameter(base): - def __init__( - self, - structure: Grammar | ConstrainedGrammar | str | dict - | list[Grammar | ConstrainedGrammar | str | dict], - primitives: dict, - # TODO: Follow this rabbit hole for `constraint_kwargs`, - # it can all be deleted my friend - constraint_kwargs: dict | None = None, - name: str = "ArchitectureParameter", - set_recursive_attribute: Callable | None = None, - **kwargs, - ): - local_vars = locals() - self.input_kwargs = { - args: local_vars[args] - for args in inspect.getfullargspec(self.__init__).args # type: ignore[misc] - if args != "self" - } - self.input_kwargs.update(**kwargs) - - if isinstance(structure, list): - structures = [ - _dict_structure_to_str( - st, - primitives, - repetitive_mapping=kwargs.get( - "terminal_to_sublanguage_map", None - ), - ) - if isinstance(st, dict) - else st - for st in structure - ] - _structures = [] - for st in structures: - if isinstance(st, str): - if constraint_kwargs is None: - _st = Grammar.fromstring(st) - else: - _st = ConstrainedGrammar.fromstring(st) - _st.set_constraints(**constraint_kwargs) - _structures.append(_st) # type: ignore[has-type] - structures = _structures - - super().__init__( - grammars=structures, - terminal_to_op_names=primitives, - edge_attr=False, - **kwargs, +class Architecture(GraphGrammar): + def __init__( + self, + structure: Grammar | ConstrainedGrammar | str | dict + | list[Grammar | ConstrainedGrammar | str | dict], + primitives: dict, + # TODO: Follow this rabbit hole for `constraint_kwargs`, + # it can all be deleted my friend + constraint_kwargs: dict | None = None, + name: str = "Architecture", + set_recursive_attribute: Callable | None = None, + **kwargs, + ): + local_vars = locals() + self.input_kwargs = { + args: local_vars[args] + for args in inspect.getfullargspec(self.__init__).args # type: ignore[misc] + if args != "self" + } + self.input_kwargs.update(**kwargs) + + if isinstance(structure, list): + structures = [ + _dict_structure_to_str( + st, + primitives, + repetitive_mapping=kwargs.get( + "terminal_to_sublanguage_map", None + ), ) - else: - if isinstance(structure, dict): - structure = _dict_structure_to_str(structure, primitives) - - if isinstance(structure, str): + if isinstance(st, dict) + else st + for st in structure + ] + _structures = [] + for st in structures: + if isinstance(st, str): if constraint_kwargs is None: - structure = Grammar.fromstring(structure) + _st = Grammar.fromstring(st) else: - structure = ConstrainedGrammar.fromstring(structure) - structure.set_constraints(**constraint_kwargs) # type: ignore[union-attr] - - super().__init__( - grammar=structure, # type: ignore[arg-type] - terminal_to_op_names=primitives, - edge_attr=False, - **kwargs, - ) - - self._set_recursive_attribute = set_recursive_attribute - self.name: str = name - - def to_pytorch(self) -> nn.Module: - self.clear_graph() - if len(self.nodes()) == 0: - composed_function = self.compose_functions() - # part below is required since PyTorch has no standard functional API - self.graph_to_self(composed_function) - self.prune_graph() - - if self._set_recursive_attribute: - m = _build(self, self._set_recursive_attribute) - - if m is not None: - return m - - self.compile() - self.update_op_names() - return super().to_pytorch() # create PyTorch model - - def create_new_instance_from_id(self, identifier: str): - g = Architecture(**self.input_kwargs) # type: ignore[arg-type] - g.load_from(identifier) - return g - - return _FunctionParameter(**kwargs) - + _st = ConstrainedGrammar.fromstring(st) + _st.set_constraints(**constraint_kwargs) + _structures.append(_st) # type: ignore[has-type] + structures = _structures + + super().__init__( + grammars=structures, + terminal_to_op_names=primitives, + edge_attr=False, + **kwargs, + ) + else: + if isinstance(structure, dict): + structure = _dict_structure_to_str(structure, primitives) + + if isinstance(structure, str): + if constraint_kwargs is None: + structure = Grammar.fromstring(structure) + else: + structure = ConstrainedGrammar.fromstring(structure) + structure.set_constraints(**constraint_kwargs) # type: ignore[union-attr] + + super().__init__( + grammar=structure, # type: ignore[arg-type] + terminal_to_op_names=primitives, + edge_attr=False, + **kwargs, + ) + + self._set_recursive_attribute = set_recursive_attribute + self.name: str = name + + def to_pytorch(self) -> nn.Module: + self.clear_graph() + if len(self.nodes()) == 0: + composed_function = self.compose_functions() + # part below is required since PyTorch has no standard functional API + self.graph_to_self(composed_function) + self.prune_graph() + + if self._set_recursive_attribute: + m = _build(self, self._set_recursive_attribute) + + if m is not None: + return m + + self.compile() + self.update_op_names() + return super().to_pytorch() # create PyTorch model + + def create_new_instance_from_id(self, identifier: str): + g = Architecture(**self.input_kwargs) # type: ignore[arg-type] + g.load_from(identifier) + return g