Skip to content

Commit b72d45b

Browse files
committed
grid search in clearml
1 parent ea9ec60 commit b72d45b

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

clearml/gridsearch.py

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import logging
2+
3+
from clearml import Task
4+
from clearml.automation import (
5+
DiscreteParameterRange,
6+
HyperParameterOptimizer,
7+
RandomSearch,
8+
UniformIntegerParameterRange,
9+
DiscreteParameterRange,
10+
GridSearch,
11+
Objective
12+
)
13+
14+
15+
def job_complete_callback(
16+
job_id, # type: str
17+
objective_value, # type: float
18+
objective_iteration, # type: int
19+
job_parameters, # type: dict
20+
top_performance_job_id, # type: str
21+
):
22+
print(
23+
"Job completed!", job_id, objective_value, objective_iteration, job_parameters
24+
)
25+
if job_id == top_performance_job_id:
26+
print(
27+
"WOOT WOOT we broke the record! Objective reached {}".format(
28+
objective_value
29+
)
30+
)
31+
32+
33+
# Connecting ClearML with the current process,
34+
# from here on everything is logged automatically
35+
# task = Task.init(
36+
# project_name="Hyper-Parameter Optimization",
37+
# task_name="Automatic Hyper-Parameter Optimization",
38+
# task_type=Task.TaskTypes.optimizer,
39+
# reuse_last_task_id=False,
40+
# )
41+
42+
# experiment template to optimize in the hyper-parameter optimization
43+
# args = {
44+
# "template_task_id": None,
45+
# "run_as_service": False,
46+
# }
47+
# args = task.connect(args)
48+
# Set default queue name for the Training tasks themselves.
49+
# later can be overridden in the UI
50+
from clearml import PipelineController, Logger
51+
52+
def initialization_step(seed=10):
53+
import numpy as np
54+
import pandas as pd
55+
56+
x = np.random.randn(1000)
57+
y = x ** 2
58+
return pd.DataFrame({"x": x, "y": y})
59+
60+
61+
def loss_function(data, theta):
62+
import numpy as np
63+
64+
return np.mean(np.abs(theta * data["x"] ** 2 - data["y"]))
65+
66+
67+
def optimization_step(data, maxiter, popsize):
68+
from scipy.optimize import differential_evolution, Bounds
69+
import numpy as np
70+
71+
def loss_function(data, theta):
72+
import numpy as np
73+
74+
return np.mean(np.abs(theta * data["x"] ** 2 - data["y"]))
75+
76+
def loss(theta):
77+
return loss_function(data, theta)
78+
79+
optim_results = differential_evolution(
80+
loss, Bounds([-1], [1]), maxiter=maxiter, popsize=popsize
81+
)
82+
a = optim_results.x
83+
return a
84+
85+
86+
def evaluation_step(data, a):
87+
from clearml import PipelineController, Logger
88+
def loss_function(data, theta):
89+
import numpy as np
90+
91+
return np.mean(np.abs(theta * data["x"] ** 2 - data["y"]))
92+
93+
loss_value = loss_function(data, a)
94+
print(f"loss={loss_value}")
95+
Logger.current_logger().report_scalar(title="loss", series="loss", value=loss_value, iteration=1)
96+
return loss_value
97+
98+
99+
def setup_pipeline(maxiter, popsize):
100+
pipe = PipelineController(
101+
name="Hyperparam kata controller", project="Hyperparamater kata", version="0.0.1"
102+
)
103+
104+
105+
pipe.add_function_step(
106+
"initialization",
107+
initialization_step,
108+
function_kwargs=dict(seed=10),
109+
function_return=["data"],
110+
cache_executed_step=True,
111+
)
112+
113+
pipe.add_function_step(
114+
"optimization",
115+
optimization_step,
116+
function_kwargs=dict(data="${initialization.data}", maxiter=maxiter, popsize=popsize),
117+
function_return=["a"],
118+
cache_executed_step=True,
119+
)
120+
121+
pipe.add_function_step(
122+
"evaluation",
123+
evaluation_step,
124+
function_kwargs=dict(data="${initialization.data}", a="${optimization.a}"),
125+
function_return=["loss_value"],
126+
monitor_metrics=[("optimization", "loss_value")],
127+
cache_executed_step=True,
128+
)
129+
return pipe
130+
131+
import itertools
132+
133+
for (maxiter, popsize) in itertools.product([10, 100], [10, 100]):
134+
print(f"running pipe with (maxiter, popsize)={(maxiter, popsize)}")
135+
pipe = setup_pipeline(maxiter, popsize)
136+
pipe.set_default_execution_queue("default")
137+
pipe.start_locally(run_pipeline_steps_locally=True)
138+
139+
import ipdb; ipdb.set_trace()
140+
141+
#pipe.set_default_execution_queue("default")
142+
#pipe.start_locally(run_pipeline_steps_locally=True)
143+

0 commit comments

Comments
 (0)