deepcausalmmm.core.train_model

Training functions for DeepCausalMMM models. Updated to use UnifiedDataPipeline for consistent data processing.

Functions

calculate_r2(y_true, y_pred)

Calculate R-squared score.

train_mmm(X_media, X_control, y[, config, ...])

Train a DeepCausalMMM model with optional UnifiedDataPipeline.

train_model_with_config(model, ...[, ...])

Train model with config-driven parameters and warm-start.

train_unified_mmm(*args, **kwargs)

Legacy wrapper for train_mmm with unified pipeline.

deepcausalmmm.core.train_model.calculate_r2(y_true: Tensor, y_pred: Tensor) float[source]

Calculate R-squared score.

deepcausalmmm.core.train_model.train_model_with_config(model: DeepCausalMMM, X_media_padded: Tensor, X_control_padded: Tensor, R: Tensor | None, y_padded: Tensor, config: Dict[str, Any], verbose: bool = True, holdout_data: Dict[str, Tensor] | None = None, pipeline: Any | None = None) Tuple[List[float], List[float], List[float], float][source]

Train model with config-driven parameters and warm-start. This matches the proven approach from dashboard_rmse_optimized.py

Parameters:
  • model – DeepCausalMMM model instance

  • X_media_padded – Padded media data [n_regions, n_timesteps, n_channels]

  • X_control_padded – Padded control data [n_regions, n_timesteps, n_controls]

  • R – Region tensor (can be None)

  • y_padded – Padded target data [n_regions, n_timesteps]

  • config – Configuration dictionary

  • verbose – Whether to print progress

Returns:

Tuple of (train_losses, train_rmses, train_r2s, best_rmse)

deepcausalmmm.core.train_model.train_mmm(X_media: ndarray, X_control: ndarray, y: ndarray, config: Dict[str, Any] | None = None, channel_names: List[str] | None = None, control_names: List[str] | None = None, verbose: bool = True, use_unified_pipeline: bool = False, train_ratio: float | None = None) Tuple[DeepCausalMMM, Dict[str, Any]][source]

Train a DeepCausalMMM model with optional UnifiedDataPipeline.

Deprecated since version 1.0.0: This function-based approach is deprecated. Please use the modern class-based approach with ModelTrainer instead:

`python from deepcausalmmm.core.trainer import ModelTrainer trainer = ModelTrainer(config) model = trainer.create_model(n_media, n_control, n_regions) trainer.create_optimizer_and_scheduler() results = trainer.train(...) `

Parameters:
  • X_media – Media variables [n_regions, n_timesteps, n_channels]

  • X_control – Control variables [n_regions, n_timesteps, n_controls]

  • y – Target variable [n_regions, n_timesteps]

  • config – Configuration dictionary (uses default if None)

  • channel_names – List of channel names

  • control_names – List of control variable names

  • verbose – Whether to print progress

  • use_unified_pipeline – Whether to use UnifiedDataPipeline for train/holdout split

  • train_ratio – Deprecated - use config[‘holdout_weeks’] instead

Returns:

Tuple of (trained_model, results_dict)

deepcausalmmm.core.train_model.train_unified_mmm(*args, **kwargs)[source]

Legacy wrapper for train_mmm with unified pipeline. Use train_mmm instead.