pymc-labs / pymc-marketing

Bayesian marketing toolbox in PyMC. Media Mix (MMM), customer lifetime value (CLV), buy-till-you-die (BTYD) models and more.
https://www.pymc-marketing.io/
Apache License 2.0
653 stars 179 forks source link

MLflow deployment example #901

Open wd60622 opened 1 month ago

wd60622 commented 1 month ago

Might be useful to have a deployment example using MLflow where various aspects of the model are logged

Would touch on logging:

Might contain something like this:

import mlflow

from pymc_marketing.mmm import MMM, GeometricAdstock, LogisticSaturation

mmm = MMM(adstock=adstock, saturation=saturation, ...)
mmm.fit(X_train, y_train)

with mlflow.start_run():
    # Log of the model
    # Some metrics
    mlflow.log_metric("divergences", mmm.idata.sample_stats["diverging"].sum())
    mlflow.log_metric("r2", ...)

    # Some parameters
    mmm.log_params(mmm.create_idata_attrs())
    mlflow.log_param("adstock_name", mmm.adstock.lookup_name)
    mlflow.log_param("saturation_name", mmm.saturation.lookup_name)
    mlflow.log_param("likelihood", mmm.model_config["likelihood"].distribution)

    # Artifacts
    configuration_file = "configuration.json"
    with open(configuration_file, "w") as f: 
        json.dump(mmm.create_idata_attrs(), f)
    mlflow.log_artifact(configuration_file)

    curve = mmm.saturation.sample_curve(mmm.idata.posterior)
    fig, _ = mmm.saturation.plot_curve(curve)
    mlflow.log_figure(fig, "saturation_curve.png")

Related to #891

juanitorduz commented 1 month ago

💯 !

louismagowan commented 1 month ago

@wd60622 I have some code written on this already which I can share if you wanna do the PR together 😁

wd60622 commented 1 month ago

@wd60622 I have some code written on this already which I can share if you wanna do the PR together 😁

Yeah, sounds great. Maybe drop what you have here or let's create a draft PR.

I against mlflow being a required dependency but think some type of helper submodule would be great. I see pip install pymc-marketing[mlflow] in our future

louismagowan commented 1 month ago

Ok cool, bear with me ⏳ 😁

louismagowan commented 1 month ago

Draft PR can be found here ☺️