mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-18 18:04:31 +08:00
Add SFM config
This commit is contained in:
@@ -198,6 +198,7 @@ Here is a list of models built on `Qlib`.
|
||||
- [LSTM based on pytorcn](qlib/contrib/model/pytorch_lstm.py)
|
||||
- [GATs based on pytorch](qlib/contrib/model/pytorch_gats.py)
|
||||
- [TabNet based on pytorch](qlib/contrib/model/tabnet.py)
|
||||
- [SFM based on pytorch](qlib/contrib/model/pytorch_sfm.py)
|
||||
<!-- - [TFT based on tensorflow](examples/benchmarks/TFT/tft.py) -->
|
||||
|
||||
Your PR of new Quant models is highly welcomed.
|
||||
|
||||
4
examples/benchmarks/SFM/requirements.txt
Normal file
4
examples/benchmarks/SFM/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
pandas==1.1.2
|
||||
numpy==1.17.4
|
||||
scikit_learn==0.23.2
|
||||
torch==1.7.0
|
||||
73
examples/benchmarks/SFM/workflow_config_sfm.yaml
Normal file
73
examples/benchmarks/SFM/workflow_config_sfm.yaml
Normal file
@@ -0,0 +1,73 @@
|
||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||
region: cn
|
||||
market: &market csi300
|
||||
benchmark: &benchmark SH000300
|
||||
data_handler_config: &data_handler_config
|
||||
start_time: 2008-01-01
|
||||
end_time: 2020-08-01
|
||||
fit_start_time: 2008-01-01
|
||||
fit_end_time: 2014-12-31
|
||||
instruments: *market
|
||||
port_analysis_config: &port_analysis_config
|
||||
strategy:
|
||||
class: TopkDropoutStrategy
|
||||
module_path: qlib.contrib.strategy.strategy
|
||||
kwargs:
|
||||
topk: 50
|
||||
n_drop: 5
|
||||
backtest:
|
||||
verbose: False
|
||||
limit_threshold: 0.095
|
||||
account: 100000000
|
||||
benchmark: *benchmark
|
||||
deal_price: close
|
||||
open_cost: 0.0005
|
||||
close_cost: 0.0015
|
||||
min_cost: 5
|
||||
task:
|
||||
model:
|
||||
class: SFM
|
||||
module_path: qlib.contrib.model.pytorch_sfm
|
||||
kwargs:
|
||||
d_feat: 6
|
||||
hidden_size: 64
|
||||
output_dim: 1
|
||||
freq_dim: 15
|
||||
dropout_W: 0.5
|
||||
dropout_U: 0.5
|
||||
n_epochs: 10
|
||||
lr: 1e-3
|
||||
batch_size: 800
|
||||
early_stop: 20
|
||||
eval_steps: 5
|
||||
loss: mse
|
||||
lr_decay: 0.96
|
||||
lr_decay_steps: 100
|
||||
optimizer: gd
|
||||
GPU: 1
|
||||
seed: 0
|
||||
dataset:
|
||||
class: DatasetH
|
||||
module_path: qlib.data.dataset
|
||||
kwargs:
|
||||
handler:
|
||||
class: ALPHA360_Denoise
|
||||
module_path: qlib.contrib.data.handler
|
||||
kwargs: *data_handler_config
|
||||
segments:
|
||||
train: [2008-01-01, 2014-12-31]
|
||||
valid: [2015-01-01, 2016-12-31]
|
||||
test: [2017-01-01, 2020-08-01]
|
||||
record:
|
||||
- class: SignalRecord
|
||||
module_path: qlib.workflow.record_temp
|
||||
kwargs: {}
|
||||
- class: SigAnaRecord
|
||||
module_path: qlib.workflow.record_temp
|
||||
kwargs:
|
||||
ana_long_short: False
|
||||
ann_scaler: 252
|
||||
- class: PortAnaRecord
|
||||
module_path: qlib.workflow.record_temp
|
||||
kwargs:
|
||||
config: *port_analysis_config
|
||||
@@ -21,6 +21,7 @@ from ...model.base import Model
|
||||
from ...data.dataset import DatasetH
|
||||
from ...data.dataset.handler import DataHandlerLP
|
||||
|
||||
|
||||
class SFM_Model(nn.Module):
|
||||
def __init__(self, d_feat=6, output_dim=1, freq_dim=10, hidden_size=64, dropout_W=0.0, dropout_U=0.0, device="cpu"):
|
||||
super().__init__()
|
||||
@@ -71,7 +72,7 @@ class SFM_Model(nn.Module):
|
||||
|
||||
for ts in range(time_step):
|
||||
x = input[:, ts, :]
|
||||
if(len(self.states)==0): #hasn't initialized yet
|
||||
if len(self.states) == 0: # hasn't initialized yet
|
||||
self.init_states(x)
|
||||
self.get_constants(x)
|
||||
p_tm1 = self.states[0]
|
||||
@@ -89,8 +90,9 @@ class SFM_Model(nn.Module):
|
||||
x_c = torch.matmul(x * B_W[0], self.W_c) + self.b_c
|
||||
x_o = torch.matmul(x * B_W[0], self.W_o) + self.b_o
|
||||
|
||||
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
|
||||
|
||||
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))
|
||||
@@ -152,13 +154,14 @@ class SFM_Model(nn.Module):
|
||||
|
||||
def get_constants(self, x):
|
||||
constants = []
|
||||
constants.append([torch.tensor(1.).to(self.device) for _ in range(6)])
|
||||
constants.append([torch.tensor(1.).to(self.device) for _ in range(7)])
|
||||
constants.append([torch.tensor(1.0).to(self.device) for _ in range(6)])
|
||||
constants.append([torch.tensor(1.0).to(self.device) for _ in range(7)])
|
||||
array = np.array([float(ii) / self.freq_dim for ii in range(self.freq_dim)])
|
||||
constants.append(torch.tensor(array).to(self.device))
|
||||
|
||||
self.states[5:] = constants
|
||||
|
||||
|
||||
class SFM(Model):
|
||||
"""SFM Model
|
||||
|
||||
@@ -221,7 +224,7 @@ class SFM(Model):
|
||||
self.lr_decay_steps = lr_decay_steps
|
||||
self.optimizer = optimizer.lower()
|
||||
self.loss_type = loss
|
||||
self.device = 'cuda:%d'%(GPU) if torch.cuda.is_available() else 'cpu'
|
||||
self.device = "cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu"
|
||||
self.use_gpu = torch.cuda.is_available()
|
||||
self.seed = seed
|
||||
|
||||
@@ -275,7 +278,7 @@ class SFM(Model):
|
||||
freq_dim=self.freq_dim,
|
||||
dropout_W=self.dropout_W,
|
||||
dropout_U=self.dropout_U,
|
||||
device = self.device
|
||||
device=self.device,
|
||||
)
|
||||
if optimizer.lower() == "adam":
|
||||
self.train_optimizer = optim.Adam(self.sfm_model.parameters(), lr=self.lr)
|
||||
@@ -301,14 +304,7 @@ class SFM(Model):
|
||||
self._fitted = False
|
||||
self.sfm_model.to(self.device)
|
||||
|
||||
def fit(
|
||||
self,
|
||||
dataset: DatasetH,
|
||||
evals_result=dict(),
|
||||
verbose=True,
|
||||
save_path=None,
|
||||
**kwargs
|
||||
):
|
||||
def fit(self, dataset: DatasetH, evals_result=dict(), verbose=True, save_path=None, **kwargs):
|
||||
|
||||
df_train, df_valid = dataset.prepare(
|
||||
["train", "valid"], col_set=["feature", "label"], data_key=DataHandlerLP.DK_L
|
||||
@@ -398,7 +394,7 @@ class SFM(Model):
|
||||
# update learning rate
|
||||
self.scheduler.step(cur_loss_val)
|
||||
|
||||
if self.device != 'cpu':
|
||||
if self.device != "cpu":
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
def get_loss(self, pred, target, loss_type):
|
||||
@@ -424,7 +420,7 @@ class SFM(Model):
|
||||
self.sfm_model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
if self.device != 'cpu':
|
||||
if self.device != "cpu":
|
||||
preds = self.sfm_model(x_test).detach().cpu().numpy()
|
||||
else:
|
||||
preds = self.sfm_model(x_test).detach().numpy()
|
||||
@@ -447,8 +443,10 @@ class SFM(Model):
|
||||
self.sfm_model.load_state_dict(torch.load(_model_path))
|
||||
self._fitted = True
|
||||
|
||||
|
||||
class AverageMeter(object):
|
||||
"""Computes and stores the average and current value"""
|
||||
|
||||
def __init__(self):
|
||||
self.reset()
|
||||
|
||||
|
||||
@@ -23,3 +23,4 @@ torch==1.6.0
|
||||
tqdm==4.49.0
|
||||
yahooquery==2.2.7
|
||||
mlflow==1.12.1
|
||||
pytorch-tabnet==2.0.1
|
||||
Reference in New Issue
Block a user