1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-09 22:10:56 +08:00

trainer & group & collect & ensemble

This commit is contained in:
lzh222333
2021-04-02 04:27:14 +00:00
parent edcd7b1ff9
commit bd7a1c11b9
12 changed files with 319 additions and 261 deletions

View File

@@ -8,9 +8,11 @@ from qlib.workflow import R
from qlib.workflow.task.gen import RollingGen, task_generator
from qlib.workflow.task.manage import TaskManager, run_task
from qlib.workflow.task.collect import RecorderCollector
from qlib.workflow.task.ensemble import RollingEnsemble
from qlib.model.ens.ensemble import RollingEnsemble, ens_workflow
import pandas as pd
from qlib.workflow.task.utils import list_recorders
from qlib.model.ens.group import RollingGroup
from qlib.model.trainer import TrainerRM
data_handler_config = {
"start_time": "2008-01-01",
@@ -94,24 +96,16 @@ def task_generating():
return tasks
# This part corresponds to "Task Storing" in the document
def task_storing(tasks, task_pool, exp_name):
print("========== task_storing ==========")
tm = TaskManager(task_pool=task_pool)
tm.create_task(tasks) # all tasks will be saved to MongoDB
# This part corresponds to "Task Running" in the document
def task_running(task_pool, exp_name):
print("========== task_running ==========")
run_task(task_train, task_pool, experiment_name=exp_name) # all tasks will be trained using "task_train" method
def task_training(tasks, task_pool, exp_name):
trainer = TrainerRM()
trainer.train(tasks, exp_name, task_pool)
# This part corresponds to "Task Collecting" in the document
def task_collecting(task_pool, exp_name):
print("========== task_collecting ==========")
def get_group_key_func(recorder):
def rec_key(recorder):
task_config = recorder.load_object("task")
model_key = task_config["model"]["class"]
rolling_key = task_config["dataset"]["kwargs"]["segments"]["test"]
@@ -119,14 +113,14 @@ def task_collecting(task_pool, exp_name):
def my_filter(recorder):
# only choose the results of "LGBModel"
model_key, rolling_key = get_group_key_func(recorder)
model_key, rolling_key = rec_key(recorder)
if model_key == "LGBModel":
return True
return False
collector = RecorderCollector(exp_name)
# group tasks by "get_task_key" and filter tasks by "my_filter"
artifact = collector.collect(RollingEnsemble(), get_group_key_func, rec_filter_func=my_filter)
artifact = ens_workflow(
RecorderCollector(exp_name=exp_name, rec_key_func=rec_key), RollingGroup(), rec_filter_func=my_filter
)
print(artifact)
@@ -143,10 +137,9 @@ def main(
}
qlib.init(provider_uri=provider_uri, region=REG_CN, mongo=mongo_conf)
reset(task_pool, exp_name)
tasks = task_generating()
task_storing(tasks, task_pool, exp_name)
task_running(task_pool, exp_name)
# reset(task_pool, exp_name)
# tasks = task_generating()
# task_training(tasks, task_pool, exp_name)
task_collecting(task_pool, exp_name)

View File

@@ -6,10 +6,10 @@ from qlib.config import REG_CN
from qlib.model.trainer import task_train
from qlib.workflow import R
from qlib.workflow.task.collect import RecorderCollector
from qlib.workflow.task.ensemble import RollingEnsemble
from qlib.model.ens.ensemble import RollingEnsemble
from qlib.workflow.task.gen import RollingGen, task_generator
from qlib.workflow.task.manage import TaskManager, run_task
from qlib.workflow.task.online import RollingOnlineManager
from qlib.workflow.online.manager import RollingOnlineManager
from qlib.workflow.task.utils import list_recorders
data_handler_config = {
@@ -155,10 +155,10 @@ def first_run():
rolling_online_manager.reset_online_tag(latest_rec.values())
def after_day():
def routine():
print("========== after_day ==========")
print_online_model()
rolling_online_manager.after_day()
rolling_online_manager.routine()
print_online_model()
task_collecting()

View File

@@ -2,7 +2,7 @@ import fire
import qlib
from qlib.config import REG_CN
from qlib.model.trainer import task_train
from qlib.workflow.task.online import OnlineManagerR
from qlib.workflow.online.manager import OnlineManagerR
from qlib.workflow.task.utils import list_recorders
data_handler_config = {
@@ -52,7 +52,7 @@ task = {
}
def first_train(experiment_name="online_svr"):
def first_train(experiment_name="online_srv"):
rid = task_train(task_config=task, experiment_name=experiment_name)
@@ -60,7 +60,7 @@ def first_train(experiment_name="online_svr"):
online_manager.reset_online_tag(rid)
def update_online_pred(experiment_name="online_svr"):
def update_online_pred(experiment_name="online_srv"):
online_manager = OnlineManagerR(experiment_name)