Created
July 10, 2020 10:12
-
-
Save davideanastasia/f05aa8de4f70eac4b3b274f22d1055d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import mlflow | |
from hyperopt import fmin, hp, tpe, rand | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import precision_recall_fscore_support | |
from sklearn.model_selection import train_test_split, cross_val_score | |
from sklearn.pipeline import make_pipeline | |
from this_project.censusdata import fetch_censusdata, make_linear_preprocessor | |
@click.command(help="Perform hyperparameter search with Hyperopt library.") | |
@click.option( | |
"--max-evals", | |
type=click.INT, | |
default=10, | |
help="Maximum number of runs to evaluate.", | |
) | |
def trainer(max_evals): | |
def build_eval_fn(X, y): | |
def eval_fn(params): | |
with mlflow.start_run(nested=True) as _: | |
# unpack parameters | |
(C,) = params | |
mlflow.set_tags({"training_type": "hyperopt"}) | |
mlflow.log_params({"C": C}) | |
# | |
# 1. read data | |
# 2. perform training | |
# 3. measure test data performance | |
# | |
mlflow.log_metrics( | |
{"precision": precision, "recall": recall, "fscore": fscore} | |
) | |
# return score | |
return -recall | |
return eval_fn | |
X, y = fetch_censusdata() | |
print(X.shape, y.shape) | |
space = [hp.quniform("C", 1.0, 100.0, 0.5)] | |
with mlflow.start_run() as _: | |
mlflow.log_param("max_evals", max_evals) | |
mlflow.set_tags({"training_type": "hyperopt"}) | |
best = fmin( | |
fn=build_eval_fn(X, y), space=space, algo=tpe.suggest, max_evals=max_evals | |
) | |
print(best) | |
if __name__ == "__main__": | |
trainer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment