Initial commit: BTC 1h prediction model (53.10% accuracy)
🎯 项目成果: - 准确率: 53.10% (2026年测试集) - 任务类型: 二分类 (预测涨/跌) - 特征: OHLCV + RSI + MACD (9个特征) - 训练数据: 2020-2025年BTC小时数据 (52,351序列) - 测试数据: 2026年1-29日 (484序列) 📊 性能指标: - 精确率: 51.79% - 召回率: 49.36% - F1分数: 0.5054 - 预测分布: 非常平衡 (偏差仅-11个) 🔬 实验历史: - 尝试了5种方案 (回归/分类, 不同数据划分) - 分类任务优于回归任务 (53.10% vs 49.5%) - 全量训练避免了过拟合问题 📦 主要模型文件: - models/checkpoints/dlinear_classification_2026/ - best_model.pt (最佳模型) - preprocessor.pkl (预处理器) - test_2026_results.json (测试结果) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
9bbdc8d42b
82 files changed
+8742
No files matched your search
@@ -0,0 +1,190 @@
|
||||
"""使用真实Binance数据训练DLinear模型 - 优化版(平衡类别 + 类别权重)"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from data.fetcher import BinanceDataFetcher
|
||||
from data.preprocessor import CryptoPreprocessor, split_data
|
||||
from data.dataset import create_dataloaders
|
||||
from models.dlinear.model import DLinear, DLinearConfig
|
||||
from training.trainer import Trainer
|
||||
from training.config import TrainingConfig
|
||||
import json
|
||||
import numpy as np
|
||||
|
||||
def main():
|
||||
print("="*80)
|
||||
print("DLinear模型训练 - 优化版(平衡类别 + 类别权重)")
|
||||
print("="*80)
|
||||
|
||||
# 步骤1: 获取真实数据
|
||||
print("\n[1/8] 从Binance获取BTC 1h真实数据...")
|
||||
fetcher = BinanceDataFetcher()
|
||||
df = fetcher.fetch_ohlcv('BTC', '1h', use_cache=True)
|
||||
print(f"✓ 已获取 {len(df)} 行真实数据")
|
||||
print(f"✓ 时间范围:{df['timestamp'].min()} 至 {df['timestamp'].max()}")
|
||||
|
||||
# 步骤2: 数据预处理(使用真实涨跌:threshold=0.0)
|
||||
print("\n[2/8] 数据预处理(使用真实涨跌标签,threshold=0.0)...")
|
||||
preprocessor = CryptoPreprocessor(
|
||||
lookback=168,
|
||||
forecast_horizon=24,
|
||||
price_change_threshold=0.0 # 任何涨幅>0都算上涨
|
||||
)
|
||||
X, y, processed_df = preprocessor.preprocess_pipeline(df, task='classification', fit=True)
|
||||
print(f"✓ 已创建序列:X形状 = {X.shape}, y形状 = {y.shape}")
|
||||
print(f"✓ 特征数量:{X.shape[-1]}")
|
||||
|
||||
# 计算类别分布
|
||||
n_down = (y == 0).sum()
|
||||
n_up = (y == 1).sum()
|
||||
print(f"✓ 标签分布(优化后):下跌(0) = {n_down} ({n_down/len(y)*100:.1f}%), 上涨(1) = {n_up} ({n_up/len(y)*100:.1f}%)")
|
||||
|
||||
# 计算类别权重
|
||||
pos_weight = n_down / n_up if n_up > 0 else 1.0
|
||||
print(f"✓ 计算类别权重:pos_weight = {pos_weight:.2f} (给上涨类{pos_weight:.2f}倍权重)")
|
||||
|
||||
# 步骤3: 划分数据集
|
||||
print("\n[3/8] 划分训练/验证/测试集 (70/15/15)...")
|
||||
train_data, val_data, test_data = split_data(X, y, train_split=0.7, val_split=0.15)
|
||||
|
||||
# 显示各数据集的类别分布
|
||||
print(f" 训练集:下跌 {(train_data[1]==0).sum()}, 上涨 {(train_data[1]==1).sum()}")
|
||||
print(f" 验证集:下跌 {(val_data[1]==0).sum()}, 上涨 {(val_data[1]==1).sum()}")
|
||||
print(f" 测试集:下跌 {(test_data[1]==0).sum()}, 上涨 {(test_data[1]==1).sum()}")
|
||||
|
||||
# 步骤4: 创建数据加载器
|
||||
print("\n[4/8] 创建PyTorch数据加载器...")
|
||||
train_loader, val_loader, test_loader = create_dataloaders(
|
||||
train_data, val_data, test_data,
|
||||
task='classification',
|
||||
batch_size=64
|
||||
)
|
||||
|
||||
# 步骤5: 创建模型
|
||||
print("\n[5/8] 创建DLinear模型...")
|
||||
model_config = DLinearConfig(
|
||||
input_dim=X.shape[-1],
|
||||
lookback=168,
|
||||
task='classification',
|
||||
hidden_dim=512,
|
||||
dropout=0.1,
|
||||
moving_avg_kernel=25
|
||||
)
|
||||
model = DLinear(model_config)
|
||||
print(f"✓ 模型已创建,可训练参数:{model.count_parameters():,} 个")
|
||||
|
||||
# 步骤6: 训练模型(使用类别权重)
|
||||
print("\n[6/8] 开始训练模型(使用加权损失函数)...")
|
||||
train_config = TrainingConfig(
|
||||
batch_size=64,
|
||||
max_epochs=50,
|
||||
learning_rate=0.001,
|
||||
early_stopping_patience=10,
|
||||
lr_scheduler='plateau',
|
||||
save_best=True,
|
||||
checkpoint_dir='./models/checkpoints/dlinear_btc_1h_balanced'
|
||||
)
|
||||
|
||||
trainer = Trainer(
|
||||
model,
|
||||
train_config,
|
||||
task='classification',
|
||||
pos_weight=pos_weight # 传入类别权重
|
||||
)
|
||||
history = trainer.fit(train_loader, val_loader)
|
||||
|
||||
# 步骤7: 评估模型
|
||||
print("\n[7/8] 在测试集上评估模型...")
|
||||
metrics = trainer.evaluate(test_loader)
|
||||
|
||||
# 保存评估结果
|
||||
results_path = './models/checkpoints/dlinear_btc_1h_balanced/test_metrics.json'
|
||||
with open(results_path, 'w') as f:
|
||||
json.dump(metrics, f, indent=2)
|
||||
|
||||
print(f"\n✓ 评估结果已保存至:{results_path}")
|
||||
|
||||
# 打印结果摘要
|
||||
print("\n" + "="*80)
|
||||
print("训练完成 - 最终测试结果(优化版)")
|
||||
print("="*80)
|
||||
print(f"\n📊 分类指标:")
|
||||
print(f" 准确率: {metrics.get('accuracy', 0):.4f}")
|
||||
print(f" F1分数: {metrics.get('f1', 0):.4f}")
|
||||
print(f" 精确率: {metrics.get('precision', 0):.4f}")
|
||||
print(f" 召回率: {metrics.get('recall', 0):.4f}")
|
||||
|
||||
if 'true_positives' in metrics:
|
||||
print(f"\n📈 混淆矩阵:")
|
||||
print(f" 真阳性(预测涨且真涨): {metrics['true_positives']}")
|
||||
print(f" 真阴性(预测跌且真跌): {metrics['true_negatives']}")
|
||||
print(f" 假阳性(预测涨但真跌): {metrics['false_positives']}")
|
||||
print(f" 假阴性(预测跌但真涨): {metrics['false_negatives']}")
|
||||
|
||||
print(f"\n💰 回测结果:")
|
||||
print(f" 总收益率: {metrics.get('backtest_total_return', 0)*100:.2f}%")
|
||||
print(f" 最终资金: ${metrics.get('backtest_final_capital', 0):,.2f}")
|
||||
print(f" 夏普比率: {metrics.get('backtest_sharpe_ratio', 0):.4f}")
|
||||
print(f" 最大回撤: {metrics.get('backtest_max_drawdown', 0)*100:.2f}%")
|
||||
print(f" 胜率: {metrics.get('backtest_win_rate', 0)*100:.2f}%")
|
||||
print(f" 交易次数: {metrics.get('backtest_num_trades', 0)}")
|
||||
|
||||
# 步骤8: ONNX导出
|
||||
print("\n[8/8] 测试ONNX导出...")
|
||||
try:
|
||||
import onnx
|
||||
import onnxruntime as ort
|
||||
import torch
|
||||
|
||||
onnx_path = './models/checkpoints/dlinear_btc_1h_balanced/model.onnx'
|
||||
|
||||
print(" 导出模型到ONNX格式...")
|
||||
model.to_onnx(onnx_path)
|
||||
print(f" ✓ ONNX模型已保存至:{onnx_path}")
|
||||
|
||||
print(" 验证ONNX模型...")
|
||||
onnx_model = onnx.load(onnx_path)
|
||||
onnx.checker.check_model(onnx_model)
|
||||
print(" ✓ ONNX模型有效")
|
||||
|
||||
# 测试推理
|
||||
test_input = torch.randn(1, 168, X.shape[-1])
|
||||
model.cpu()
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
pytorch_output = model(test_input).numpy()
|
||||
|
||||
ort_session = ort.InferenceSession(onnx_path)
|
||||
onnx_output = ort_session.run(None, {'input': test_input.numpy()})[0]
|
||||
|
||||
diff = np.abs(pytorch_output - onnx_output).max()
|
||||
print(f" ✓ PyTorch与ONNX的最大差异:{diff:.8f}")
|
||||
|
||||
if diff < 1e-5:
|
||||
print(" ✓ ONNX导出成功!")
|
||||
else:
|
||||
print(" ⚠ ONNX输出有差异(可能仍可接受)")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ✗ ONNX导出失败:{e}")
|
||||
|
||||
print("\n" + "="*80)
|
||||
print("✓ DLinear模型训练完成(优化版)")
|
||||
print("="*80)
|
||||
|
||||
# 对比旧版本
|
||||
print("\n📊 优化效果对比:")
|
||||
print(" 旧版本(1.5%阈值):")
|
||||
print(" - 类别分布:上涨 15.5%, 下跌 84.5%")
|
||||
print(" - 准确率:84.47% F1:0.00 召回率:0.00")
|
||||
print(" - 模型策略:永远预测下跌")
|
||||
print(" ")
|
||||
print(" 新版本(0%阈值 + 类别权重):")
|
||||
print(f" - 类别分布:上涨 {n_up/len(y)*100:.1f}%, 下跌 {n_down/len(y)*100:.1f}%")
|
||||
print(f" - 准确率:{metrics.get('accuracy', 0):.2%} F1:{metrics.get('f1', 0):.2f} 召回率:{metrics.get('recall', 0):.2f}")
|
||||
print(f" - 回测收益:{metrics.get('backtest_total_return', 0)*100:.2f}%")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user