-
Notifications
You must be signed in to change notification settings - Fork 17
/
mlflow_project_driver.py
56 lines (45 loc) · 1.53 KB
/
mlflow_project_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import itertools
import os
import subprocess
import sys
import warnings
import mlflow
warnings.filterwarnings('ignore')
PROJECT_DIR = sys.path[0]
os.chdir(PROJECT_DIR)
experiment_name = 'rented_bikes'
mlflow.set_experiment(experiment_name)
# delete default experiment if exits
if mlflow.get_experiment_by_name("Default").lifecycle_stage == 'active':
mlflow.delete_experiment("0")
subprocess.run("mlflow gc", shell=True, check=False, stdout=subprocess.DEVNULL)
# Model Hyper-parameters
parameters = {
"learning_rate": [0.1, 0.05, 0.01],
"max_depth": [4, 5, 6],
}
# Tuning the hyper-parameters via grid search
# generate parameters combinations
params_keys = parameters.keys()
params_values = [
parameters[key] if isinstance(parameters[key], list) else [parameters[key]]
for key in params_keys
]
runs_parameters = [
dict(zip(params_keys, combination)) for combination in itertools.product(*params_values)
]
# execute experiment runs in parallel in docker containers
submitted_runs = []
for run_parameters in runs_parameters:
submitted_runs.append(mlflow.projects.run(
uri='.',
backend='local',
parameters=run_parameters,
synchronous=False,
docker_args={"user": f"{os.getuid()}:{os.getgid()}"},
))
print(f"Submitted {len(submitted_runs)} runs. Waiting for them to finish...")
# get runs status (blocking)
runs_status = [run.wait() for run in submitted_runs]
print(f"Experiment '{experiment_name}' finished!")
print(f"{sum(runs_status)} runs succeeded out of {len(runs_status)} submitted")