From 6fb19eb58d4f5dda1cdac47c3ace0a92bfc47de5 Mon Sep 17 00:00:00 2001 From: lwwang1995 Date: Fri, 27 Nov 2020 10:09:17 +0800 Subject: [PATCH] Update training setting. --- .../ALSTM/workflow_config_alstm.yaml | 18 ++++++++++++++++-- .../benchmarks/GRU/workflow_config_gru.yaml | 18 ++++++++++++++++-- .../benchmarks/HATS/worflow_config_hats.yaml | 18 ++++++++++++++++-- .../benchmarks/LSTM/workflow_config_lstm.yaml | 18 ++++++++++++++++-- .../benchmarks/SFM/workflow_config_sfm.yaml | 16 +++++++++++++++- qlib/contrib/model/pytorch_alstm.py | 9 ++------- qlib/contrib/model/pytorch_gats.py | 13 ++++--------- qlib/contrib/model/pytorch_gru.py | 1 + qlib/contrib/model/pytorch_hats.py | 11 +++-------- qlib/contrib/model/pytorch_lstm.py | 7 +------ qlib/contrib/model/pytorch_sfm.py | 12 +----------- 11 files changed, 91 insertions(+), 50 deletions(-) diff --git a/examples/benchmarks/ALSTM/workflow_config_alstm.yaml b/examples/benchmarks/ALSTM/workflow_config_alstm.yaml index bb35b6da5..dd57761f3 100644 --- a/examples/benchmarks/ALSTM/workflow_config_alstm.yaml +++ b/examples/benchmarks/ALSTM/workflow_config_alstm.yaml @@ -8,6 +8,20 @@ data_handler_config: &data_handler_config fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market + infer_processors: + - class: RobustZScoreNorm + kwargs: + fields_group: feature + clip_outlier: true + - class: Fillna + kwargs: + fields_group: feature + learn_processors: + - class: DropnaLabel + - class: CSRankNorm + kwargs: + fields_group: label + label: ["Ref($close, -2) / Ref($close, -1) - 1"] port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy @@ -37,7 +51,7 @@ task: lr: 1e-3 early_stop: 20 batch_size: 800 - metric: IC + metric: loss loss: mse seed: 0 GPU: 0 @@ -47,7 +61,7 @@ task: module_path: qlib.data.dataset kwargs: handler: - class: ALPHA360_Denoise + class: ALPHA360 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: diff --git a/examples/benchmarks/GRU/workflow_config_gru.yaml b/examples/benchmarks/GRU/workflow_config_gru.yaml index e9e6224e6..bdfcd4e55 100644 --- a/examples/benchmarks/GRU/workflow_config_gru.yaml +++ b/examples/benchmarks/GRU/workflow_config_gru.yaml @@ -8,6 +8,20 @@ data_handler_config: &data_handler_config fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market + infer_processors: + - class: RobustZScoreNorm + kwargs: + fields_group: feature + clip_outlier: true + - class: Fillna + kwargs: + fields_group: feature + learn_processors: + - class: DropnaLabel + - class: CSRankNorm + kwargs: + fields_group: label + label: ["Ref($close, -2) / Ref($close, -1) - 1"] port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy @@ -37,7 +51,7 @@ task: lr: 1e-3 early_stop: 20 batch_size: 800 - metric: IC + metric: loss loss: mse seed: 0 GPU: 0 @@ -46,7 +60,7 @@ task: module_path: qlib.data.dataset kwargs: handler: - class: ALPHA360_Denoise + class: ALPHA360 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: diff --git a/examples/benchmarks/HATS/worflow_config_hats.yaml b/examples/benchmarks/HATS/worflow_config_hats.yaml index 0abed6c62..b08df14e0 100644 --- a/examples/benchmarks/HATS/worflow_config_hats.yaml +++ b/examples/benchmarks/HATS/worflow_config_hats.yaml @@ -8,6 +8,20 @@ data_handler_config: &data_handler_config fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market + infer_processors: + - class: RobustZScoreNorm + kwargs: + fields_group: feature + clip_outlier: true + - class: Fillna + kwargs: + fields_group: feature + learn_processors: + - class: DropnaLabel + - class: CSRankNorm + kwargs: + fields_group: label + label: ["Ref($close, -2) / Ref($close, -1) - 1"] port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy @@ -36,7 +50,7 @@ task: n_epochs: 200 lr: 1e-3 early_stop: 20 - metric: IC + metric: loss loss: mse base_model: GRU seed: 0 @@ -46,7 +60,7 @@ task: module_path: qlib.data.dataset kwargs: handler: - class: ALPHA360_Denoise + class: ALPHA360 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: diff --git a/examples/benchmarks/LSTM/workflow_config_lstm.yaml b/examples/benchmarks/LSTM/workflow_config_lstm.yaml index 354149dae..6512a0df3 100644 --- a/examples/benchmarks/LSTM/workflow_config_lstm.yaml +++ b/examples/benchmarks/LSTM/workflow_config_lstm.yaml @@ -8,6 +8,20 @@ data_handler_config: &data_handler_config fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market + infer_processors: + - class: RobustZScoreNorm + kwargs: + fields_group: feature + clip_outlier: true + - class: Fillna + kwargs: + fields_group: feature + learn_processors: + - class: DropnaLabel + - class: CSRankNorm + kwargs: + fields_group: label + label: ["Ref($close, -2) / Ref($close, -1) - 1"] port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy @@ -37,7 +51,7 @@ task: lr: 1e-3 early_stop: 20 batch_size: 800 - metric: IC + metric: loss loss: mse seed: 0 GPU: 0 @@ -46,7 +60,7 @@ task: module_path: qlib.data.dataset kwargs: handler: - class: ALPHA360_Denoise + class: ALPHA360 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: diff --git a/examples/benchmarks/SFM/workflow_config_sfm.yaml b/examples/benchmarks/SFM/workflow_config_sfm.yaml index 04f796150..59596760f 100644 --- a/examples/benchmarks/SFM/workflow_config_sfm.yaml +++ b/examples/benchmarks/SFM/workflow_config_sfm.yaml @@ -8,6 +8,20 @@ data_handler_config: &data_handler_config fit_start_time: 2008-01-01 fit_end_time: 2014-12-31 instruments: *market + infer_processors: + - class: RobustZScoreNorm + kwargs: + fields_group: feature + clip_outlier: true + - class: Fillna + kwargs: + fields_group: feature + learn_processors: + - class: DropnaLabel + - class: CSRankNorm + kwargs: + fields_group: label + label: ["Ref($close, -2) / Ref($close, -1) - 1"] port_analysis_config: &port_analysis_config strategy: class: TopkDropoutStrategy @@ -51,7 +65,7 @@ task: module_path: qlib.data.dataset kwargs: handler: - class: ALPHA360_Denoise + class: ALPHA360 module_path: qlib.contrib.data.handler kwargs: *data_handler_config segments: diff --git a/qlib/contrib/model/pytorch_alstm.py b/qlib/contrib/model/pytorch_alstm.py index 065963e73..7b4943db2 100644 --- a/qlib/contrib/model/pytorch_alstm.py +++ b/qlib/contrib/model/pytorch_alstm.py @@ -44,7 +44,7 @@ class ALSTM(Model): dropout=0.0, n_epochs=200, lr=0.001, - metric="IC", + metric="", batch_size=2000, early_stop=20, loss="mse", @@ -142,21 +142,16 @@ class ALSTM(Model): def metric_fn(self, pred, label): mask = torch.isfinite(label) - if self.metric == "IC": - return self.cal_ic(pred[mask], label[mask]) if self.metric == "" or self.metric == "loss": # use loss return -self.loss_fn(pred[mask], label[mask]) raise ValueError("unknown metric `%s`" % self.metric) - def cal_ic(self, pred, label): - return torch.mean(pred * label) - def train_epoch(self, x_train, y_train): x_train_values = x_train.values - y_train_values = np.squeeze(y_train.values) * 100 + y_train_values = np.squeeze(y_train.values) self.alstm_model.train() diff --git a/qlib/contrib/model/pytorch_gats.py b/qlib/contrib/model/pytorch_gats.py index d951f1873..77a02a9b2 100755 --- a/qlib/contrib/model/pytorch_gats.py +++ b/qlib/contrib/model/pytorch_gats.py @@ -43,13 +43,13 @@ class GAT(Model): d_feat=6, hidden_size=64, num_layers=2, - dropout=0.7, + dropout=0.0, n_epochs=200, - lr=0.0001, - metric="loss", + lr=0.001, + metric="", early_stop=20, loss="mse", - base_model="LSTM", + base_model="GRU", with_pretrain=True, optimizer="adam", GPU="0", @@ -148,17 +148,12 @@ class GAT(Model): def metric_fn(self, pred, label): mask = torch.isfinite(label) - if self.metric == "IC": - return self.cal_ic(pred[mask], label[mask]) if self.metric == "" or self.metric == "loss": # use loss return -self.loss_fn(pred[mask], label[mask]) raise ValueError("unknown metric `%s`" % self.metric) - def cal_ic(self, pred, label): - return torch.mean(pred * label) - def get_daily_inter(self, df, shuffle=False): # organize the train data into daily inter as daily batches daily_count = df.groupby(level=0).size().values diff --git a/qlib/contrib/model/pytorch_gru.py b/qlib/contrib/model/pytorch_gru.py index 02664b6ac..282ce72dd 100755 --- a/qlib/contrib/model/pytorch_gru.py +++ b/qlib/contrib/model/pytorch_gru.py @@ -146,6 +146,7 @@ class GRU(Model): raise ValueError("unknown metric `%s`" % self.metric) + def train_epoch(self, x_train, y_train): x_train_values = x_train.values diff --git a/qlib/contrib/model/pytorch_hats.py b/qlib/contrib/model/pytorch_hats.py index a0da88dbf..7affea73c 100644 --- a/qlib/contrib/model/pytorch_hats.py +++ b/qlib/contrib/model/pytorch_hats.py @@ -52,11 +52,11 @@ class HATS(Model): num_layers=2, dropout=0.5, n_epochs=200, - lr=0.0001, - metric="loss", + lr=0.01, + metric="", early_stop=20, loss="mse", - base_model="LSTM", + base_model="GRU", with_pretrain=True, optimizer="adam", GPU="0", @@ -154,17 +154,12 @@ class HATS(Model): def metric_fn(self, pred, label): mask = torch.isfinite(label) - if self.metric == "IC": - return self.cal_ic(pred[mask], label[mask]) if self.metric == "" or self.metric == "loss": # use loss return -self.loss_fn(pred[mask], label[mask]) raise ValueError("unknown metric `%s`" % self.metric) - def cal_ic(self, pred, label): - return torch.mean(pred * label) - def get_daily_inter(self, df, shuffle=False): # organize the train data into daily inter as daily batches daily_count = df.groupby(level=0).size().values diff --git a/qlib/contrib/model/pytorch_lstm.py b/qlib/contrib/model/pytorch_lstm.py index 22cc21e7f..844a08a83 100755 --- a/qlib/contrib/model/pytorch_lstm.py +++ b/qlib/contrib/model/pytorch_lstm.py @@ -46,7 +46,7 @@ class LSTM(Model): dropout=0.0, n_epochs=200, lr=0.001, - metric="loss", + metric="", batch_size=2000, early_stop=20, loss="mse", @@ -140,16 +140,12 @@ class LSTM(Model): def metric_fn(self, pred, label): mask = torch.isfinite(label) - if self.metric == "IC": - return self.cal_ic(pred[mask], label[mask]) if self.metric == "" or self.metric == "loss": # use loss return -self.loss_fn(pred[mask], label[mask]) raise ValueError("unknown metric `%s`" % self.metric) - def cal_ic(self, pred, label): - return torch.mean(pred * label) def train_epoch(self, x_train, y_train): @@ -193,7 +189,6 @@ class LSTM(Model): losses = [] indices = np.arange(len(x_values)) - np.random.shuffle(indices) for i in range(len(indices))[:: self.batch_size]: diff --git a/qlib/contrib/model/pytorch_sfm.py b/qlib/contrib/model/pytorch_sfm.py index 4fbe12fb7..9e897a246 100644 --- a/qlib/contrib/model/pytorch_sfm.py +++ b/qlib/contrib/model/pytorch_sfm.py @@ -102,7 +102,7 @@ class SFM_Model(nn.Module): i = self.inner_activation( x_i + torch.matmul(h_tm1 * B_U[0], self.U_i) - ) + ) # not sure whether I am doing in the right unsquuze ste = self.inner_activation(x_ste + torch.matmul(h_tm1 * B_U[0], self.U_ste)) fre = self.inner_activation(x_fre + torch.matmul(h_tm1 * B_U[0], self.U_fre)) @@ -283,10 +283,6 @@ class SFM(Model): ) ) - if loss not in {"mse", "binary"}: - raise NotImplementedError("loss {} is not supported!".format(loss)) - self._scorer = mean_squared_error if loss == "mse" else roc_auc_score - self.sfm_model = SFM_Model( d_feat=self.d_feat, output_dim=self.output_dim, @@ -318,7 +314,6 @@ class SFM(Model): losses = [] indices = np.arange(len(x_values)) - np.random.shuffle(indices) for i in range(len(indices))[:: self.batch_size]: @@ -428,17 +423,12 @@ class SFM(Model): def metric_fn(self, pred, label): mask = torch.isfinite(label) - if self.metric == "IC": - return self.cal_ic(pred[mask], label[mask]) if self.metric == "" or self.metric == "loss": # use loss return -self.loss_fn(pred[mask], label[mask]) raise ValueError("unknown metric `%s`" % self.metric) - def cal_ic(self, pred, label): - return torch.mean(pred * label) - def predict(self, dataset): if not self._fitted: raise ValueError("model is not fitted yet!")