-
Notifications
You must be signed in to change notification settings - Fork 0
25 tarin on three year data #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+237
−3
Merged
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
df1105f
initial example of two year data
rogerkuou fef01cf
update examples notebook
rogerkuou 2b3ac47
add example training scritps
rogerkuou 2bdf8b5
add example slurm file
rogerkuou 1159fbc
update fig dir
rogerkuou 3e2c4b4
add README
rogerkuou 994d36b
Merge branch 'main' into 25_test_two_year_data
rogerkuou 8cd1c8f
Apply suggestions from code review
rogerkuou fe8f024
fix conflicts
rogerkuou a3ba05d
separate training and inference
rogerkuou 3c99673
update model exportation with checkpoint
rogerkuou 2b4c7c5
add inference scripts
rogerkuou 9ed2e00
use logging to replace print
rogerkuou 1de1cfb
update example slurm scripts
rogerkuou efa17a1
force example notebook to be identical as main
rogerkuou 25297dc
Apply suggestions from code review
rogerkuou 6b04a27
revert changes in model file
rogerkuou ba0408f
remove inference script
rogerkuou 74a180c
maintain the same config in example script as example notebook
rogerkuou eeb29ff
update the training loop
rogerkuou 8481c08
update logger and log file
rogerkuou baba960
update the training script and slurm file
rogerkuou e89d4e4
document the efficiency calculation in README
rogerkuou 3a5dbd6
add an example slurm log
rogerkuou 4fd95d9
Merge branch 'main' into 25_test_two_year_data
rogerkuou fb94642
add docstring to datasets
rogerkuou 17a8294
update example training script with train_monthly_model function
rogerkuou 13714f9
update slurm
rogerkuou 228c18a
update training script
rogerkuou 8898cbf
add logger to training script
rogerkuou c799baf
enable printing in slurm logv files
rogerkuou 92fbdeb
add constraints on the lattitude
rogerkuou 3b9cae6
add slurm output file of a subset
rogerkuou 7494f4a
add a full SLURM log file
rogerkuou a13472b
update readme
rogerkuou 9cbd7e6
update slurm job time to 4hrs default
rogerkuou e9996e4
Update scripts/README.md
rogerkuou 6bd6391
update longitude constraint
rogerkuou cfb2d7c
Merge branch 'main' into 25_test_two_year_data
rogerkuou 2981bbd
update training script with new etting
rogerkuou 4fe2aa2
config data paths
rogerkuou 7b2eef3
update training script
rogerkuou 8ea313d
update training script
rogerkuou 7fac495
update training script
rogerkuou e5c3e48
update training parameters
rogerkuou 1c0a61a
fix residuals
rogerkuou 3dd0364
variable names
rogerkuou 78b3a8c
add scripts to run best tuned model on test dataset
rogerkuou 66560b0
doc best hypterparameters
rogerkuou 1e93be5
remove shebang
rogerkuou eaa9d8f
solve conflict
rogerkuou dbfbd47
reorgnize folder
rogerkuou aa2779b
formatting
rogerkuou f1a8fab
merge run best model for example
rogerkuou 90f94f1
rename training.slurm
rogerkuou 379f953
update training script
rogerkuou 5cae0b8
update the latest parameters
rogerkuou a59e227
reduce node number and training batch number
rogerkuou 0d02180
Merge branch 'main' into 25_test_two_year_data
SarahAlidoost 193eb1a
remove old logs
SarahAlidoost 66083a7
clean up the scripts
SarahAlidoost 8bedfed
add early stopping print statement to train
SarahAlidoost 235a912
expose some more tune parameters in training, adjust scheduler min_lr…
SarahAlidoost 35bbcf7
undo the unneccessary changes to data_preparation scripts
SarahAlidoost 1a6716e
update scripts
SarahAlidoost 1a5996e
update scripts
SarahAlidoost f049d25
adjust batch size
SarahAlidoost 3218cce
update readme of scripts
SarahAlidoost d46d699
update batch size
SarahAlidoost 1d3d77f
solve conficts
rogerkuou 1c1d0d2
update readme
rogerkuou 53e4f27
update readme
rogerkuou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| import ray | ||
| import xarray as xr | ||
|
|
||
| from climanet.dataset import DataLoaderConfig, STDataset | ||
| from climanet.st_encoder_decoder import SpatioTemporalModel | ||
| from climanet.train import TrainConfig, train_monthly_model | ||
| from climanet.utils import configure_compute_resources, read_st_data, set_seed | ||
|
|
||
|
|
||
| def _build_dataset( | ||
| prepared_data_dir: Path, | ||
| years: list[int], | ||
| var_name: str, | ||
| land_mask: xr.DataArray, | ||
| patch_size: tuple[int, int, int], | ||
| stride: tuple[int, int], | ||
| ) -> STDataset: | ||
|
|
||
| data = [read_st_data(data_path=f"{prepared_data_dir}/{year}", var_name=var_name) for year in years] | ||
| input_das, input_da_nan_masks, monthly_das, padded_days_masks, time_features_list = zip(*data) | ||
|
|
||
| input_da = xr.concat(input_das, dim="M") | ||
| input_da_nan_mask = xr.concat(input_da_nan_masks, dim="M") | ||
| monthly_da = xr.concat(monthly_das, dim="M") | ||
| padded_days_mask = xr.concat(padded_days_masks, dim="M") | ||
| time_features = xr.concat(time_features_list, dim="M") | ||
|
|
||
| return STDataset( | ||
| input_da=input_da, | ||
| input_da_nan_mask=input_da_nan_mask, | ||
| monthly_da=monthly_da, | ||
| padded_days_mask=padded_days_mask, | ||
| time_features=time_features, | ||
| land_mask=land_mask, | ||
| patch_size=patch_size, | ||
| stride=stride, | ||
| sh_embed_dim=96, | ||
| sh_order_L=10, | ||
| verbose=False, | ||
| load_lazy=True, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--run-dir", | ||
| type=str, | ||
| default=Path("./run_dir").resolve(), | ||
| ) | ||
| parser.add_argument( | ||
| "--prepared-data-dir", | ||
| type=str, | ||
| default=Path("./data").resolve(), | ||
| ) | ||
| parser.add_argument( | ||
| "--tune-dir", | ||
| type=str, | ||
| default=Path("./data").resolve(), | ||
| ) | ||
| parser.add_argument( | ||
| "--lsm-dir", | ||
| type=str, | ||
| default=Path("./data").resolve(), | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| var_name = "tos" | ||
| device = "cuda" | ||
| prepared_data_dir = Path(args.prepared_data_dir).resolve() | ||
| lsm_dir = Path(args.lsm_dir).resolve() | ||
| tune_dir = Path(args.tune_dir).resolve() | ||
| run_dir = Path(args.run_dir).resolve() | ||
|
|
||
| # Load the best hyperparameters from tuning | ||
| analysis = ray.tune.ExperimentAnalysis(str(tune_dir)) | ||
| best_result = analysis.get_best_trial("loss", "min") | ||
| best_config = best_result.config | ||
|
|
||
| # set the random seed for reproducibility | ||
| set_seed() | ||
|
|
||
| # Build dataset for training and validation | ||
| lsm_file_path = lsm_dir / "era5_lsm_bool.nc" | ||
| lsm_mask = xr.open_dataset(lsm_file_path)["lsm"] # make sure is dask array | ||
|
|
||
| dataset_patch_size = (1, 40, 40) | ||
| dataset_stride = (20, 20) | ||
|
|
||
| train_years = [2018, 2019, 2020] | ||
| dataset_train = _build_dataset( | ||
| prepared_data_dir=prepared_data_dir, | ||
| years=train_years, | ||
| var_name=var_name, | ||
| land_mask=lsm_mask, | ||
| patch_size=dataset_patch_size, | ||
| stride=dataset_stride, | ||
| ) | ||
|
|
||
| validation_year = [2021] | ||
| dataset_validation = _build_dataset( | ||
| prepared_data_dir=prepared_data_dir, | ||
| years=validation_year, | ||
| var_name=var_name, | ||
| land_mask=lsm_mask, | ||
| patch_size=dataset_patch_size, | ||
| stride=dataset_stride, | ||
| ) | ||
|
|
||
| # Build the dataloader config | ||
| dataloader_num_workers = 32 # adjust if needed | ||
| use_cuda = device == "cuda" | ||
| dataloader_config = DataLoaderConfig( | ||
| batch_size=100, # adjust if OOM issue | ||
| shuffle=True, | ||
| num_workers=dataloader_num_workers, | ||
| pin_memory=use_cuda, | ||
| persistent_workers=True, | ||
| device=device, | ||
| multiprocessing_context="spawn", | ||
| ) | ||
|
|
||
| # Build the model with the best hyperparameters from tuning | ||
| patch_size = (1, best_config["patch_size"], best_config["patch_size"]) | ||
| overlap = best_config["overlap"] | ||
| embed_dim = best_config["embed_dim"] | ||
| dropout = best_config["dropout"] | ||
| hidden = best_config["hidden"] | ||
| spatial_depth = best_config["spatial_depth"] | ||
| spatial_heads = best_config["spatial_heads"] | ||
|
|
||
| model = SpatioTemporalModel( | ||
| patch_size=patch_size, | ||
| overlap=overlap, | ||
| embed_dim=embed_dim, | ||
| dropout=dropout, | ||
| hidden=hidden, | ||
| spatial_depth=spatial_depth, | ||
| spatial_heads=spatial_heads, | ||
| ) | ||
|
|
||
| # move the model to GPU and configure compute resources | ||
| model = configure_compute_resources( | ||
| model, | ||
| device=device, | ||
| compute_threads=None, # on gpu, it is not used | ||
| dataloader_num_workers=dataloader_num_workers | ||
| ) | ||
|
|
||
| # Training configuration | ||
| training_config = TrainConfig( | ||
| calculate_residuals=True, | ||
| num_epoch=101, | ||
| patience=10, | ||
| accumulation_steps=2, | ||
| optimizer_lr=best_config["optimizer_lr"], | ||
| device=device, | ||
| verbose=True, | ||
| verbose_epoch_interval=20, | ||
| tune_checkpoint=False, | ||
| store_model=True, | ||
| store_logs=True, | ||
| ) | ||
|
|
||
| trained_model = train_monthly_model( | ||
| model=model, | ||
| dataset_train=dataset_train, | ||
| dataloader_config=dataloader_config, | ||
| training_config=training_config, | ||
| dataset_validation=dataset_validation, | ||
| run_dir=run_dir, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/bin/bash | ||
| #SBATCH --job-name=training | ||
| #SBATCH --partition=gpu | ||
| #SBATCH --constraint=a100_80 | ||
| #SBATCH --nodes=1 | ||
| #SBATCH --ntasks-per-node=1 | ||
| #SBATCH --cpus-per-task=128 | ||
| #SBATCH --gpus-per-task=4 | ||
| #SBATCH --exclusive | ||
| #SBATCH --mem=0 | ||
| #SBATCH --time=12:00:00 | ||
| #SBATCH --account=bd0854 | ||
| #SBATCH --output=training_%j.out | ||
|
|
||
| set -euo pipefail | ||
| ulimit -s 204800 | ||
|
|
||
| # Activate uv env | ||
| UV_ENV="$HOME/climanet_py314" | ||
| source "$UV_ENV/bin/activate" | ||
|
|
||
| # Set the scratch directory because they are avialable to all nodes | ||
| RUN_DIR="/scratch/b/$USER/train" | ||
|
|
||
| # data directory (adjust this path to your data location) | ||
| PREPARED_DATA_DIR="/scratch/b/$USER/data" | ||
| TUNE_DIR="/scratch/b/$USER/tune" | ||
| LSM_DIR="/scratch/b/$USER/data" | ||
|
|
||
| echo "Starting training.py script..." | ||
| python -u $HOME/ClimaNet/scripts/training.py \ | ||
| --run-dir "$RUN_DIR" \ | ||
| --prepared-data-dir "$PREPARED_DATA_DIR" \ | ||
| --tune-dir "$TUNE_DIR" \ | ||
| --lsm-dir "$LSM_DIR" | ||
|
|
||
| echo "**********Training script completed.************" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.