You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.6 KiB

1 month ago
  1. # Kronos微调-支持自定义CSV数据集
  2. 这是一个在自定义的CSV格式数据上微调Kronos模型的完整流程。包含顺序训练(先训练tokenizer再训练predictor)和单独模块训练,同时支持分布式训练。
  3. ## 1. 准备数据
  4. ### 数据格式
  5. CSV文件必须包含以下列:
  6. - `timestamps`: 每个数据点的时间戳
  7. - `open`: 开盘价
  8. - `high`: 最高价
  9. - `low`: 最低价
  10. - `close`: 收盘价
  11. - `volume`: 交易量
  12. - `amount`: 交易金额
  13. (volume和amount可以全0如果没有这部分的数据)
  14. ### 示例数据格式
  15. | timestamps | open | close | high | low | volume | amount |
  16. |------------|------|-------|------|-----|--------|--------|
  17. | 2019/11/26 9:35 | 182.45215 | 184.45215 | 184.95215 | 182.45215 | 15136000 | 0 |
  18. | 2019/11/26 9:40 | 184.35215 | 183.85215 | 184.55215 | 183.45215 | 4433300 | 0 |
  19. | 2019/11/26 9:45 | 183.85215 | 183.35215 | 183.95215 | 182.95215 | 3070900 | 0 |
  20. > **标准数据样例**: `data/HK_ali_09988_kline_5min_all.csv`
  21. ## 2. 准备config文件
  22. data_path及预训练模型路径需要修改,训练参数可以自己调节
  23. ```yaml
  24. # 数据配置
  25. data:
  26. data_path: "/path/to/your/data.csv"
  27. lookback_window: 512 # 要使用的历史数据点
  28. predict_window: 48 # 要预测的未来点数
  29. max_context: 512 # 最大上下文长度
  30. ...
  31. ```
  32. 这里还有其他一些设置, `configs/config_ali09988_candle-5min.yaml` 有更详细的注释。
  33. ## 3. 训练
  34. ### 方法1: 直接顺序训练
  35. `train_sequential.py` 脚本自动处理完整的训练流程:
  36. ```bash
  37. # 完整训练(tokenizer + predictor)
  38. python train_sequential.py --config configs/config_ali09988_candle-5min.yaml
  39. # 跳过已存在的模型
  40. python train_sequential.py --config configs/config_ali09988_candle-5min.yaml --skip-existing
  41. # 只训练tokenizer
  42. python train_sequential.py --config configs/config_ali09988_candle-5min.yaml --skip-basemodel
  43. # 只训练predictor
  44. python train_sequential.py --config configs/config_ali09988_candle-5min.yaml --skip-tokenizer
  45. ```
  46. ### 方法2: 单独组件训练
  47. 可以单独训练每个组件:
  48. ```bash
  49. # 步骤1: 训练tokenizer
  50. python finetune_tokenizer.py --config configs/config_ali09988_candle-5min.yaml
  51. # 步骤2: 训练predictor(需要微调后的tokenizer)
  52. python finetune_base_model.py --config configs/config_ali09988_candle-5min.yaml
  53. ```
  54. ### DDP训练
  55. 如果有多卡,可以开启ddp加速训练:
  56. ```bash
  57. # 设置通信后端(NVIDIA GPU用nccl,CPU/混合用gloo)
  58. DIST_BACKEND=nccl \
  59. torchrun --standalone --nproc_per_node=8 train_sequential.py --config configs/config_ali09988_candle-5min.yaml
  60. ```
  61. ## 4. 训练结果
  62. 训练过程生成以下输出:
  63. ### 模型检查点
  64. - **Tokenizer**: 保存到 `{base_save_path}/{exp_name}/tokenizer/best_model/`
  65. - **Predictor**: 保存到 `{base_save_path}/{exp_name}/basemodel/best_model/`
  66. ### 训练日志
  67. - **控制台输出**: 实时训练进度和指标
  68. - **日志文件**: 详细日志保存到 `{base_save_path}/logs/`
  69. - **验证跟踪**: 基于验证损失保存最佳模型
  70. ## 5. 预测可视化
  71. 以下图像显示了kronos在阿里巴巴股票数据上微调后的示例训练结果:
  72. ![训练结果 1](examples/HK_ali_09988_kline_5min_all_historical_20250919_073929.png)
  73. ![训练结果 2](examples/HK_ali_09988_kline_5min_all_historical_20250919_073944.png)
  74. ![训练结果 3](examples/HK_ali_09988_kline_5min_all_historical_20250919_074012.png)
  75. ![训练结果 4](examples/HK_ali_09988_kline_5min_all_historical_20250919_074042.png)
  76. ![训练结果 5](examples/HK_ali_09988_kline_5min_all_historical_20250919_074251.png)