feat: enhance training model functionality and reporting
- Added `evidently` to requirements for improved model evaluation. - Introduced `TrainModelResult` class with a `to_dict` method for better result handling. - Updated `train_model` method to return a comprehensive training result, including run details. - Enhanced `cleanup_run_directory` method in `DataManagerRepository` for improved resource management. - Adjusted type hints in `TrainModel` for clarity and consistency.
This commit is contained in:
@@ -23,6 +23,9 @@ with workflow.unsafe.imports_passed_through():
|
||||
|
||||
from model_manager.utils.models.train_model_params import TrainModelParams
|
||||
from model_manager.utils.repository.data_manager_repository import DataManagerRepository
|
||||
from model_manager.utils.models.train_model_result import TrainModelResult
|
||||
|
||||
import mlflow
|
||||
|
||||
|
||||
class Training(SientiaMonitoring):
|
||||
@@ -146,7 +149,7 @@ class Training(SientiaMonitoring):
|
||||
raise
|
||||
|
||||
@activity.defn(name='train_model')
|
||||
async def train_model(self, input_data: dict[str, Any]) -> dict[str, str | None]:
|
||||
async def train_model(self, input_data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""
|
||||
Train a machine learning model.
|
||||
|
||||
@@ -246,22 +249,24 @@ class Training(SientiaMonitoring):
|
||||
tags=None,
|
||||
metadata=metadata,
|
||||
) as run_info:
|
||||
train_result.run_name = run_info.run_name
|
||||
train_result.run_id = run_info.run_id
|
||||
|
||||
train_result = self.data_manager_repository.generate_report(
|
||||
train_result,
|
||||
metadata=metadata,
|
||||
)
|
||||
|
||||
if train_result.report_path is None or train_result.train_data_path is None or train_result.test_data_path is None:
|
||||
raise ValueError('Report path, train data path, or test data path is not set')
|
||||
|
||||
wrapper.store_model(name=train_params.model_name)
|
||||
|
||||
train_result.run_name = run_info.run_name
|
||||
train_result.run_id = run_info.run_id
|
||||
mlflow.log_artifact(train_result.report_path)
|
||||
mlflow.log_artifact(train_result.train_data_path)
|
||||
mlflow.log_artifact(train_result.test_data_path)
|
||||
|
||||
train_result = self.data_manager_repository.generate_report(
|
||||
train_result,
|
||||
metadata=metadata,
|
||||
)
|
||||
|
||||
model_saved = True
|
||||
|
||||
return {
|
||||
'run_name': run_info.run_name,
|
||||
'run_id': run_info.run_id,
|
||||
}
|
||||
return train_result.to_dict()
|
||||
except Exception as e: # noqa: BLE001
|
||||
|
||||
error_msg = f'Error training model - error: {str(e)}'
|
||||
@@ -296,7 +301,7 @@ class Training(SientiaMonitoring):
|
||||
run_dir = input_data.get('run_dir', '')
|
||||
|
||||
try:
|
||||
self.model_repository.cleanup_run_directory(run_dir)
|
||||
self.data_manager_repository.cleanup_run_directory(run_dir, metadata)
|
||||
except Exception as e: # noqa: BLE001
|
||||
error_msg = f'Error cleaning up resources - Run directory: {run_dir}, Error: {str(e)}'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user