forked from zenml-io/zenml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_promoter.py
72 lines (62 loc) · 2.51 KB
/
model_promoter.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Apache Software License 2.0
#
# Copyright (c) ZenML GmbH 2024. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from zenml import get_step_context, step
from zenml.client import Client
from zenml.logger import get_logger
logger = get_logger(__name__)
@step
def model_promoter(accuracy: float, stage: str = "production") -> bool:
"""Model promoter step.
This is an example of a step that conditionally promotes a model. It takes
in the accuracy of the model and the stage to promote the model to. If the
accuracy is below 80%, the model is not promoted. If it is above 80%, the
model is promoted to the stage indicated in the parameters. If there is
already a model in the indicated stage, the model with the higher accuracy
is promoted.
Args:
accuracy: Accuracy of the model.
stage: Which stage to promote the model to.
Returns:
Whether the model was promoted or not.
"""
is_promoted = False
if accuracy < 0.8:
logger.info(
f"Model accuracy {accuracy*100:.2f}% is below 80% ! Not promoting model."
)
else:
logger.info(f"Model promoted to {stage}!")
is_promoted = True
# Get the model in the current context
current_model = get_step_context().model
# Get the model that is in the production stage
client = Client()
try:
stage_model = client.get_model_version(current_model.name, stage)
# We compare their metrics
prod_accuracy = stage_model.get_artifact(
"sklearn_classifier"
).run_metadata["test_accuracy"]
if float(accuracy) > float(prod_accuracy):
# If current model has better metrics, we promote it
is_promoted = True
current_model.set_stage(stage, force=True)
except KeyError:
# If no such model exists, current one is promoted
is_promoted = True
current_model.set_stage(stage, force=True)
return is_promoted