1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-12 07:16:54 +08:00

Merge pull request #74 from Derek-Wds/main

Update scripts
This commit is contained in:
you-n-g
2020-12-04 14:30:42 +08:00
committed by GitHub
26 changed files with 122 additions and 194 deletions

View File

@@ -27,6 +27,7 @@ jobs:
run: | run: |
pip install --upgrade cython pip install --upgrade cython
pip install numpy jupyter jupyter_contrib_nbextensions pip install numpy jupyter jupyter_contrib_nbextensions
pip install -U scipy scikit-learn # installing without this line will cause errors on GitHub Actions, while instsalling locally won't
python setup.py install python setup.py install
- name: Install Lightgbm for MacOS - name: Install Lightgbm for MacOS
@@ -56,4 +57,4 @@ jobs:
- name: Test workflow by config - name: Test workflow by config
run: | run: |
qrun examples/benchmarks/LightGBM/workflow_config_lightgbm.yaml qrun examples/benchmarks/LightGBM/workflow_config_lightgbm_Alpha158.yaml

View File

@@ -114,7 +114,7 @@ Version 0.4.1
Version 0.4.2 Version 0.4.2
-------------------- --------------------
- Refactor DataHandler - Refactor DataHandler
- Add ``ALPHA360`` DataHandler - Add ``Alpha360`` DataHandler
Version 0.4.3 Version 0.4.3

View File

