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
57
58
59
60
61
62
63
64
65
66
67
68
|
- import os
- import tempfile
- import time
- import mlflow
- from ray import air, tune
- from ray.air import session
- from ray.air.integrations.mlflow import MLflowLoggerCallback, setup_mlflow
- def evaluation_fn(step, width, height):
- return (0.1 + width * step / 100) ** (-1) + height * 0.1
- def train_function(config):
- width, height = config["width"], config["height"]
- for step in range(config.get("steps", 100)):
- # Iterative training function - can be any arbitrary training procedure
- intermediate_score = evaluation_fn(step, width, height)
- # Feed the score back to Tune.
- session.report({"iterations": step, "mean_loss": intermediate_score})
- time.sleep(0.1)
- def train_function_mlflow(config):
- setup_mlflow(config)
-
- # Hyperparameters
- width, height = config["width"], config["height"]
- for step in range(config.get("steps", 100)):
- # Iterative training function - can be any arbitrary training procedure
- intermediate_score = evaluation_fn(step, width, height)
- # Log the metrics to mlflow
- mlflow.log_metrics(dict(mean_loss=intermediate_score), step=step)
- # Feed the score back to Tune.
- session.report({"iterations": step, "mean_loss": intermediate_score})
- time.sleep(0.1)
- def tune_with_setup(mlflow_tracking_uri, finish_fast=False):
- # Set the experiment, or create a new one if does not exist yet.
- mlflow.set_tracking_uri(mlflow_tracking_uri)
- mlflow.set_experiment(experiment_name="mixin_example")
-
- tuner = tune.Tuner(
- train_function_mlflow,
- tune_config=tune.TuneConfig(
- num_samples=5
- ),
- run_config=air.RunConfig(
- name="mlflow",
- ),
- param_space={
- "width": tune.randint(10, 100),
- "height": tune.randint(0, 100),
- "steps": 5 if finish_fast else 100,
- "mlflow": {
- "experiment_name": "mixin_example",
- "tracking_uri": mlflow.get_tracking_uri(),
- },
- },
- )
- results = tuner.fit()
- smoke_test = True
- mlflow_tracking_uri = os.path.join(tempfile.gettempdir(), "mlruns")
- tune_with_setup(mlflow_tracking_uri, finish_fast=smoke_test)
|