This commit is contained in:
czzhangheng 2025-11-27 08:36:07 +08:00
parent 4019645f5d
commit c22d946393
5 changed files with 76 additions and 22 deletions

8
.vscode/launch.json vendored
View File

@ -5,6 +5,14 @@
"version": "0.2.0",
"configurations": [
{
"name": "DDGCRN: METR-LA",
"type": "debugpy",
"request": "launch",
"program": "run.py",
"console": "integratedTerminal",
"args": "--config ./config/DDGCRN/METR-LA.yaml"
},
// STID
{
"name": "STID: PEMS-BAY",

59
config/DDGCRN/METR-LA.yaml Executable file
View File

@ -0,0 +1,59 @@
basic:
dataset: "METR-LA"
mode: "train"
device: "cuda:0"
model: "DDGCRN"
seed: 2023
data:
add_day_in_week: true
add_time_in_day: true
column_wise: false
days_per_week: 7
default_graph: true
horizon: 24
lag: 24
normalizer: std
num_nodes: 207
steps_per_day: 288
test_ratio: 0.2
tod: false
val_ratio: 0.2
sample: 1
input_dim: 1
batch_size: 64
model:
input_dim: 1
output_dim: 1
embed_dim: 12
rnn_units: 64
num_layers: 1
cheb_order: 2
use_day: True
use_week: False
num_nodes: 207
horizon: 24
train:
loss_func: mask_mae
seed: 10
batch_size: 64
epochs: 100
lr_init: 0.003
weight_decay: 0
lr_decay: False
lr_decay_rate: 0.3
lr_decay_step: "5,20,40,70"
early_stop: True
early_stop_patience: 15
grad_norm: False
max_grad_norm: 5
debug: false
mae_thresh: 0.0
mape_thresh: 0.0
log_step: 10000
plot: False
output_dim: 1

View File

@ -203,7 +203,7 @@ class Trainer:
self.stats.record_step_time(step_time, mode)
# 累积损失和预测结果
total_loss += loss.item()
total_loss += d_loss.item()
y_pred.append(d_output.detach().cpu())
y_true.append(d_label.detach().cpu())

View File

@ -84,9 +84,10 @@ def init_device(args):
def init_loss(args, scaler):
device = args["basic"]["device"]
args = args["train"]
mask_v = args['mae_thresh']
match args["loss_func"]:
case "mask_mae":
return masked_mae_loss(scaler, mask_value=None)
return masked_mae_loss(scaler, mask_value=mask_v)
case "mae":
return torch.nn.L1Loss().to(device)
case "mse":

View File

@ -1,30 +1,16 @@
import torch
def masked_mae_loss(scaler, mask_value):
def loss(preds, labels):
# 仅对预测反归一化;标签在数据管道中保持原始量纲
if scaler:
preds = scaler.inverse_transform(preds)
# # 仅对预测反归一化;标签在数据管道中保持原始量纲
# if scaler:
# preds = scaler.inverse_transform(preds)
return mae_torch(pred=preds, true=labels, mask_value=mask_value)
return loss
def get_loss_function(args, scaler):
if args["loss_func"] == "mask_mae":
# Return callable loss (no .to for function closures); disable masking by default
return masked_mae_loss(scaler, mask_value=None)
elif args["loss_func"] == "mae":
return torch.nn.L1Loss().to(args["device"])
elif args["loss_func"] == "mse":
return torch.nn.MSELoss().to(args["device"])
elif args["loss_func"] == "Huber":
return torch.nn.HuberLoss().to(args["device"])
else:
raise ValueError("Unsupported loss function: {}".format(args.loss_func))
import torch
def mae_torch(pred, true, mask_value=None):
if mask_value is not None:
mask = torch.gt(true, mask_value)