@@ -153,9 +153,6 @@ Qlib provides a tool named `qrun` to run the whole workflow automatically (inclu
annualized_return 0.128982 annualized_return 0.128982
information_ratio 1.444287 information_ratio 1.444287
max_drawdown -0.091078 max_drawdown -0.091078
``` ```
Here are detailed documents for `qrun` and [workflow](https://qlib.readthedocs.io/en/latest/component/workflow.html). Here are detailed documents for `qrun` and [workflow](https://qlib.readthedocs.io/en/latest/component/workflow.html).
@@ -218,7 +215,7 @@ All the models listed above are runnable with ``Qlib``. Users can find the confi
## Run multiple models ## Run multiple models
`Qlib` also provides a script [`run_all_model.py`](examples/run_all_model.py) which can run multiple models for several iterations. (**Note**: the script only supprots *Linux* now. Other OS will be supported in the future.) `Qlib` also provides a script [`run_all_model.py`](examples/run_all_model.py) which can run multiple models for several iterations. (**Note**: the script only supprots *Linux* now. Other OS will be supported in the future.)
The script will create a unique virtual environment for each model, and delete the environments after training. Thus, only experiment results such as `IC` and `backtest` results will be generated and stored. (**Note**: the script will erase your previous experiment records created by running itself.) The script will create a unique virtual environment for each model, and delete the environments after training. Thus, only experiment results such as `IC` and `backtest` results will be generated and stored.
Here is an example of running all the models for 10 iterations: Here is an example of running all the models for 10 iterations:
```python ```python

View File

@@ -54,7 +54,6 @@ task:
batch_size: 800 batch_size: 800
metric: loss metric: loss
loss: mse loss: mse
seed: 0
GPU: 0 GPU: 0
rnn_type: GRU rnn_type: GRU
dataset: dataset:
@@ -62,7 +61,7 @@ task:
module_path: qlib.data.dataset module_path: qlib.data.dataset
kwargs: kwargs:
handler: handler:
class: ALPHA360 class: Alpha360
module_path: qlib.contrib.data.handler module_path: qlib.contrib.data.handler
kwargs: *data_handler_config kwargs: *data_handler_config
segments: segments:

View File

@@ -56,14 +56,13 @@ task:
base_model: LSTM base_model: LSTM
with_pretrain: True with_pretrain: True
model_path: "benchmarks/LSTM/model_lstm_csi300.pkl" model_path: "benchmarks/LSTM/model_lstm_csi300.pkl"
seed: 0
GPU: 0 GPU: 0
dataset: dataset:
class: DatasetH class: DatasetH
module_path: qlib.data.dataset module_path: qlib.data.dataset
kwargs: kwargs:
handler: handler:
class: ALPHA360 class: Alpha360
module_path: qlib.contrib.data.handler module_path: qlib.contrib.data.handler
kwargs: *data_handler_config kwargs: *data_handler_config
segments: segments:
@@ -74,6 +73,11 @@ task:
- class: SignalRecord - class: SignalRecord
module_path: qlib.workflow.record_temp module_path: qlib.workflow.record_temp
kwargs: {} kwargs: {}
- class: SigAnaRecord
module_path: qlib.workflow.record_temp
kwargs:
ana_long_short: False
ann_scaler: 252
- class: PortAnaRecord - class: PortAnaRecord
module_path: qlib.workflow.record_temp module_path: qlib.workflow.record_temp
kwargs: kwargs:

View File

@@ -54,14 +54,13 @@ task:
batch_size: 800 batch_size: 800
metric: loss metric: loss
loss: mse loss: mse
seed: 0
GPU: 0 GPU: 0
dataset: dataset:
class: DatasetH class: DatasetH
module_path: qlib.data.dataset module_path: qlib.data.dataset
kwargs: kwargs:
handler: handler:
class: ALPHA360 class: Alpha360
module_path: qlib.contrib.data.handler module_path: qlib.contrib.data.handler
kwargs: *data_handler_config kwargs: *data_handler_config
segments: segments:

View File

@@ -54,14 +54,13 @@ task:
batch_size: 800 batch_size: 800
metric: loss metric: loss
loss: mse loss: mse
seed: 0
GPU: 0 GPU: 0
dataset: dataset:
class: DatasetH class: DatasetH
module_path: qlib.data.dataset module_path: qlib.data.dataset
kwargs: kwargs:
handler: handler:
class: ALPHA360 class: Alpha360
module_path: qlib.contrib.data.handler module_path: qlib.contrib.data.handler
kwargs: *data_handler_config kwargs: *data_handler_config
segments: segments:

View File

@@ -32,7 +32,7 @@ task:
kwargs: kwargs:
loss: mse loss: mse
colsample_bytree: 0.8879 colsample_bytree: 0.8879
learning_rate: 0.0421 learning_rate: 0.2
subsample: 0.8789 subsample: 0.8789
lambda_l1: 205.6999 lambda_l1: 205.6999
lambda_l2: 580.9768 lambda_l2: 580.9768

View File

@@ -57,14 +57,13 @@ task:
eval_steps: 5 eval_steps: 5
loss: mse loss: mse
optimizer: adam optimizer: adam
GPU: 1 GPU: 0
seed: 710
dataset: dataset:
class: DatasetH class: DatasetH
module_path: qlib.data.dataset module_path: qlib.data.dataset
kwargs: kwargs:
handler: handler:
class: ALPHA360 class: Alpha360
module_path: qlib.contrib.data.handler module_path: qlib.contrib.data.handler
kwargs: *data_handler_config kwargs: *data_handler_config
segments: segments:

View File

@@ -208,7 +208,7 @@ class Alpha158Formatter(GenericDataFormatter):
model_params = { model_params = {
"dropout_rate": 0.4, "dropout_rate": 0.4,
"hidden_layer_size": 16, "hidden_layer_size": 160,
"learning_rate": 0.0001, "learning_rate": 0.0001,
"minibatch_size": 128, "minibatch_size": 128,
"max_gradient_norm": 0.0135, "max_gradient_norm": 0.0135,

View File

@@ -15,6 +15,7 @@ import traceback
import functools import functools
import statistics import statistics
import subprocess import subprocess
from datetime import datetime
from pathlib import Path from pathlib import Path
from operator import xor from operator import xor
from pprint import pprint from pprint import pprint
@@ -45,8 +46,6 @@ if not exists_qlib_data(provider_uri):
GetData().qlib_data(target_dir=provider_uri, region=REG_CN) GetData().qlib_data(target_dir=provider_uri, region=REG_CN)
qlib.init(provider_uri=provider_uri, region=REG_CN, exp_manager=exp_manager) qlib.init(provider_uri=provider_uri, region=REG_CN, exp_manager=exp_manager)
if os.path.isdir(exp_path):
shutil.rmtree(exp_path)
# decorator to check the arguments # decorator to check the arguments
def only_allow_defined_args(function_to_decorate): def only_allow_defined_args(function_to_decorate):
@@ -136,9 +135,9 @@ def get_all_folders(models, exclude) -> dict:
# function to get all the files under the model folder # function to get all the files under the model folder
def get_all_files(folder_path) -> (str, str): def get_all_files(folder_path, dataset) -> (str, str):
yaml_path = str(Path(f"{folder_path}") / "*.yaml") yaml_path = str(Path(f"{folder_path}") / f"*{dataset}*.yaml")
req_path = str(Path(f"{folder_path}") / "*.txt") req_path = str(Path(f"{folder_path}") / f"*.txt")
return glob.glob(yaml_path)[0], glob.glob(req_path)[0] return glob.glob(yaml_path)[0], glob.glob(req_path)[0]
@@ -152,6 +151,10 @@ def get_all_results(folders) -> dict:
result["annualized_return_with_cost"] = list() result["annualized_return_with_cost"] = list()
result["information_ratio_with_cost"] = list() result["information_ratio_with_cost"] = list()
result["max_drawdown_with_cost"] = list() result["max_drawdown_with_cost"] = list()
result["ic"] = list()
result["icir"] = list()
result["rank_ic"] = list()
result["rank_icir"] = list()
for recorder_id in recorders: for recorder_id in recorders:
if recorders[recorder_id].status == "FINISHED": if recorders[recorder_id].status == "FINISHED":
recorder = R.get_recorder(recorder_id=recorder_id, experiment_name=fn) recorder = R.get_recorder(recorder_id=recorder_id, experiment_name=fn)
@@ -159,19 +162,27 @@ def get_all_results(folders) -> dict:
result["annualized_return_with_cost"].append(metrics["excess_return_with_cost.annualized_return"]) result["annualized_return_with_cost"].append(metrics["excess_return_with_cost.annualized_return"])
result["information_ratio_with_cost"].append(metrics["excess_return_with_cost.information_ratio"]) result["information_ratio_with_cost"].append(metrics["excess_return_with_cost.information_ratio"])
result["max_drawdown_with_cost"].append(metrics["excess_return_with_cost.max_drawdown"]) result["max_drawdown_with_cost"].append(metrics["excess_return_with_cost.max_drawdown"])
result["ic"].append(metrics["IC"])
result["icir"].append(metrics["ICIR"])
result["rank_ic"].append(metrics["Rank IC"])
result["rank_icir"].append(metrics["Rank ICIR"])
results[fn] = result results[fn] = result
return results return results
# function to generate and save markdown table # function to generate and save markdown table
def gen_and_save_md_table(metrics): def gen_and_save_md_table(metrics, dataset):
table = "| Model Name | Annualized Return | Information Ratio | Max Drawdown |\n" table = "| Model Name | Dataset | IC | ICIR | Rank IC | Rank ICIR | Annualized Return | Information Ratio | Max Drawdown |\n"
table += "|---|---|---|---|\n" table += "|---|---|---|---|---|---|---|---|---|\n"
for fn in metrics: for fn in metrics:
ic = metrics[fn]["ic"]
icir = metrics[fn]["icir"]
ric = metrics[fn]["rank_ic"]
ricir = metrics[fn]["rank_icir"]
ar = metrics[fn]["annualized_return_with_cost"] ar = metrics[fn]["annualized_return_with_cost"]
ir = metrics[fn]["information_ratio_with_cost"] ir = metrics[fn]["information_ratio_with_cost"]
md = metrics[fn]["max_drawdown_with_cost"] md = metrics[fn]["max_drawdown_with_cost"]
table += f"| {fn} | {ar[0]:9.4f}±{ar[1]:9.2f} | {ir[0]:9.4f}±{ir[1]:9.2f}| {md[0]:9.4f}±{md[1]:9.2f} |\n" table += f"| {fn} | {dataset} | {ic[0]:5.4f}±{ic[1]:2.2f} | {icir[0]:5.4f}±{icir[1]:2.2f}| {ric[0]:5.4f}±{ric[1]:2.2f} | {ricir[0]:5.4f}±{ricir[1]:2.2f} | {ar[0]:5.4f}±{ar[1]:2.2f} | {ir[0]:5.4f}±{ir[1]:2.2f}| {md[0]:5.4f}±{md[1]:2.2f} |\n"
pprint(table) pprint(table)
with open("table.md", "w") as f: with open("table.md", "w") as f:
f.write(table) f.write(table)
@@ -180,7 +191,7 @@ def gen_and_save_md_table(metrics):
# function to run the all the models # function to run the all the models
@only_allow_defined_args @only_allow_defined_args
def run(times=1, models=None, exclude=False): def run(times=1, models=None, dataset="Alpha360", exclude=False):
""" """
Please be aware that this function can only work under Linux. MacOS and Windows will be supported in the future. Please be aware that this function can only work under Linux. MacOS and Windows will be supported in the future.
Any PR to enhance this method is highly welcomed. Any PR to enhance this method is highly welcomed.
@@ -193,6 +204,8 @@ def run(times=1, models=None, exclude=False):
determines the specific model or list of models to run or exclude. determines the specific model or list of models to run or exclude.
exclude : boolean exclude : boolean
determines whether the model being used is excluded or included. determines whether the model being used is excluded or included.
dataset : str
determines the dataset to be used for each model.
Usage: Usage:
------- -------
@@ -206,13 +219,16 @@ def run(times=1, models=None, exclude=False):
# Case 2 - run specific models multiple times # Case 2 - run specific models multiple times
python run_all_model.py 3 mlp python run_all_model.py 3 mlp
# Case 3 - run other models except those are given as arguments for multiple times # Case 3 - run specific models multiple times with specific dataset
python run_all_model.py 3 [mlp,tft,lstm] True python run_all_model.py 3 mlp Alpha158
# Case 4 - run specific models for one time # Case 4 - run other models except those are given as arguments for multiple times
python run_all_model.py 3 [mlp,tft,lstm] --exclude=True
# Case 5 - run specific models for one time
python run_all_model.py --models=[mlp,lightgbm] python run_all_model.py --models=[mlp,lightgbm]
# Case 5 - run other models except those are given as aruments for one time # Case 6 - run other models except those are given as aruments for one time
python run_all_model.py --models=[mlp,tft,sfm] --exclude=True python run_all_model.py --models=[mlp,tft,sfm] --exclude=True
""" """
@@ -226,7 +242,7 @@ def run(times=1, models=None, exclude=False):
env_path, python_path, conda_activate = create_env() env_path, python_path, conda_activate = create_env()
# get all files # get all files
sys.stderr.write("Retrieving files...\n") sys.stderr.write("Retrieving files...\n")
yaml_path, req_path = get_all_files(folders[fn]) yaml_path, req_path = get_all_files(folders[fn], dataset)
sys.stderr.write("\n") sys.stderr.write("\n")
# install requirements.txt # install requirements.txt
sys.stderr.write("Installing requirements.txt...\n") sys.stderr.write("Installing requirements.txt...\n")
@@ -240,6 +256,7 @@ def run(times=1, models=None, exclude=False):
sys.stderr.write("\n") sys.stderr.write("\n")
# install qlib # install qlib
sys.stderr.write("Installing qlib...\n") sys.stderr.write("Installing qlib...\n")
execute(f"{python_path} -m pip install --upgrade pip") # TODO: FIX ME!
execute(f"{python_path} -m pip install --upgrade cython") # TODO: FIX ME! execute(f"{python_path} -m pip install --upgrade cython") # TODO: FIX ME!
if fn == "TFT": if fn == "TFT":
execute( execute(
@@ -272,12 +289,15 @@ def run(times=1, models=None, exclude=False):
results = cal_mean_std(results) results = cal_mean_std(results)
# generating md table # generating md table
sys.stderr.write(f"Generating markdown table...\n") sys.stderr.write(f"Generating markdown table...\n")
gen_and_save_md_table(results) gen_and_save_md_table(results, dataset)
sys.stderr.write("\n") sys.stderr.write("\n")
# print erros # print erros
sys.stderr.write(f"Here are some of the errors of the models...\n") sys.stderr.write(f"Here are some of the errors of the models...\n")
pprint(errors) pprint(errors)
sys.stderr.write("\n") sys.stderr.write("\n")
# move results folder
shutil.move(exp_path, exp_path + f"_{dataset}_{datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}")
shutil.move("table.md", f"table_{dataset}_{datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}.md")
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -4,6 +4,7 @@
__version__ = "0.6.0.dev" __version__ = "0.6.0.dev"
import os import os
import re import re
import sys import sys

View File

@@ -43,7 +43,7 @@ _DEFAULT_INFER_PROCESSORS = [
] ]
class ALPHA360(DataHandlerLP): class Alpha360(DataHandlerLP):
def __init__( def __init__(
self, self,
instruments="csi500", instruments="csi500",
@@ -119,7 +119,7 @@ class ALPHA360(DataHandlerLP):
return fields, names return fields, names
class ALPHA360vwap(ALPHA360): class Alpha360vwap(Alpha360):
def get_label_config(self): def get_label_config(self):
return (["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"]) return (["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"])

View File

@@ -57,7 +57,7 @@ class ALSTM(Model):
loss="mse", loss="mse",
optimizer="adam", optimizer="adam",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -76,7 +76,7 @@ class ALSTM(Model):
self.early_stop = early_stop self.early_stop = early_stop
self.optimizer = optimizer.lower() self.optimizer = optimizer.lower()
self.loss = loss self.loss = loss
self.visible_GPU = GPU self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_gpu = torch.cuda.is_available() self.use_gpu = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -113,8 +113,9 @@ class ALSTM(Model):
) )
) )
np.random.seed(self.seed) if self.seed is not None:
torch.manual_seed(self.seed) np.random.seed(self.seed)
torch.manual_seed(self.seed)
self.ALSTM_model = ALSTMModel( self.ALSTM_model = ALSTMModel(
d_feat=self.d_feat, d_feat=self.d_feat,
@@ -130,11 +131,7 @@ class ALSTM(Model):
raise NotImplementedError("optimizer {} is not supported!".format(optimizer)) raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
self._fitted = False self._fitted = False
if self.use_gpu: self.ALSTM_model.to(self.device)
self.ALSTM_model.cuda()
# set the visible GPU
if self.visible_GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
def mse(self, pred, label): def mse(self, pred, label):
loss = (pred - label) ** 2 loss = (pred - label) ** 2
@@ -172,12 +169,8 @@ class ALSTM(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.ALSTM_model(feature) pred = self.ALSTM_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -205,12 +198,8 @@ class ALSTM(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.ALSTM_model(feature) pred = self.ALSTM_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -298,10 +287,7 @@ class ALSTM(Model):
else: else:
end = begin + self.batch_size end = begin + self.batch_size
x_batch = torch.from_numpy(x_values[begin:end]).float() x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device)
if self.use_gpu:
x_batch = x_batch.cuda()
with torch.no_grad(): with torch.no_grad():
if self.use_gpu: if self.use_gpu:

View File

@@ -62,7 +62,7 @@ class GATs(Model):
model_path=None, model_path=None,
optimizer="adam", optimizer="adam",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -83,7 +83,7 @@ class GATs(Model):
self.base_model = base_model self.base_model = base_model
self.with_pretrain = with_pretrain self.with_pretrain = with_pretrain
self.model_path = model_path self.model_path = model_path
self.visible_GPU = GPU self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_gpu = torch.cuda.is_available() self.use_gpu = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -123,8 +123,11 @@ class GATs(Model):
seed, seed,
) )
) )
np.random.seed(self.seed)
torch.manual_seed(self.seed) if self.seed is not None:
np.random.seed(self.seed)
torch.manual_seed(self.seed)
self.GAT_model = GATModel( self.GAT_model = GATModel(
d_feat=self.d_feat, d_feat=self.d_feat,
hidden_size=self.hidden_size, hidden_size=self.hidden_size,
@@ -140,11 +143,7 @@ class GATs(Model):
raise NotImplementedError("optimizer {} is not supported!".format(optimizer)) raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
self._fitted = False self._fitted = False
if self.use_gpu: self.GAT_model.to(self.device)
self.GAT_model.cuda()
# set the visible GPU
if self.visible_GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
def mse(self, pred, label): def mse(self, pred, label):
loss = (pred - label) ** 2 loss = (pred - label) ** 2
@@ -190,12 +189,8 @@ class GATs(Model):
for idx, count in zip(daily_index, daily_count): for idx, count in zip(daily_index, daily_count):
batch = slice(idx, idx + count) batch = slice(idx, idx + count)
feature = torch.from_numpy(x_train_values[batch]).float() feature = torch.from_numpy(x_train_values[batch]).float().to(self.device)
label = torch.from_numpy(y_train_values[batch]).float() label = torch.from_numpy(y_train_values[batch]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.GAT_model(feature) pred = self.GAT_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -221,12 +216,8 @@ class GATs(Model):
for idx, count in zip(daily_index, daily_count): for idx, count in zip(daily_index, daily_count):
batch = slice(idx, idx + count) batch = slice(idx, idx + count)
feature = torch.from_numpy(x_values[batch]).float() feature = torch.from_numpy(x_values[batch]).float().to(self.device)
label = torch.from_numpy(y_values[batch]).float() label = torch.from_numpy(y_values[batch]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.GAT_model(feature) pred = self.GAT_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -330,10 +321,7 @@ class GATs(Model):
for idx, count in zip(daily_index, daily_count): for idx, count in zip(daily_index, daily_count):
batch = slice(idx, idx + count) batch = slice(idx, idx + count)
x_batch = torch.from_numpy(x_values[batch]).float() x_batch = torch.from_numpy(x_values[batch]).float().to(self.device)
if self.use_gpu:
x_batch = x_batch.cuda()
with torch.no_grad(): with torch.no_grad():
if self.use_gpu: if self.use_gpu:

View File

@@ -57,7 +57,7 @@ class GRU(Model):
loss="mse", loss="mse",
optimizer="adam", optimizer="adam",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -76,7 +76,7 @@ class GRU(Model):
self.early_stop = early_stop self.early_stop = early_stop
self.optimizer = optimizer.lower() self.optimizer = optimizer.lower()
self.loss = loss self.loss = loss
self.visible_GPU = GPU self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_gpu = torch.cuda.is_available() self.use_gpu = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -113,8 +113,9 @@ class GRU(Model):
) )
) )
np.random.seed(self.seed) if self.seed is not None:
torch.manual_seed(self.seed) np.random.seed(self.seed)
torch.manual_seed(self.seed)
self.gru_model = GRUModel( self.gru_model = GRUModel(
d_feat=self.d_feat, d_feat=self.d_feat,
@@ -130,11 +131,7 @@ class GRU(Model):
raise NotImplementedError("optimizer {} is not supported!".format(optimizer)) raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
self._fitted = False self._fitted = False
if self.use_gpu: self.gru_model.to(self.device)
self.gru_model.cuda()
# set the visible GPU
if self.visible_GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
def mse(self, pred, label): def mse(self, pred, label):
loss = (pred - label) ** 2 loss = (pred - label) ** 2
@@ -172,12 +169,8 @@ class GRU(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.gru_model(feature) pred = self.gru_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -205,12 +198,8 @@ class GRU(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.gru_model(feature) pred = self.gru_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -298,10 +287,7 @@ class GRU(Model):
else: else:
end = begin + self.batch_size end = begin + self.batch_size
x_batch = torch.from_numpy(x_values[begin:end]).float() x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device)
if self.use_gpu:
x_batch = x_batch.cuda()
with torch.no_grad(): with torch.no_grad():
if self.use_gpu: if self.use_gpu:

View File

@@ -57,7 +57,7 @@ class LSTM(Model):
loss="mse", loss="mse",
optimizer="adam", optimizer="adam",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -76,7 +76,7 @@ class LSTM(Model):
self.early_stop = early_stop self.early_stop = early_stop
self.optimizer = optimizer.lower() self.optimizer = optimizer.lower()
self.loss = loss self.loss = loss
self.visible_GPU = GPU self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_gpu = torch.cuda.is_available() self.use_gpu = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -113,8 +113,9 @@ class LSTM(Model):
) )
) )
np.random.seed(self.seed) if self.seed is not None:
torch.manual_seed(self.seed) np.random.seed(self.seed)
torch.manual_seed(self.seed)
self.lstm_model = LSTMModel( self.lstm_model = LSTMModel(
d_feat=self.d_feat, d_feat=self.d_feat,
@@ -130,11 +131,7 @@ class LSTM(Model):
raise NotImplementedError("optimizer {} is not supported!".format(optimizer)) raise NotImplementedError("optimizer {} is not supported!".format(optimizer))
self._fitted = False self._fitted = False
if self.use_gpu: self.lstm_model.to(self.device)
self.lstm_model.cuda()
# set the visible GPU
if self.visible_GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
def mse(self, pred, label): def mse(self, pred, label):
loss = (pred - label) ** 2 loss = (pred - label) ** 2
@@ -172,12 +169,8 @@ class LSTM(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.lstm_model(feature) pred = self.lstm_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -205,12 +198,8 @@ class LSTM(Model):
if len(indices) - i < self.batch_size: if len(indices) - i < self.batch_size:
break break
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float() feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float() label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
if self.use_gpu:
feature = feature.cuda()
label = label.cuda()
pred = self.lstm_model(feature) pred = self.lstm_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
@@ -298,10 +287,7 @@ class LSTM(Model):
else: else:
end = begin + self.batch_size end = begin + self.batch_size
x_batch = torch.from_numpy(x_values[begin:end]).float() x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device)
if self.use_gpu:
x_batch = x_batch.cuda()
with torch.no_grad(): with torch.no_grad():
if self.use_gpu: if self.use_gpu:

View File

@@ -61,7 +61,7 @@ class DNNModelPytorch(Model):
optimizer="gd", optimizer="gd",
loss="mse", loss="mse",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -79,7 +79,7 @@ class DNNModelPytorch(Model):
self.lr_decay_steps = lr_decay_steps self.lr_decay_steps = lr_decay_steps
self.optimizer = optimizer.lower() self.optimizer = optimizer.lower()
self.loss_type = loss self.loss_type = loss
self.visible_GPU = GPU self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_GPU = torch.cuda.is_available() self.use_GPU = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -116,8 +116,9 @@ class DNNModelPytorch(Model):
) )
) )
np.random.seed(self.seed) if self.seed is not None:
torch.manual_seed(self.seed) np.random.seed(self.seed)
torch.manual_seed(self.seed)
if loss not in {"mse", "binary"}: if loss not in {"mse", "binary"}:
raise NotImplementedError("loss {} is not supported!".format(loss)) raise NotImplementedError("loss {} is not supported!".format(loss))
@@ -146,11 +147,7 @@ class DNNModelPytorch(Model):
) )
self._fitted = False self._fitted = False
if self.use_GPU: self.dnn_model.to(self.device)
self.dnn_model.cuda()
# set the visible GPU
if self.visible_GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = self.visible_GPU
def fit( def fit(
self, self,
@@ -187,13 +184,9 @@ class DNNModelPytorch(Model):
w_train_values = torch.from_numpy(w_train.values).float() w_train_values = torch.from_numpy(w_train.values).float()
train_num = y_train_values.shape[0] train_num = y_train_values.shape[0]
# prepare validation data # prepare validation data
x_val_auto = torch.from_numpy(x_valid.values).float() x_val_auto = torch.from_numpy(x_valid.values).float().to(self.device)
y_val_auto = torch.from_numpy(y_valid.values).float() y_val_auto = torch.from_numpy(y_valid.values).float().to(self.device)
w_val_auto = torch.from_numpy(w_valid.values).float() w_val_auto = torch.from_numpy(w_valid.values).float().to(self.device)
if self.use_GPU:
x_val_auto = x_val_auto.cuda()
y_val_auto = y_val_auto.cuda()
w_val_auto = w_val_auto.cuda()
for step in range(self.max_steps): for step in range(self.max_steps):
if stop_steps >= self.early_stop_rounds: if stop_steps >= self.early_stop_rounds:
@@ -204,14 +197,9 @@ class DNNModelPytorch(Model):
self.dnn_model.train() self.dnn_model.train()
self.train_optimizer.zero_grad() self.train_optimizer.zero_grad()
choice = np.random.choice(train_num, self.batch_size) choice = np.random.choice(train_num, self.batch_size)
x_batch_auto = x_train_values[choice] x_batch_auto = x_train_values[choice].to(self.device)
y_batch_auto = y_train_values[choice] y_batch_auto = y_train_values[choice].to(self.device)
w_batch_auto = w_train_values[choice] w_batch_auto = w_train_values[choice].to(self.device)
if self.use_GPU:
x_batch_auto = x_batch_auto.cuda()
y_batch_auto = y_batch_auto.cuda()
w_batch_auto = w_batch_auto.cuda()
# forward # forward
preds = self.dnn_model(x_batch_auto) preds = self.dnn_model(x_batch_auto)
@@ -276,9 +264,7 @@ class DNNModelPytorch(Model):
if not self._fitted: if not self._fitted:
raise ValueError("model is not fitted yet!") raise ValueError("model is not fitted yet!")
x_test_pd = dataset.prepare("test", col_set="feature") x_test_pd = dataset.prepare("test", col_set="feature")
x_test = torch.from_numpy(x_test_pd.values).float() x_test = torch.from_numpy(x_test_pd.values).float().to(self.device)
if self.use_GPU:
x_test = x_test.cuda()
self.dnn_model.eval() self.dnn_model.eval()
with torch.no_grad(): with torch.no_grad():

View File

@@ -217,7 +217,7 @@ class SFM(Model):
loss="mse", loss="mse",
optimizer="gd", optimizer="gd",
GPU="0", GPU="0",
seed=0, seed=None,
**kwargs **kwargs
): ):
# Set logger. # Set logger.
@@ -239,7 +239,7 @@ class SFM(Model):
self.eval_steps = eval_steps self.eval_steps = eval_steps
self.optimizer = optimizer.lower() self.optimizer = optimizer.lower()
self.loss = loss self.loss = loss
self.device = "cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu" self.device = torch.device("cuda:%d" % (GPU) if torch.cuda.is_available() else "cpu")
self.use_gpu = torch.cuda.is_available() self.use_gpu = torch.cuda.is_available()
self.seed = seed self.seed = seed
@@ -282,8 +282,9 @@ class SFM(Model):
) )
) )
np.random.seed(self.seed) if self.seed is not None:
torch.manual_seed(self.seed) np.random.seed(self.seed)
torch.manual_seed(self.seed)
self.sfm_model = SFM_Model( self.sfm_model = SFM_Model(
d_feat=self.d_feat, d_feat=self.d_feat,

View File

@@ -1,25 +0,0 @@
requests==2.22.0
six==1.14.0
lxml==4.5.0
statsmodels==0.12.0
pandas==1.1.2
matplotlib==3.3.2
scipy==1.3.3
numpy==1.17.4
Cython==0.29.21
fire==0.3.1
gevent_socketio==0.3.6
hyperopt==0.2.4
lightgbm==3.0.0
loguru==0.5.3
plotly==4.10.0
pymongo==3.11.0
PyYAML==5.3.1
redis==3.5.3
redis_lock==0.2.0
sacred==0.8.1
scikit_learn==0.23.2
torch==1.6.0
tqdm==4.49.0
yahooquery==2.2.7
mlflow==1.12.1

View File

@@ -10,6 +10,7 @@ from setuptools import find_packages, setup, Extension
NAME = "pyqlib" NAME = "pyqlib"
DESCRIPTION = "A Quantitative-research Platform" DESCRIPTION = "A Quantitative-research Platform"
REQUIRES_PYTHON = ">=3.5.0" REQUIRES_PYTHON = ">=3.5.0"
VERSION = "0.6.0.dev" VERSION = "0.6.0.dev"
# Detect Cython # Detect Cython