- start_system.py: 跨平台进程管理 - Windows 使用 CREATE_NEW_PROCESS_GROUP 和 terminate() - Unix 使用 setsid 和 killpg - 自动检测并启用 Windows ANSI 颜色支持 - 设备选择逻辑优化: - macOS: 优先使用 MPS,回退到 CPU - Windows/Linux: 强制使用 CPU(无 CUDA 依赖) - 影响文件: - api/prediction_service.py - auto_trade_polymarket.py - training/config.py
64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
"""Training configuration."""
|
||
|
||
from dataclasses import dataclass
|
||
from typing import Literal
|
||
|
||
|
||
@dataclass
|
||
class TrainingConfig:
|
||
"""Configuration for model training."""
|
||
|
||
# Training parameters
|
||
batch_size: int = 64
|
||
max_epochs: int = 100
|
||
learning_rate: float = 0.001
|
||
weight_decay: float = 1e-5
|
||
|
||
# Early stopping
|
||
early_stopping_patience: int = 10
|
||
early_stopping_min_delta: float = 1e-4
|
||
|
||
# Learning rate scheduler
|
||
lr_scheduler: Literal['plateau', 'cosine', 'none'] = 'plateau'
|
||
lr_patience: int = 5
|
||
lr_factor: float = 0.5
|
||
|
||
# Device
|
||
device: str = 'auto' # 'auto', 'mps', 'cuda', or 'cpu'
|
||
|
||
# Logging
|
||
log_interval: int = 10 # Log every N batches
|
||
|
||
# Checkpointing
|
||
save_best: bool = True
|
||
checkpoint_dir: str = './models/checkpoints'
|
||
|
||
def get_device(self) -> str:
|
||
"""Get the device to use for training.
|
||
|
||
Returns:
|
||
Device string ('mps', 'cuda', or 'cpu')
|
||
|
||
Note:
|
||
macOS 使用 MPS,Windows/Linux 使用 CPU
|
||
"""
|
||
import torch
|
||
import platform
|
||
|
||
if self.device != 'auto':
|
||
return self.device
|
||
|
||
# 根据操作系统选择设备:macOS 用 MPS,Windows/Linux 用 CPU
|
||
if platform.system() == 'Darwin': # macOS
|
||
if torch.backends.mps.is_available():
|
||
return 'mps'
|
||
else:
|
||
return 'cpu'
|
||
else: # Windows / Linux
|
||
return 'cpu'
|
||
|
||
|
||
if __name__ == "__main__":
|
||
config = TrainingConfig()
|
||
print(f"Using device: {config.get_device()}")
|