-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhp_tuning_select_best_model.py
41 lines (32 loc) · 1.31 KB
/
hp_tuning_select_best_model.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
# {% include 'template/license_header' %}
from typing import List
from typing_extensions import Annotated
from sklearn.base import ClassifierMixin
from zenml import get_step_context, step
from zenml.logger import get_logger
logger = get_logger(__name__)
@step
def hp_tuning_select_best_model(
step_names: List[str],
) -> Annotated[ClassifierMixin, "best_model"]:
"""Find best model across all HP tuning attempts.
This is an example of a model hyperparameter tuning step that loops
other artifacts linked to model version in Model Control Plane to find
the best hyperparameter tuning output model of all according to the metric.
Returns:
The best possible model class and its' parameters.
"""
### ADD YOUR OWN CODE HERE - THIS IS JUST AN EXAMPLE ###
model = get_step_context().model
best_model = None
best_metric = -1
# consume artifacts attached to current model version in Model Control Plane
for step_name in step_names:
hp_output = model.get_artifact("hp_result")
model_: ClassifierMixin = hp_output.load()
# fetch metadata we attached earlier
metric = float(hp_output.run_metadata["metric"])
if best_model is None or best_metric < metric:
best_model = model_
### YOUR CODE ENDS HERE ###
return best_model