{"id":16271997,"url":"https://github.com/logcreative/qlib-cnn","last_synced_at":"2025-10-18T09:37:18.898Z","repository":{"id":168638060,"uuid":"428453476","full_name":"LogCreative/qlib-CNN","owner":"LogCreative","description":null,"archived":false,"fork":false,"pushed_at":"2022-01-23T06:38:48.000Z","size":5974,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T15:50:14.087Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LogCreative.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-15T23:30:18.000Z","updated_at":"2024-03-19T15:19:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"b140ee64-0b95-4217-a919-a92aca869a44","html_url":"https://github.com/LogCreative/qlib-CNN","commit_stats":null,"previous_names":["logcreative/qlib-cnn"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LogCreative/qlib-CNN","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LogCreative%2Fqlib-CNN","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LogCreative%2Fqlib-CNN/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LogCreative%2Fqlib-CNN/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LogCreative%2Fqlib-CNN/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LogCreative","download_url":"https://codeload.github.com/LogCreative/qlib-CNN/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LogCreative%2Fqlib-CNN/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266586839,"owners_count":23952199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T18:15:44.503Z","updated_at":"2025-10-18T09:37:13.860Z","avatar_url":"https://github.com/LogCreative.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# qlib-CNN\r\n\r\nQlib 依赖版本：0.8.3\r\n\r\n## 运行范例\r\n\r\n直接运行范例代码，得到结果。\r\n\r\n```\r\npython task1/workflow_by_code.py\r\n```\r\n\r\n![](task1/pass.png)\r\n\r\n## pytorch 实现 CNN\r\n\r\n基础版本的CNN论文展现了下面的三层卷积网络。\r\n\r\n![](img/oldcnn.png)\r\n\r\n但是对于一维卷积网络来讲，删去了 Pooling 层，并采用 padding 的方式保证数据维度一致。更好的优化需要使用 TCN 模型做多层卷积，使用因果卷积 Chomp1d。\r\n\r\n首先需要运行 [数据获取](task2/get_data.ipynb) 的代码。\r\n\r\n[代码](task2/pytorch_cnn.py) 为 CNN 的实现情况，层由 `layers` 定义。运行时需要将其移入 qlib 包的 `qlib/contrib/model/` 中，训练框架参考 [pytorch_nn](https://github.com/microsoft/qlib/blob/main/qlib/contrib/model/pytorch_nn.py)。\r\n\r\n使用\r\n```cmd\r\nqrun task2/workflow_config_cnn_Alpha158.yaml\r\nqrun task2/workflow_config_cnn_Alpha360.yaml\r\n```\r\n运行模型。\r\n\r\n使用含有 RTX 3080 Ti 的服务器上运行该模型，结果如下：\r\n\r\n![](img/run_CNN.png)\r\n\r\n### 分析\r\n\r\nTCN 已经被[实现](https://github.com/microsoft/qlib/blob/main/qlib/contrib/model/pytorch_tcn_ts.py)，参照对应的 [YAML 文件](task3/workflow_config_tcn_Alpha158.yaml)，将尝试直接运行该模型。由于本地算力不足，使用 Google Colab 运行之见 [代码](task3/workflow_tcn.ipynb)。\r\n\r\n\u003e `TSDatasetSampler` 近期有 API 变动，需要手动进行数据转换（已经进行 Issue [评论](https://github.com/microsoft/qlib/issues/411#issuecomment-993484655)）。\r\n\r\n![](img/report.png)\r\n\r\n![](img/return.png)\r\n\r\n![](img/scoreIC.png)\r\n\r\n![](img/IC.png)\r\n\r\n### 量化交易分析流程\r\n\r\n- **构造数据** 按照 Qlib 官方库的方法获取内置数据。\r\n股票池：沪深300成分股。\r\n- **特征分析** 特征采用 Qlib 内置的因子库 Alpha158 中的 158 个因子特征。\r\n- **特征预处理** 训练与验证集：DropnaLabel 用于去除缺失值，CSRrankNorm 用于截面标准化。测试集：RobustZScoreNorm 用来归一化数据，Fillna 用来去除缺失值。\r\n- **训练模型** 使用自己的模型，或者是 TCN 等其他模型。\r\n- **回测模型** 得到最优参数后，进行模型预测得到测试集中每一天股票的预测收益率。使用TopkDropout 策略。\r\n- **回测结果分析** 尝试将 TCN 适配进 workflow_by_code.ipynb 中可视化结果。发现由于 TCN 实现使用了 TSDatasetSampler，最近有 API 变动，适配了一些代码以绘制出图像。对此在 Qlib 中进行了 Issue 评论。\r\n- **模型上线** 现在关于自行实现的 CNN 模型上线还不现实，效果还不好。而现在的 TCN 模型在普通的 GPU （GTX 1050 Ti）上会因为显存不足的原因无法运行，轻量化的 CNN 模型还有待探索。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogcreative%2Fqlib-cnn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flogcreative%2Fqlib-cnn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogcreative%2Fqlib-cnn/lists"}