create_log_callback#
- pymc_marketing.mlflow.create_log_callback(stats=None, parameters=None, exclude_tuning=True, take_every=100)[source]#
Create callback function to log sample stats and parameter values to MLflow during sampling.
This callback only works for the “pymc” sampler.
- Parameters:
stats (
list[str] |None) – List of sample statistics to log from the Drawparameters (
list[str] |None) – Names of variables to log from the draw PyMC has just recorded. A model-level name such assigmais logged on its constrained scale, a value variable name such assigma_log__on the sampler’s unconstrained scale. Deterministics can be logged too. Each value must be a scalar, sincemlflow.log_metriconly accepts scalars.exclude_tuning (
bool) – Whether to exclude tuning steps from logging. Defaults to True.take_every (
int) – Specifies the interval at which to log values. Defaults to 100.
- Returns:
callback (
Callable) – The callback function to log sample stats and parameter values to MLflow during sampling
Examples
Create example model:
import pymc as pm with pm.Model() as model: mu = pm.Normal("mu") sigma = pm.HalfNormal("sigma") obs = pm.Normal("obs", mu=mu, sigma=sigma, observed=[1, 2, 3])
Log off divergences and logp every 100th draw:
import mlflow from pymc_marketing.mlflow import create_log_callback callback = create_log_callback( stats=["diverging", "model_logp"], take_every=100, ) mlflow.set_experiment("Live Tracking Stats") with mlflow.start_run(): idata = pm.sample(model=model, callback=callback)
Log the parameters
muandsigmaevery 100th draw. PyMC samplessigmaon the unconstrained scale assigma_log__; the callback logssigmaitself, read from the draw PyMC just recorded. Passsigma_log__to log the unconstrained value instead:import mlflow from pymc_marketing.mlflow import create_log_callback callback = create_log_callback( parameters=["mu", "sigma"], take_every=100, ) mlflow.set_experiment("Live Tracking Parameters") with mlflow.start_run(): idata = pm.sample(model=model, callback=callback)