|
| 1 | +# Apache Software License 2.0 |
| 2 | +# |
| 3 | +# Copyright (c) ZenML GmbH 2024. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from typing import Optional |
| 19 | +from uuid import UUID |
| 20 | + |
| 21 | +from steps import model_evaluator, model_promoter, model_trainer, model_grid_searcher |
| 22 | + |
| 23 | +from pipelines import ( |
| 24 | + feature_engineering, |
| 25 | +) |
| 26 | +from zenml import pipeline |
| 27 | +from zenml.client import Client |
| 28 | +from zenml.logger import get_logger |
| 29 | + |
| 30 | + |
| 31 | +logger = get_logger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +@pipeline |
| 35 | +def training( |
| 36 | + train_dataset_id: Optional[UUID] = None, |
| 37 | + test_dataset_id: Optional[UUID] = None, |
| 38 | + target: Optional[str] = "target", |
| 39 | +): |
| 40 | + """ |
| 41 | + Model training pipeline. |
| 42 | +
|
| 43 | + This is a pipeline that loads the data from a preprocessing pipeline, |
| 44 | + trains a model on it and evaluates the model. If it is the first model |
| 45 | + to be trained, it will be promoted to production. If not, it will be |
| 46 | + promoted only if it has a higher accuracy than the current production |
| 47 | + model version. |
| 48 | +
|
| 49 | + Args: |
| 50 | + train_dataset_id: ID of the train dataset produced by feature engineering. |
| 51 | + test_dataset_id: ID of the test dataset produced by feature engineering. |
| 52 | + target: Name of target column in dataset. |
| 53 | + """ |
| 54 | + # Link all the steps together by calling them and passing the output |
| 55 | + # of one step as the input of the next step. |
| 56 | + |
| 57 | + # Execute Feature Engineering Pipeline |
| 58 | + if train_dataset_id is None or test_dataset_id is None: |
| 59 | + dataset_trn, dataset_tst = feature_engineering() |
| 60 | + else: |
| 61 | + client = Client() |
| 62 | + dataset_trn = client.get_artifact_version( |
| 63 | + name_id_or_prefix=train_dataset_id |
| 64 | + ) |
| 65 | + dataset_tst = client.get_artifact_version( |
| 66 | + name_id_or_prefix=test_dataset_id |
| 67 | + ) |
| 68 | + |
| 69 | + model, _, _ = model_grid_searcher( |
| 70 | + dataset_trn=dataset_trn, target=target |
| 71 | + ) |
| 72 | + |
| 73 | + acc, _ = model_evaluator( |
| 74 | + model=model, |
| 75 | + dataset_trn=dataset_trn, |
| 76 | + dataset_tst=dataset_tst, |
| 77 | + target=target, |
| 78 | + ) |
| 79 | + |
| 80 | + model_promoter(accuracy=acc) |
0 commit comments