Skip to main content

Automating the ML Training Lifecycle with MLxOPS

We tend to reinvent the training lifecycle on different projects. Given that most ML training lifecycle consist of similar steps we can automate most of the parts of an ML project.

This package contains model training life cycle components for automating the training and scoring life cycle of models. Pandas dataframes are used as the underlying data object. As for the estimators, it can be sklearn type estimators that has fit and predict method. Estimators like XGBoost en LightGBM can also be used.

MLxOPS serves as experiment and operations module by also persisting all metadata for each component and any artifacts we need to reproduce results.

The mlxops components module contains Machine Learning life cycle components of each step. The components consists of:

  • DataLoader
    • Data loading component of the training pipeline. This component also contains any stateless preprocessing steps the user wants to apply on the data.
  • DataFeatureMapper
    • Feature processing pipeline. Apply Feature mapper for example Normalization for numeric features and OneHotEncoder for categoric features. Mapper here is stateful.
  • DataValidator
    • Data validation component of the training pipeline. Can use sklearn outlier detectors or use can implement their own. This validator should return a mask where -1 is an outlier and 1 an inlier.
  • ModelTrainer
    • Model component of the training pipeline. ModelTrainer depends on a runned DataLoader, DataFeatureMapper and DataValidator to fit estimator.
  • ModelEvaluator
    • Compare metrics between trained model and current model in production. This can be any of the metrics implemented in sklearn
  • ArtifactPusher
    • Pushes artifacts to PROD directory if current model is an improvement on the current best model

The mlxops package also contains high level interfaces for training and scoring using pipeline modules:

  • ModelTrainingPipeline
    • This is a high level implementation of the training life cycle for all the steps in life cycle
  • ScoringPipeline
    • This pipeline can be used to score data. The model can be loaded from disk or supplied.

To install package:

pip install mlxops

Example 1: Using the different pipeline components

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import IsolationForest
from sklearn.metrics import accuracy_score
from sklearn.model_selection import ShuffleSplit

# local imports
import mlxops
from mlxops.components import DataLoader, DataFeatureMapper, DataValidator
from mlxops.components import ModelTrainer, ModelEvaluator, ArtifactPusher

DataLoader component of the life cycle

Load data using the DataLoader to load data:

file_url = "http://storage.googleapis.com/download.tensorflow.org/data/heart.csv"
data_loader = DataLoader.from_file(file_url, target='target', splitter=ShuffleSplit)
data_loader.run()

DataLoader splits data into train, test and evaulation sets. These sets can retrieved from the following properties

data_loader.train_set
data_loader.test_set
data_loader.eval_set

Feature mapper component of the life cycle

This component does for example Normalization, OneHotEncoding etc. If the dtypes are properly set user can use the infered pipeline. For this example we will use the infered feature mapper. The feature mapper's run method takes in a DataLoader object.

feature_mapper = DataFeatureMapper.from_infered_pipeline()
feature_mapper.run(data_loader=data_loader)

Example to apply the feature mapper:

mapped_train_features = feature_mapper.transform(data_loader.train_set[0])

Validator component of the life cycle

This component step is optional and it takes in a sklearn validator or outlier detector as input. This validator needs to implement a predict method that returns 1 for inlier and -1 for outlier. The validator component gets applied on feature mapped data from the feature_mapper step. The run method takes in a runned DataLoader and DataValidator as input.

data_validator = DataValidator(
    validator=IsolationForest(contamination=0.001)
)
data_validator.run(data_loader=data_loader, feature_mapper=feature_mapper)

The mask for the train data can be retrieved from a property method. This mask can be used in conjunction with the DataLoader component to select only relevant samples. For example

train_data, train_targets = data_loader.train_set
valid_train_data, valid_train_targets = train_data[data_validator.trainset_valid],\
                                        train_targets[data_validator.trainset_valid]

ModelTrainer Component of the Machine Learning experiment life cycle

The ModelTrainer takes in a sklearn type estimator that implements fit and predict. The ModelTrainer's run method takes components DataLoader, DataFeatureMapper and DataValidator component as inputs. We will set this as our base model.

base_trainer = ModelTrainer(
    estimator=LogisticRegression()
)
base_trainer.run(data_loader, feature_mapper, data_validator)

