|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Any, Dict, Optional, Sequence |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class DreamerConfig: |
| 9 | + """Dreamer 全局配置:涵盖模型结构、优化器参数与算法开关。""" |
| 10 | + # --- 基础环境参数 --- |
| 11 | + obs_dim: int |
| 12 | + action_dim: int |
| 13 | + |
| 14 | + # --- RSSM 核心维度 --- |
| 15 | + embed_dim: int = 64 # 观测编码后的维度 |
| 16 | + deter_dim: int = 128 # 确定性状态 h_t 的维度 (GRU) |
| 17 | + stoch_dim: int = 32 # 随机状态 z_t 的维度 |
| 18 | + stoch_classes: int = 32 # (V2/V3) 离散潜在变量的类别数 |
| 19 | + hidden_dim: int = 128 # MLP 隐藏层维度 |
| 20 | + |
| 21 | + # --- 训练超参数 --- |
| 22 | + model_lr: float = 3e-4 # 世界模型学习率 |
| 23 | + actor_lr: float = 8e-5 # 策略网络学习率 |
| 24 | + critic_lr: float = 8e-5 # 价值网络学习率 |
| 25 | + entropy_scale: float = 1e-3 |
| 26 | + grad_clip: float = 100.0 # 梯度裁剪阈值 |
| 27 | + use_obs_norm: bool = True |
| 28 | + normalize_advantage: bool = True |
| 29 | + |
| 30 | + # --- RL 算法参数 --- |
| 31 | + discount: float = 0.99 # 折扣因子 gamma |
| 32 | + lambda_: float = 0.95 # Lambda-return 平滑系数 |
| 33 | + horizon: int = 15 # 想象视界长度 H |
| 34 | + |
| 35 | + # --- Loss 权重 --- |
| 36 | + free_nats: float = 1.0 # KL 散度的 Free bits 阈值 |
| 37 | + kl_scale: float = 1.0 # KL Loss 权重 |
| 38 | + discount_scale: float = 10.0 |
| 39 | + |
| 40 | + # --- V2 特性开关 --- |
| 41 | + kl_balance: float = 0.8 # KL Balancing 权重 (0.8 给先验) |
| 42 | + |
| 43 | + # --- V3 特性开关 --- |
| 44 | + use_symlog: bool = False # 是否启用 Symlog 数值压缩 |
| 45 | + target_tau: float = 0.01 # Critic Target 软更新系数 |
| 46 | + |
| 47 | + # --- V3 离散回归配置 --- |
| 48 | + reward_bins: int = 0 # 奖励离散化的桶数量 (0表示使用标量回归) |
| 49 | + reward_min: float = -10.0 |
| 50 | + reward_max: float = 10.0 |
| 51 | + value_bins: int = 0 # 价值离散化的桶数量 |
| 52 | + value_min: float = -20.0 |
| 53 | + value_max: float = 20.0 |
| 54 | + |
| 55 | + |
| 56 | +@dataclass |
| 57 | +class TrainConfig: |
| 58 | + """训练流程相关配置。""" |
| 59 | + env_ids: Sequence[str] = ("CartPole-v1",) |
| 60 | + agent_versions: Sequence[str] = ("v1",) |
| 61 | + seeds: Sequence[int] = (42, 2024) |
| 62 | + total_steps: int = 30_000 |
| 63 | + seed_steps: int = 2_000 |
| 64 | + train_every: int = 1 |
| 65 | + train_steps: int = 1 |
| 66 | + batch_size: int = 32 |
| 67 | + seq_len: int = 8 |
| 68 | + horizon: int = 15 |
| 69 | + replay_size: int = 100_000 |
| 70 | + log_every: int = 1000 |
| 71 | + eval_every: int = 2_000 |
| 72 | + eval_episodes: int = 5 |
| 73 | + workdir: str = "runs/dreamer" |
| 74 | + workdir_time_format: str = "%Y%m%d_%H%M%S" |
| 75 | + timestamp_workdir_if_exists: bool = True |
| 76 | + device: str = "cuda" if torch.cuda.is_available() else "cpu" |
| 77 | + env_kwargs: Optional[Dict[str, Any]] = None |
| 78 | + |
| 79 | + # Exploration schedule (epsilon-greedy after seed steps) |
| 80 | + exploration_epsilon_start: float = 0.10 |
| 81 | + exploration_epsilon_end: float = 0.00 |
| 82 | + exploration_decay_steps: int = 20_000 |
| 83 | + |
| 84 | + # Actor regularization / reward scaling |
| 85 | + entropy_scale: float = 1e-3 |
| 86 | + model_lr: float = 3e-4 |
| 87 | + actor_lr: float = 3e-4 |
| 88 | + critic_lr: float = 3e-4 |
| 89 | + |
| 90 | + # Model configuration |
| 91 | + embed_dim: int = 64 |
| 92 | + deter_dim: int = 128 |
| 93 | + stoch_dim: int = 32 |
| 94 | + stoch_classes: int = 32 |
| 95 | + hidden_dim: int = 128 |
| 96 | + use_obs_norm: bool = True |
| 97 | + normalize_advantage: bool = True |
| 98 | + discount: float = 0.99 |
| 99 | + lambda_: float = 0.95 |
| 100 | + free_nats: float = 1.0 |
| 101 | + kl_scale: float = 1.0 |
| 102 | + discount_scale: float = 10.0 |
| 103 | + kl_balance: float = 0.8 |
| 104 | + target_tau: float = 0.01 |
| 105 | + |
| 106 | + # 是否每次训练前清空 metrics 文件,避免重复运行时混入历史点 |
| 107 | + overwrite_metrics: bool = True |
| 108 | + |
| 109 | + # Plotting |
| 110 | + plot_path: str = "runs/dreamer/return_curve.png" |
| 111 | + |
| 112 | + def __post_init__(self) -> None: |
| 113 | + """填充默认环境参数。""" |
| 114 | + if self.env_kwargs is None: |
| 115 | + self.env_kwargs = {} |
0 commit comments