πΏMG¶
A generic framework for building differentiable models. πΏMG enables seamless coupling of neural networks with differentiable process-based equations, leveraging PyTorch's auto-differentiation for efficient, large-scale optimization on GPU. The spiritual successor to HydroDL, πΏMG generalizes differentiable modeling for cross-domain application while also imposing basic standardizations for research-to-operations pipelines.
Key Features¶
- π€ Hybrid Modeling: Combine neural networks with process-based equations for enhanced interpretability and generalizability. Instead of manual model parameter calibration, for instance, use neural networks to directly learn robust and interpretable parameters (Tsai et al., 2021).
- π PyTorch Integration: Scale with PyTorch for efficient training and compatibility with modern ML tools and numerical solvers.
- π§© Modular Plugin Architecture: Swap in domain-specific components and configurations with ease.
- β‘ Benchmarking: All in one place. πΏMG + hydroDL2 will enable rapid deployment and replication of key published MHPI results.
- π NextGen-ready: Designed for CSDMS BMI compliance to support differentiable hydrological models in NOAA-OWP's NextGen National Water Modeling Framework. (See the NextGen-ready πΏHBV2.0 for an example with a πΏMG-supported BMI).
πΏMG is designed to scale with modern deep learning tools (e.g., foundation models) while maintaining physical interpretability. Our peer-reviewed and published benchmarks show that well-tuned differentiable models can match deep networks in performanceβwhile better extrapolating to extreme or data-scarce conditions and predicting physically meaningful variables.
Differentiable modeling introduces more modeling choices than traditional deep learning due to its physical constraints. This includes learning parameters, missing process representations, corrections, or other enhancements for physical models.
Note: While differentiable models are powerful and have many desirable characteristics, they come with a larger decision space than purely data-driven neural networks since physical processes are involved, and can thus feel "trickier" to work with. Hence, we recommend beginning with our example notebooks and then systematically making changes, one at a time. Pay attention to multifaceted outputs, diverse causal analyses, and predictions of untrained variables permitted by differentiable models, rather than purely trying to outperform other models' metrics.
This work is mantained by MHPI and advised by Dr. Chaopeng Shen. If you find it useful, please cite (dedicated citations are coming):
Shen, C., et al. (2023). Differentiable modelling to unify machine learning and physical models for geosciences. Nature Reviews Earth & Environment, 4(8), 552β567. <https://doi.org/10.1038/s43017-023-00450-9>.
Installation¶
To install πΏMG, clone the repo and install in developer mode with Astral UV:
Pip and Conda are also supported, though UV is recommended. See setup for further details.
Quick Start¶
See how to run.
Example -- Differentiable Parameter Learning: Use an LSTM to learn parameters for the HBV hydrological model.
from hydroDL2.models.hbv.hbv import HBV
from dMG.core.data.loaders import HydroLoader
from dMG.core.utils import load_nn_model, print_config, set_randomseed
from dMG.models.delta_models import DplModel
from example import load_config, take_data_sample
CONFIG_PATH = '../example/conf/config_dhbv_1_0.yaml'
# Model configuration
config = load_config(CONFIG_PATH)
# Initialize physical model and NN.
phy_model = HBV(config['delta_model']['phy_model'])
nn = load_nn_model(phy_model, config['delta_model'])
# Create differentiable model dHBV: a torch.nn.Module that describes how
# the NN is linked to the physical model HBV.
dpl_model = DplModel(phy_model=phy_model, nn_model=nn)
# Load dataset of NN and HBV inputs.
dataset = HydroLoader(config).dataset
dataset_sample = take_data_sample(config, dataset, days=730, basins=100)
output = dpl_model(dataset_sample)
This exposes a key characteristic of the differentiable model DplModel
: composition of a physical model, phy_model
, and a neural network, nn
. Internally, DplModel
looks like
# NN forward
parameters = self.nn_model(dataset_sample['xc_nn_norm'])
# Physics model forward
predictions = self.phy_model(
dataset_sample,
parameters,
)
Check out examples to see model training/testing/simulation in detail. We recommend starting here, which is a continuation of the above. A Colab Notebook for this Ξ΄HBV example is also available.
Use Cases¶
1. Lumped Hydrology¶
Lumped differentiable rainfall-runoff models πΏHBV 1.0 and improved πΏHBV 1.1p.
2. Unseen Extreme Events Test with πΏHBV 1.1p¶
In the unseen extreme events spatial test, we used water years with a 5-year or lower return period peak flow from 1990/10/01 to 2014/09/30 for training, and held out the water years with greater than a 5-year return period peak flow for testing. The spatial test was conducted using a 5-fold cross-validation approach for basins in the CAMELS dataset. This application has been benchmarked against LSTM and demonstrates better extrapolation abilities. Find more details and results in Song, Sawadekar, et al. (2024).
3. National-scale Water Modeling¶
A national-scale water modeling study on approximately 180,000 river reaches (with a median length of 7 km) across CONUS using the high-resolution, multiscale, differentiable water model πΏHBV 2.0. This model is also operating at global-scales and has been used to generate high-quality, seamless simulations for the entire CONUS. Find more details and results in Song, Bindas, et al. (2025).
4. Global-scale Photosynthesis Modeling¶
Currently in development. Find more details and results in Aboelyazeed et al. (2024).
Ecosystem Integration¶
- HydroDL 2.0 (
hydroDL2
): Home to MHPI's suite of process-based hydrology models and differentiable model augmentations.
- Differentiable Ecosystem Modeling (
diffEcosys (dev version only)
): A physics-informed machine learning system for ecosystem modeling, demonstrated using the photosynthesis process representation within the Functionally Assembled Terrestrial Ecosystem Simulator (FATES) model. This model is coupled to NNs that learn parameters from observations of photosynthesis rates. - Other Development: Many additions are currently in the progress: (i) numerical PDE solvers on PyTorch, torchode, torchdiffeq; (ii) adjoint sensitivity; (iii) extremely efficient and highly accurate surrogate models for process-based equations; (iv) data assimilation methods; (v) downscaled and bias-corrected climate data; (vi) mysteriously powerful neural networks, and more ...
πΏMG Architecture¶
- Data Loaders: Bulk data preprocessors customized per dataset.
- Data Samplers: Dataset samplers for minibatching during model training and inference.
- Trainers: Orchestrates high-level model training, testing, and simulation.
- ModelHandler: Manages multimodeling, multi-GPU computation, and other high level operations. Acts as an drop-in model interface for CSDMS BMI or other R2O wrappers.
- Delta Models: Differentiable models; describes how NNs and physical models are coupled (e.g.,
DplModel
for parameter learning).
Repo¶
```text
.
βββ src/dMG/
β βββ __main__.py # Runs πΏMG; models, experiments
β βββ core/
β β βββ calc/ # Calculation utilities
β β βββ data/ # Data loaders and samplers
β β βββ post/ # Post-processing utilities; plotting
β β βββ utils/ # Helper functions
β βββ models/
β β βββ criterion # Loss functions
β β βββ delta_models # Differentiable model modalities
β β βββ multimodels # Multimodeling processors
β β βββ neural_networks/ # Neural network architectures
β β βββ phy_models/ # Physical Models
β β βββ model_handler.py # High-level model manager
β βββ trainers/ # Model training routines
βββ conf/
β βββ hydra/ # Hydra settings
β βββ observations/ # Observation configuration files
β βββ config.py # Configuration validator
β βββ default.yaml # Default master configuration file
βββ docs/
βββ envs/ # Python ENV configurations
βββ example/ # Tutorials
```
Contributing¶
We welcome contributions! Please submit changes via a fork and pull requests. For more details, refer to docs/CONTRIBUTING.md.
Please submit an issue to report any questions, concerns, bugs, etc.