-
Notifications
You must be signed in to change notification settings - Fork 168
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
Multispans #414
Open
Kazeya27
wants to merge
8
commits into
master
Choose a base branch
from
MultiSPANS
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Multispans #414
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9eca91
feat: MultiSPANS
Kazeya27 e0989bf
fix: normalization.py
Kazeya27 0da0e86
fix: executor
Kazeya27 3b742a6
Merge branch 'master' into MultiSPANS
Kazeya27 16141c5
fix: executor
Kazeya27 159a83d
fix: model
Kazeya27 aff8e83
Merge branch 'master' into MultiSPANS
Kazeya27 41ecd11
style: config
Kazeya27 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 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,35 @@ | ||
{ | ||
"gpu": true, | ||
"gpu_id": 0, | ||
"max_epoch": 100, | ||
"train_loss": "masked_mae", | ||
"epoch": 0, | ||
"learner": "adam", | ||
"learning_rate": 0.01, | ||
"weight_decay": 0, | ||
"lr_epsilon": 1e-8, | ||
"lr_beta1": 0.9, | ||
"lr_beta2": 0.999, | ||
"lr_alpha": 0.99, | ||
"lr_momentum": 0, | ||
"lr_decay": false, | ||
"lr_scheduler": "multisteplr", | ||
"lr_decay_ratio": 0.1, | ||
"steps": [5, 20, 40, 70], | ||
"step_size": 10, | ||
"lr_T_max": 30, | ||
"lr_eta_min": 0, | ||
"lr_patience": 10, | ||
"lr_threshold": 1e-4, | ||
"clip_grad_norm": false, | ||
"max_grad_norm": 1.0, | ||
"use_early_stop": false, | ||
"patience": 50, | ||
"log_level": "INFO", | ||
"log_every": 1, | ||
"saved_model": true, | ||
"load_best_epoch": true, | ||
"hyper_tune": false, | ||
"pred_channel_idx":[0], | ||
"outfeat_dim":1 | ||
} |
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,26 @@ | ||
{ | ||
"embed_dim":64, | ||
"skip_conv_flag" : false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. json配置文件的风格规范都统一一下,冒号前无空格,后面一空格,这个pr的所有json都检查一下 |
||
"residual_conv_flag" : false, | ||
"skip_dim":64, | ||
"num_layers":3, | ||
"num_heads": 8, | ||
|
||
"conv_kernels":[1,2,3,6], | ||
"conv_stride":1, | ||
"conv_if_gc":true, | ||
"norm_type":"BatchNorm", | ||
|
||
"gconv_hop_num" : 3, | ||
"gconv_alpha" : 0, | ||
|
||
"att_dropout":0.1, | ||
"ffn_dropout":0.1, | ||
"Satt_pe_type":"laplacian", | ||
"Spe_learnable":false, | ||
"Tatt_pe_type":"sincos", | ||
"Tpe_learnable":false, | ||
"Smask_flag":true, | ||
"block_forward_mode":0, | ||
"sstore_attn":false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import os | ||
import time | ||
from functools import partial | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from libcity.executor.traffic_state_executor import TrafficStateExecutor | ||
from libcity.model import loss | ||
|
||
|
||
class MultiSPANSExecutor(TrafficStateExecutor): | ||
def __init__(self, config, model, data_feature): | ||
super().__init__(config, model, data_feature) | ||
self.pred_channel_idx = self.config.get("pred_channel_idx", None) | ||
|
||
def _build_train_loss(self): | ||
""" | ||
根据全局参数`train_loss`选择训练过程的loss函数 | ||
如果该参数为none,则需要使用模型自定义的loss函数 | ||
注意,loss函数应该接收`Batch`对象作为输入,返回对应的loss(torch.tensor) | ||
""" | ||
if self.train_loss.lower() == 'none': | ||
self._logger.warning('Received none train loss func and will use the loss func defined in the model.') | ||
return None | ||
if self.train_loss.lower() not in ['mae', 'mse', 'rmse', 'mape', 'logcosh', 'huber', 'quantile', 'masked_mae', | ||
'masked_mse', 'masked_rmse', 'masked_mape', 'r2', 'evar']: | ||
self._logger.warning('Received unrecognized train loss function, set default mae loss func.') | ||
else: | ||
self._logger.info('You select `{}` as train loss function.'.format(self.train_loss.lower())) | ||
|
||
def func(batch, channel_index): | ||
y_true = batch['y'] | ||
y_predicted = self.model.predict(batch) | ||
y_true = self._scaler.inverse_transform(y_true[..., :self.output_dim]) | ||
y_predicted = self._scaler.inverse_transform(y_predicted[..., :self.output_dim], | ||
channel_idx=channel_index) | ||
if channel_index is not None: | ||
y_true = y_true[..., channel_index] | ||
assert (y_true.shape[-1] == y_predicted.shape[-1]), 'Uncompatiable prediction & label channel!' | ||
|
||
if self.train_loss.lower() == 'mae': | ||
lf = loss.masked_mae_torch | ||
elif self.train_loss.lower() == 'mse': | ||
lf = loss.masked_mse_torch | ||
elif self.train_loss.lower() == 'rmse': | ||
lf = loss.masked_rmse_torch | ||
elif self.train_loss.lower() == 'mape': | ||
lf = loss.masked_mape_torch | ||
elif self.train_loss.lower() == 'logcosh': | ||
lf = loss.log_cosh_loss | ||
elif self.train_loss.lower() == 'huber': | ||
lf = loss.huber_loss | ||
elif self.train_loss.lower() == 'quantile': | ||
lf = loss.quantile_loss | ||
elif self.train_loss.lower() == 'masked_mae': | ||
lf = partial(loss.masked_mae_torch, null_val=0) | ||
elif self.train_loss.lower() == 'masked_mse': | ||
lf = partial(loss.masked_mse_torch, null_val=0) | ||
elif self.train_loss.lower() == 'masked_rmse': | ||
lf = partial(loss.masked_rmse_torch, null_val=0) | ||
elif self.train_loss.lower() == 'masked_mape': | ||
lf = partial(loss.masked_mape_torch, null_val=0) | ||
elif self.train_loss.lower() == 'r2': | ||
lf = loss.r2_score_torch | ||
elif self.train_loss.lower() == 'evar': | ||
lf = loss.explained_variance_score_torch | ||
else: | ||
lf = loss.masked_mae_torch | ||
return lf(y_predicted, y_true) | ||
|
||
return func | ||
|
||
def evaluate(self, test_dataloader): | ||
""" | ||
use model to test data | ||
|
||
Args: | ||
test_dataloader(torch.Dataloader): Dataloader | ||
""" | ||
self._logger.info('Start evaluating ...') | ||
with torch.no_grad(): | ||
self.model.eval() | ||
y_truths = [] | ||
y_preds = [] | ||
for batch in test_dataloader: | ||
batch.to_tensor(self.device) | ||
output = self.model.predict(batch) | ||
y_true = batch['y'] | ||
y_true = self._scaler.inverse_transform(y_true[..., :self.output_dim]) | ||
y_pred = self._scaler.inverse_transform(output[..., :self.output_dim], | ||
channel_idx=self.pred_channel_idx) | ||
if self.pred_channel_idx is not None: | ||
y_true = y_true[..., self.pred_channel_idx] | ||
assert ( | ||
y_true.shape[-1] == output.shape[-1] | ||
), 'Uncompatiable prediction & label channel!' | ||
|
||
y_truths.append(y_true.cpu().numpy()) | ||
y_preds.append(y_pred.cpu().numpy()) | ||
# evaluate_input = {'y_true': y_true, 'y_pred': y_pred} | ||
# self.evaluator.collect(evaluate_input) | ||
# self.evaluator.save_result(self.evaluate_res_dir) | ||
y_preds = np.concatenate(y_preds, axis=0) | ||
y_truths = np.concatenate(y_truths, axis=0) # concatenate on batch | ||
outputs = {'prediction': y_preds, 'truth': y_truths} | ||
filename = \ | ||
time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime(time.time())) + '_' \ | ||
+ self.config['model'] + '_' + self.config['dataset'] + '_predictions.npz' | ||
np.savez_compressed(os.path.join(self.evaluate_res_dir, filename), **outputs) | ||
self.evaluator.clear() | ||
self.evaluator.collect({'y_true': torch.tensor(y_truths), 'y_pred': torch.tensor(y_preds)}) | ||
test_result = self.evaluator.save_result(self.evaluate_res_dir) | ||
return test_result | ||
|
||
def _train_epoch(self, train_dataloader, epoch_idx, loss_func=None): | ||
""" | ||
完成模型一个轮次的训练 | ||
|
||
Args: | ||
train_dataloader: 训练数据 | ||
epoch_idx: 轮次数 | ||
loss_func: 损失函数 | ||
|
||
Returns: | ||
list: 每个batch的损失的数组 | ||
""" | ||
self.model.train() | ||
loss_func = loss_func if loss_func is not None else self.model.calculate_loss | ||
losses = [] | ||
for batch in train_dataloader: | ||
self.optimizer.zero_grad() | ||
batch.to_tensor(self.device) | ||
loss = loss_func(batch, self.pred_channel_idx) | ||
self._logger.debug(loss.item()) | ||
losses.append(loss.item()) | ||
loss.backward() | ||
if self.clip_grad_norm: | ||
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.max_grad_norm) | ||
self.optimizer.step() | ||
return losses | ||
|
||
def _valid_epoch(self, eval_dataloader, epoch_idx, loss_func=None): | ||
""" | ||
完成模型一个轮次的评估 | ||
|
||
Args: | ||
eval_dataloader: 评估数据 | ||
epoch_idx: 轮次数 | ||
loss_func: 损失函数 | ||
|
||
Returns: | ||
float: 评估数据的平均损失值 | ||
""" | ||
with torch.no_grad(): | ||
self.model.eval() | ||
loss_func = loss_func if loss_func is not None else self.model.calculate_loss | ||
losses = [] | ||
for batch in eval_dataloader: | ||
batch.to_tensor(self.device) | ||
loss = loss_func(batch, self.pred_channel_idx) | ||
self._logger.debug(loss.item()) | ||
losses.append(loss.item()) | ||
mean_loss = np.mean(losses) | ||
self._writer.add_scalar('eval loss', mean_loss, epoch_idx) | ||
return mean_loss |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
和以前的output_dim的区别是什么?