Usage Guide¶
This guide demonstrates how to use the torchhydro library to configure and run hydrological models. The core workflow revolves around defining a set of parameters, updating a default configuration, and launching the training and evaluation process with a single function.
Core Concept¶
The main workflow consists of three steps:
- Define Parameters: You start by defining all your experiment's parameters (like model choice, dataset, variables, and hyperparameters) using the
torchhydro.configs.config.cmdfunction. This creates a parameter object. - Update Configuration: The default configuration is loaded using
torchhydro.configs.config.default_config_file(). Then, your custom parameters are merged into it usingtorchhydro.configs.config.update_cfg(). - Train and Evaluate: Finally, you pass the consolidated configuration dictionary to the
torchhydro.trainers.trainer.train_and_evaluate()function, which handles the entire pipeline: data loading, model building, training, and evaluation.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Data configuration¶
Before running anything, create hydro_setting.yml in your home directory so the data resolver can locate datasets (see Installation):
1 2 3 4 | |
In source_cfgs, select a dataset with the dataset_id key:
1 | |
Paths are resolved centrally from hydro_setting.yml — there is no per-dataset path to configure. The legacy source_name + source_path form is still accepted for backward compatibility, but source_path is ignored in favor of central resolution.
Below are two practical examples demonstrating this workflow.
Example 1: Training a Standard LSTM on CAMELS Data¶
This example shows how to train a standard LSTM model for streamflow prediction using the CAMELS-US dataset.
Step 1: Define Parameters¶
First, we define all the necessary parameters for our experiment. This includes data source, model type, variables, time periods, and training settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Step 2: Run the Pipeline¶
With the parameters defined, we simply call the update and train functions.
1 2 3 4 5 6 7 8 9 | |
The library will now handle data loading, preprocessing, model training, and finally, evaluation on the test set. Results will be saved in the directory specified by the sub parameter (e.g., results/lstm_camels).
Example 2: Training a Physics-Informed Model (DPL-XAJ)¶
This example demonstrates a more advanced use case: training a Differentiable Parameter Learning (DPL) model. Here, an LSTM is coupled with the Xinanjiang (XAJ) hydrological model. The network learns to output the parameters of the XAJ model.
Step 1: Define DPL Parameters¶
The parameter definition is similar, but we specify a different model (DplAttrXaj), dataset (DplDataset), and some additional hyperparameters specific to the physics-informed approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Step 2: Run the DPL Pipeline¶
The execution step is identical.
1 2 3 4 5 6 7 8 9 | |
Conventions and Additional Features¶
Time ranges are left-closed, right-open¶
Each period ["start", "end"] includes start and excludes end. Adjacent periods therefore do not overlap:
1 2 | |
A strict overlap between train/valid/test is reported as an advisory warning (results are labelled in-sample); it never blocks execution. Set allow_split_overlap: True to suppress the warning when the in-sample intent is explicit.
Time units¶
min_time_unit accepts h/H (hourly), d/D (daily), and ME/MS/M (monthly). Use min_time_interval for sub-daily intervals (e.g. "3" with unit "h" for 3-hourly data).
Cloud-Zarr lazy loading¶
To train on large cloud Zarr stores without loading them fully into memory, use the CloudZarrLazyDataset dataset and CloudZarrChunkBatchSampler sampler. See the Cloud-Zarr Joint Training guide and experiments/train_cloud_zarr_lazy_joint.py for details.
Explore More¶
torchhydro supports a wide range of models, datasets, and data sources beyond the examples above.
Example Guides¶
- LSTM on CAMELS-US: Benchmark results on 671 US basins
- Songliao Flood Models: LSTM / LSTM+GNN / WDNE flood forecasting
- Sanxia Multi-Site: LSTM / MTSLSTM multi-site experiments
- MoE / Diffusion / FNO: Mixture-of-Experts, Diffusion, and Fourier Neural Operator models
- Seq2Seq / BALSTM / MTSLSTM: Encoder-decoder, attention-based LSTM, multi-timescale models
Advanced Topics¶
- Cloud-Zarr Joint Training: Lazy loading from cloud Zarr stores
- Lightning Fabric: Distributed training with Fabric
- ERA5-Land & GPM: ERA5-Land reanalysis and GPM satellite precipitation
- Model Interpretability: SHAP analysis and loss landscape visualization
API Reference¶
- Models API: All registered model architectures and loss functions
- Datasets API: Dataset classes, data sources, samplers, and scalers
- Trainers API: Training and evaluation pipeline
- Configs API: Configuration system and data resolver