diff --git a/examples/solver_tutorial.py b/examples/solver_tutorial.py new file mode 100644 index 0000000..1650b68 --- /dev/null +++ b/examples/solver_tutorial.py @@ -0,0 +1,110 @@ +# coding: utf-8 +import os +import time +import tempfile +import hyppopy as hp + + +# ### Get Some Data + +from sklearn.datasets import load_iris +iris_data = load_iris() +input_data = [iris_data.data, iris_data.target] + + +# ### Setup ProjectManager +# +# We could read the configuration from .json or .xml using ProjectManager.read_config, but parameters can can also be set via a dictionary. +# All subsections in the section hyperparameter represent a hyperparameter. Top-level of each hyperparameter is the name. Additionally one +# needs to specifiy a domain [uniform, categorical, loguniform, normal], a type [str, int, float] and the data, wich is either a range [from, to] +# or a list of categories. All key value pairs in the section settings are added to the ProjectManager as member variables. If you add the +# section custom you can add your own workflow specific parameter. The ProjectManager is a Singleton, thus need no instanciation and can be used +# everywhere but exists only once! + +config = { +"hyperparameter": { + "C": { + "domain": "uniform", + "data": [0, 20], + "type": "float" + }, + "gamma": { + "domain": "uniform", + "data": [0.0001, 20.0], + "type": "float" + }, + "kernel": { + "domain": "categorical", + "data": ["linear", "sigmoid", "poly", "rbf"], + "type": "str" + }, + "decision_function_shape": { + "domain": "categorical", + "data": ["ovo", "ovr"], + "type": "str" + } +}, +"settings": { + "solver_plugin": { + "max_iterations": 300, + "use_plugin" : "hyperopt", + "output_dir": os.path.join(tempfile.gettempdir(), 'results') + }, + "custom": { + "the_answer": 42 + } +}} + +if hp.ProjectManager.set_config(config): + print("Valid config dict set!") +else: + print("Invalid config dict!") + +print("--------------------------------------------------------------") +print("max_iterations:\t{}".format(hp.ProjectManager.max_iterations)) +print("use_plugin:\t{}".format(hp.ProjectManager.use_plugin)) +print("output_dir:\t{}".format(hp.ProjectManager.output_dir)) +print("the_answer:\t{}".format(hp.ProjectManager.the_answer)) + + +# ### Define the problem +# +# We define a blackbox function with the signature func(data, params). The first parameter data is whatever we tell the solver later. +# So we are free in defining the type of data we want to give our blackbox function. However, the parameter params is fixed and of type +# dict. Each iteration, the solver will create a sample of each of the hyperparameter defined via the config set and throw it into our +# blackbox function. +# E.g. in our case above params in one round could look like {"C": 0.3, "gamma": 2.8, "kernel": "poly", "decision_function_shape", "ovo"}. + +from sklearn.svm import SVC +from sklearn.model_selection import cross_val_score + +def my_blackbox_function(data, params): + clf = SVC(**params) + return -cross_val_score(estimator=clf, X=data[0], y=data[1], cv=3).mean() + + +# ### Feeding the Solver +# +# Now everything is prepared to set up the solver. First we request the solver from the SolverFactory which assembled a solver from the +# plugin parts we specified via the use_plugin parameter. Then we only need to set the our blackbox function and the data. + +solver = hp.SolverFactory.get_solver() +solver.set_loss_function(my_blackbox_function) +solver.set_data(input_data) + + +# ### Start the Solver and get the results + +print("\nStart optimization...") +start = time.process_time() +solver.run() +end = time.process_time() +print("Finished optimization!\n") +print("Total Time: {}s\n".format(end-start)) +res, best = solver.get_results() +print("---- Optimal Parameter -----\n") +for p in best.items(): + print(" - {} : {}".format(p[0], p[1])) + +solver.save_results() + diff --git a/examples/use_hyppopy_solver.py b/examples/use_hyppopy_solver.py deleted file mode 100644 index 93965da..0000000 --- a/examples/use_hyppopy_solver.py +++ /dev/null @@ -1,47 +0,0 @@ -import os -import sys -from sklearn.svm import SVC -from sklearn.model_selection import cross_val_score - -# the ProjectManager is loading your config file and giving you access -# to everything specified in the settings/custom section of the config -from hyppopy.projectmanager import ProjectManager - -# the SolverFactory builds the Solver class for you -from hyppopy.solverfactory import SolverFactory - -# we use in this example the SimpleDataLoader -from hyppopy.workflows.dataloader.simpleloader import SimpleDataLoader - -# until Hyppopy is not fully installable we need -# to set the Hyppopy package folder by hand -HYPPOPY_DIR = "D:/MyPythonModules/hyppopy" -sys.path.append(HYPPOPY_DIR) - -# let the ProjectManager read your config file -DATA = os.path.join(HYPPOPY_DIR, *("hyppopy", "tests", "data", "Titanic")) -ProjectManager.read_config(os.path.join(DATA, 'rf_config.json')) - -# ----- reading data somehow ------ -dl = SimpleDataLoader() -dl.start(path=ProjectManager.data_path, - data_name=ProjectManager.data_name, - labels_name=ProjectManager.labels_name) -# --------------------------------- - -# ----- defining loss function ------ -def blackbox_function(params): - clf = SVC(**params) - return -cross_val_score(estimator=clf, X=dl.data[0], y=dl.data[1], cv=3).mean() -# ----------------------------------- - -# ----- create and run the solver ------ -# get a solver instance from the SolverFactory -solver = SolverFactory.get_solver() -# set your loss function -solver.set_loss_function(blackbox_function) -# run the solver -solver.run() -# store your results -solver.save_results(savedir="C:\\Users\\Me\\Desktop\\myTestProject") -# -------------------------------------- \ No newline at end of file diff --git a/hyppopy/__init__.py b/hyppopy/__init__.py index 5f03f22..86a11c3 100644 --- a/hyppopy/__init__.py +++ b/hyppopy/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.1.1dev' +__version__ = '0.1.2dev' from hyppopy.solverfactory import SolverFactory from hyppopy.projectmanager import ProjectManager \ No newline at end of file diff --git a/planning/Hyppopy.vpp b/planning/Hyppopy.vpp index 0890b31..1602521 100644 Binary files a/planning/Hyppopy.vpp and b/planning/Hyppopy.vpp differ diff --git a/setup.py b/setup.py index a05339e..59b01a5 100644 --- a/setup.py +++ b/setup.py @@ -1,61 +1,61 @@ # -*- coding: utf-8 -*- import os from setuptools import setup, find_packages with open('README.rst') as f: readme = f.read() with open('LICENSE') as f: license = f.read() -VERSION = "0.1.1" +VERSION = "0.1.2dev" ROOT = os.path.dirname(os.path.realpath(__file__)) new_init = [] with open(os.path.join(ROOT, *("hyppopy", "__init__.py")), "r") as infile: for line in infile: new_init.append(line) for n in range(len(new_init)): if new_init[n].startswith("__version__"): split = line.split("=") new_init[n] = "__version__ = '" + VERSION + "'\n" with open(os.path.join(ROOT, *("hyppopy", "__init__.py")), "w") as outfile: outfile.writelines(new_init) setup( name='hyppopy', version=VERSION, description='Hyper-Parameter Optimization Toolbox for Blackboxfunction Optimization', long_description=readme, # if you want, put your own name here # (this would likely result in people sending you emails) author='Sven Wanner', author_email='s.wanner@dkfz.de', url='', license=license, packages=find_packages(exclude=('*test*', 'doc')), package_data={ 'hyppopy.plugins': ['*.yapsy-plugin'] }, # the requirements to install this project. # Since this one is so simple this is empty. install_requires=[ 'dicttoxml>=1.7.4', 'xmltodict>=0.11.0', 'hyperopt>=0.1.1', 'Optunity>=1.1.1', 'numpy>=1.16.0', 'matplotlib>=3.0.2', 'scikit-learn>=0.20.2', 'scipy>=1.2.0', 'Sphinx>=1.8.3', 'xmlrunner>=1.7.7', 'Yapsy>=1.11.223', 'pandas>=0.24.1', 'seaborn>=0.9.0' ], )