Let save this base model and build a new model. We will compare the new model against the base model later.

mlxops.saved_model.save(base_trainer, "lr_base_model")

Let build a second challenger model using a random forest classifier

new_trainer = ModelTrainer(
    estimator=RandomForestClassifier(n_estimators=50)
)
new_trainer.run(data_loader, feature_mapper, data_validator)

ModelEvaluator component step in the Machine Learning life cycle

The ModelEvaluator component compares two models based on the supplied metrics. he ModelEvaluator's run method takes components DataLoader, DataFeatureMapper and DataValidator component as inputs as well as a new trained model. The base model is an instance atrributes of the class.

evaluator = ModelEvaluator(base_model="lr_base_model",
                           metrics=[accuracy_score])
evaluator.run(
    data_loader, feature_mapper, data_validator, new_trainer
)

To check if this model should be pushed:

evaluator.push_model

Check the models compare with the supplied metrics:

evaluator.evaluation_metrics

Pipeline example

For a more high level interface. We can build a pipeline. Here we demonstrate a ModelTrainingPipeline. Similar to the example above.

from mlxops.pipeline import ModelTrainingPipeline

Setup Pipeline

file_url = "http://storage.googleapis.com/download.tensorflow.org/data/heart.csv"

train_pipeline_arguments = {
    'data_loader': DataLoader.from_file(
        file_name=file_url, target='target', splitter=ShuffleSplit
    ),
    'data_validator': DataValidator(
        validator=IsolationForest(contamination=0.01)
    ),
    'feature_mapper': DataFeatureMapper.from_infered_pipeline(),
    'trainer': ModelTrainer(
        estimator=RandomForestClassifier(n_estimators=100, max_depth=3)
    ),
    'evaluator': ModelEvaluator(
        base_model='lr_base_model',
        metrics=[accuracy_score]
    ),
    'pusher': ArtifactPusher(model_serving_dir='testfolder'),
    "run_id": "0.0.0.1"
}

Execute Pipeline:

train_pipeline = ModelTrainingPipeline(
    **train_pipeline_arguments
)
train_pipeline.run()
mlxops.saved_model.save(train_pipeline, "base_model")

The saved_model module saves all component artifacts used in the pipeline along with other metadata. Lets load the saved pipeline

del train_pipeline

loaded_pipeline = mlxops.saved_model.load("base_model")

Each component can be access through loaded_pipeline.component where component can be any of:

* data_loader
* data_validator
* evaluator
* feature_mapper
* trainer
* pusher

Metadata

Release files for mlxops 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlxops 0.1.0
File Size Uploaded Provenance Actions
mlxops-0.1.0.tar.gz 12.4 kB Without provenance Details for mlxops-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mlxops 0.1.0
File Interpreter ABI Platform Actions
mlxops-0.1.0-py3.6.egg Legacy Egg format - - Details for mlxops-0.1.0-py3.6.egg
mlxops-0.1.0-py3-none-any.whl Python 3 none any Details for mlxops-0.1.0-py3-none-any.whl

Total release size: 63.4 kB

Release files / mlxops-0.1.0.tar.gz

Download URL mlxops-0.1.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d8f6a0fc5dd97f180a40ba0b5b54d748d5e9a18addb4d6b815f046c8ecd72d78
BLAKE2b-256 checksum
How to use checksums
bdf3042f9c97ef5fa8e7fc23fa5bb2f822be8f77cb42c9fea9a1c68ba3067d9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.15

Release files / mlxops-0.1.0-py3.6.egg

Download URL mlxops-0.1.0-py3.6.egg
Size 34.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
41f20a0ff4293fe378ff8f7df38fe0c9e5e894b7943cc96d1362b2a1ff119d07
BLAKE2b-256 checksum
How to use checksums
c477dca2d818adc2df1afff48022a461c90ae2f82f6d1c719926b871db951aff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.15

Release files / mlxops-0.1.0-py3-none-any.whl

Download URL mlxops-0.1.0-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52de4b3cbdaaeda248a5210dbbf2ad9a4cb8e236c4ece6159664c754c934723c
BLAKE2b-256 checksum
How to use checksums
4cfa82e7d32787d32a439c0abc003bd73e0ed42a05bd69d0fc047b513f6e0321
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.15

Security for mlxops 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

3 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page