-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'downstream/dev' into dev
- Loading branch information
Showing
10 changed files
with
503 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,83 @@ | ||
""" | ||
Author: silencesoup [email protected] | ||
Date: 2024-04-20 11:30:16 | ||
LastEditors: silencesoup [email protected] | ||
LastEditTime: 2024-04-20 11:41:05 | ||
FilePath: /torchhydro/tests/test_model.py | ||
""" | ||
|
||
import torch | ||
from torchhydro.models import seq2seq | ||
from torchhydro.models import model_dict_function | ||
|
||
|
||
def test_model(): | ||
# put your model param here, it's just an example for seq2seq model | ||
model_configs = { | ||
"Seq2Seq": { | ||
"input_size": 16, | ||
"output_size": 1, | ||
"hidden_size": 256, | ||
"forecast_length": 168, | ||
"cnn_size": 120, | ||
"model_mode": "single", | ||
}, | ||
"Seq2Seq_dual": { | ||
"input_size": 16, | ||
"output_size": 1, | ||
"hidden_size": 256, | ||
"forecast_length": 168, | ||
"cnn_size": 120, | ||
"model_mode": "dual", | ||
"input_size_encoder2": 1, | ||
}, | ||
} | ||
batch_size = 2 | ||
|
||
# Initialize the model | ||
model_config = model_configs["Seq2Seq_dual"] | ||
|
||
model = model_dict_function.pytorch_model_dict["Seq2Seq"]( | ||
input_size=model_config["input_size"], | ||
output_size=model_config["output_size"], | ||
hidden_size=model_config["hidden_size"], | ||
forecast_length=model_config["forecast_length"], | ||
cnn_size=model_config["cnn_size"], | ||
model_mode=model_config["model_mode"], | ||
) | ||
|
||
# Generate random inputs for testing | ||
# sourcery skip: no-conditionals-in-tests | ||
if model_config["model_mode"] == "single": | ||
src1 = torch.rand( | ||
batch_size, | ||
model_config["forecast_length"], | ||
model_config["input_size"] - 1, | ||
) | ||
src2 = torch.rand( | ||
batch_size, model_config["forecast_length"], model_config["cnn_size"] | ||
) | ||
else: # "dual" | ||
src1 = torch.rand( | ||
batch_size, model_config["forecast_length"], model_config["input_size"] | ||
) | ||
src2 = torch.rand( | ||
batch_size, | ||
model_config["forecast_length"], | ||
model_config["input_size_encoder2"], | ||
) | ||
|
||
trg_start_token = torch.rand(batch_size, 1, model_config["output_size"]) | ||
|
||
# Execute the model | ||
outputs = model(src1, src2, trg_start_token) # Adjusted to pass parameters directly | ||
|
||
# Print outputs to verify | ||
print("Outputs shape:", outputs.shape) | ||
assert outputs.shape == ( | ||
batch_size, | ||
model_config["forecast_length"], | ||
model_config["output_size"], | ||
), "Output shape is incorrect" | ||
|
||
print("Test passed with output shape:", outputs.shape) |
This file contains 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 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,112 @@ | ||
""" | ||
Author: Wenyu Ouyang | ||
Date: 2024-04-17 12:55:24 | ||
LastEditTime: 2024-04-17 13:31:16 | ||
LastEditors: Xinzhuo Wu | ||
Description: | ||
FilePath: /torchhydro/tests/test_train_seq2seq.py | ||
Copyright (c) 2021-2024 Wenyu Ouyang. All rights reserved. | ||
""" | ||
|
||
import pytest | ||
from torchhydro.configs.config import cmd, default_config_file, update_cfg | ||
from torchhydro.trainers.trainer import train_and_evaluate, ensemble_train_and_evaluate | ||
|
||
import logging | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
for logger_name in logging.root.manager.loggerDict: | ||
logger = logging.getLogger(logger_name) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
@pytest.fixture() | ||
def config(): | ||
project_name = "test_mean_seq2seq/ex1" | ||
config_data = default_config_file() | ||
args = cmd( | ||
sub=project_name, | ||
source_cfgs={ | ||
"source": "HydroMean", | ||
"source_path": { | ||
"forcing": "basins-origin/hour_data/1h/mean_data/mean_data_forcing", | ||
"target": "basins-origin/hour_data/1h/mean_data/mean_data_target", | ||
"attributes": "basins-origin/attributes.nc", | ||
}, | ||
}, | ||
ctx=[1], | ||
model_name="Seq2Seq", | ||
model_hyperparam={ | ||
"input_size": 16, | ||
"output_size": 1, | ||
"hidden_size": 256, | ||
"cnn_size": 120, | ||
"forecast_length": 24, | ||
"model_mode": "dual", | ||
}, | ||
model_loader={"load_way": "best"}, | ||
gage_id=[ | ||
"21401550", | ||
], | ||
batch_size=256, | ||
rho=168, | ||
var_t=["gpm_tp", "gfs_tp"], | ||
var_c=[ | ||
"area", # 面积 | ||
"ele_mt_smn", # 海拔(空间平均) | ||
"slp_dg_sav", # 地形坡度 (空间平均) | ||
"sgr_dk_sav", # 河流坡度 (平均) | ||
"for_pc_sse", # 森林覆盖率 | ||
"glc_cl_smj", # 土地覆盖类型 | ||
"run_mm_syr", # 陆面径流 (流域径流的空间平均值) | ||
"inu_pc_slt", # 淹没范围 (长期最大) | ||
"cmi_ix_syr", # 气候湿度指数 | ||
"aet_mm_syr", # 实际蒸散发 (年平均) | ||
"snw_pc_syr", # 雪盖范围 (年平均) | ||
"swc_pc_syr", # 土壤水含量 | ||
"gwt_cm_sav", # 地下水位深度 | ||
"cly_pc_sav", # 土壤中的黏土、粉砂、砂粒含量 | ||
"dor_pc_pva", # 调节程度 | ||
], | ||
var_out=["streamflow"], | ||
dataset="MultiSourceDataset", | ||
sampler="HydroSampler", | ||
scaler="DapengScaler", | ||
train_epoch=1, | ||
save_epoch=1, | ||
train_period=[ | ||
("2017-07-01", "2017-09-29"), | ||
("2018-07-01", "2018-09-29"), | ||
("2019-07-01", "2019-09-29"), | ||
("2020-07-01", "2020-09-29"), | ||
], | ||
test_period=[ | ||
("2021-07-01", "2021-09-29"), | ||
], | ||
valid_period=[ | ||
("2021-07-01", "2021-09-29"), | ||
], | ||
loss_func="RMSESum", | ||
opt="Adam", | ||
lr_scheduler={ | ||
"lr": 0.001, | ||
"lr_factor": 0.96, | ||
}, | ||
which_first_tensor="batch", | ||
rolling=True, | ||
static=False, | ||
early_stopping=True, | ||
patience=4, | ||
ensemble=True, | ||
ensemble_items={ | ||
"kfold": 5, | ||
"batch_sizes": [256], | ||
}, | ||
) | ||
update_cfg(config_data, args) | ||
return config_data | ||
|
||
|
||
def test_seq2seq(config): | ||
# train_and_evaluate(config) | ||
ensemble_train_and_evaluate(config) |
This file contains 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 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 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 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
Oops, something went wrong.