基于模型的强化学习理论基础
基于模型的强化学习(Model-Based Reinforcement Learning, MBRL)通过学习环境模型来加速策略优化,是样本效率最高的学习范式之一。1
问题定义
MBRL框架
MBRL的核心思想:
- 学习环境模型: 和
- 利用模型规划:在模型中进行想象 rollout
- 策略更新:基于真实交互和模型交互更新策略
┌─────────────────────────────────────────────────────────────┐
│ MBRL 核心流程 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 真实环境 ──交互──▶ 轨迹数据 ──▶ 学习世界模型 │
│ ▲ │ │
│ │ ▼ │
│ │ 在模型中规划 │
│ │ │ │
│ └─────────策略更新◀────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
与无模型RL的对比
| 方面 | Model-Free RL | Model-Based RL |
|---|---|---|
| 样本效率 | 低(需大量交互) | 高(模型辅助) |
| 计算成本 | 低(直接策略优化) | 高(需学习模型) |
| 泛化能力 | 有限 | 较强 |
| 模型误差 | 无 | 需处理 |
| 规划能力 | 有限 | 强大 |
PETS:概率集成轨迹采样
Chua et al. (2018)
PETS是NeurIPS 2018的最佳论文之一,核心思想是结合不确定性估计与轨迹采样规划。2
架构设计
PETS 架构
┌──────────────────────────────────────────────────────┐
│ │
│ 概率集成模型 (Ensemble of Probabilistic Models) │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Model 1 │ │ Model 2 │ │ Model 3 │ ... │
│ │ N(μ,σ²) │ │ N(μ,σ²) │ │ N(μ,σ²) │ │
│ └────┬────┘ └────┬────┘ └────┬────┘ │
│ └───────────┴───────────┘ │
│ │ │
│ ▼ │
│ 轨迹采样 (Trajectory Sampling) │
│ CEM-RS: Cross-Entropy Method with Resampling │
│ │ │
│ ▼ │
│ 模型预测控制 (MPC) │
│ │ │
│ ▼ │
│ 执行第一个动作 │
│ │
└──────────────────────────────────────────────────────┘
算法实现
import numpy as np
from typing import List, Tuple
class PETS:
def __init__(self, state_dim, action_dim, n_ensemble=5, horizon=25):
self.n_ensemble = n_ensemble
self.horizon = horizon
# 概率集成模型
self.models = [
ProbabilisticModel(state_dim, action_dim)
for _ in range(n_ensemble)
]
# 交叉熵方法参数
self.n_samples = 1000
self.n_elites = 100
def predict(self, states, actions):
"""使用集成模型预测下一个状态"""
# 对每个模型进行预测
predictions = []
for model in self.models:
mean, var = model.predict(states, actions)
# 添加观测噪声
std = np.sqrt(var) + 1e-6
samples = mean + np.random.randn(*mean.shape) * std
predictions.append(samples)
# 返回所有预测的集合
return np.stack(predictions)
def plan(self, state, env):
"""使用MPC进行规划"""
state = state.reshape(1, -1)
mean_action = np.zeros(self.horizon)
for CEM_iter in range(5):
# 采样动作序列
action_samples = np.random.randn(self.n_samples, self.horizon)
action_samples = action_samples * 0.5 + mean_action.reshape(1, -1)
action_samples = np.clip(action_samples, -1, 1)
# 评估每个动作序列
returns = []
for i in range(self.n_samples):
sim_state = state.copy()
total_reward = 0
for t in range(self.horizon):
action = action_samples[i, t]
# 使用集成模型预测
next_states = self.predict(sim_state, action.reshape(1, -1))
# 取集成均值
next_state = next_states.mean(axis=0)
# 简化的奖励函数
reward = self.reward(sim_state, action)
total_reward += reward
sim_state = next_state
returns.append(total_reward)
# 选择精英动作
returns = np.array(returns)
elite_indices = np.argsort(returns)[-self.n_elites:]
elite_actions = action_samples[elite_indices]
# 更新均值
mean_action = elite_actions.mean(axis=0)
return mean_action[0]
def reward(self, state, action):
"""简化的奖励函数"""
return -np.sum(state**2) - 0.1 * np.sum(action**2)不确定性估计
PETS的关键是概率模型,可以估计预测的不确定性:
class ProbabilisticModel:
def __init__(self, state_dim, action_dim, hidden_dim=200):
self.network = nn.Sequential(
nn.Linear(state_dim + action_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, state_dim * 2) # 均值 + 方差
)
def predict(self, state, action):
x = torch.cat([state, action], dim=-1)
output = self.network(x)
mean = output[..., :state.shape[-1]]
log_var = output[..., state.shape[-1]:]
var = torch.exp(log_var) + 1e-6
return mean, varDreamer系列算法
Hafner et al. (2019-2023)
Dreamer系列是MBRL领域最具影响力的工作,包括DreamerV1/PlaNet、DreamerV2和DreamerV3。345
DreamerV1/PlaNet
核心创新:RSSM(循环状态空间模型)
RSSM 架构
┌─────────────────────────────────────────────────────────┐
│ │
│ 隐状态 h_t ─────────────────────────────────────┐ │
│ │ │ │
│ │ │ │
│ ▼ │ │
│ ┌────────────────┐ ┌────────────────┐ │ │
│ │ 随机状态 s_t │◀────▶│ 循环状态 h_t │ │ │
│ │ (stochastic) │ │ (deterministic)│ │ │
│ └───────┬────────┘ └───────┬────────┘ │ │
│ │ │ │ │
│ ▼ │ │ │
│ ┌────────────────┐ │ │ │
│ │ 先验 p(s_t| │ │ │ │
│ │ h_{t-1},a_{t-1}) │ │ │
│ └───────┬────────┘ │ │ │
│ │ │ │ │
│ ▼ ▼ │ │
│ ┌────────────────┐ ┌────────────────┐ │ │
│ │ 后验 q(s_t| │ │ 动力学模型 │ │ │
│ │ h_{t-1},a_{t-1},o_t) │ p(o_t|s_t,h_t) │ │ │
│ └────────────────┘ └────────────────┘ │ │
│ │
└─────────────────────────────────────────────────────────┘
DreamerV2改进
| 改进点 | DreamerV1 | DreamerV2 |
|---|---|---|
| 隐表示 | 连续高斯 | 离散分类 |
| KL平衡 | 固定权重 | 自由KL |
| 网络结构 | 标准 | LayerNorm |
DreamerV3 (Nature 2025)
DreamerV3是首个在Nature发表的MBRL论文,实现了单一算法覆盖150+任务。5
关键创新:
- symlog变换:统一处理不同尺度的奖励
- symlog重放:稳定的价值函数学习
- 改进的Actor-Critic:更稳定的训练
SimPLe
Kaiser et al. (2019)
SimPLe在仅100K次环境交互(约为model-free方法的1/50)下学习Atari游戏策略。6
SimPLe 流程
┌─────────────────────────────────────────────────────┐
│ │
│ ┌─────────────┐ │
│ │ 真实环境 │ │
│ │ (100K步限制)│ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ 视频预测 │ ───▶ │ 模拟环境 │ │
│ │ 世界模型 │ │ (World Model)│ │
│ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │
│ │ 交替优化 │ │
│ │ │ │
│ ◀─────────────────────┘ │
│ │
│ ┌─────────────┐ │
│ │ 策略优化 │ │
│ │ (PPO等) │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────┘
模型误差处理
复合误差问题
MBRL的主要挑战是多步预测误差累积:
误差会随着步数增加而指数增长。
应对策略
| 策略 | 方法 | 优缺点 |
|---|---|---|
| 不确定性估计 | 集成模型、贝叶斯网络 | 量化但不解决 |
| 轨迹长度限制 | 短horizon rollout | 简单有效 |
| 模型校正 | DART、MPC | 需额外计算 |
| 重新规划 | CEM、MCTS | 鲁棒但慢 |
想象时序差分学习
Dreamer使用”想象 rollout”来估计价值:
def imagine_rollout(self, initial_state, horizon=15):
"""在隐空间进行想象 rollout"""
h, s = initial_state
rewards = []
states = [(h, s)]
for t in range(horizon):
# 策略选择动作
a = self.actor(torch.cat([h, s], dim=-1))
# 在模型中前进一步
h_new = self.dynamics_rnn(h, s, a)
s_new = self.prior(h_new) # 采样
# 奖励预测
r = self.reward(h_new, s_new)
rewards.append(r)
states.append((h_new, s_new))
h, s = h_new, s_new
# 计算回报
returns = self.compute_returns(rewards)
return states, returns样本效率对比
| 算法 | Atari游戏 | 所需交互步数 |
|---|---|---|
| DQN | 人类水平 | ~200M |
| Rainbow | 超人类 | ~50M |
| SimPLe | 接近人类 | ~100K |
| PETS | 达到部分 | ~500K |
| DreamerV3 | 超人类 | <1M |
参考资料
相关链接
- World Models与规划 — Dreamer、MuZero详细解析
- 模型预测控制 — MPC理论基础
- 分布式RL算法 — 大规模MBRL
Footnotes
-
Moos et al., “A Survey on Model-Based Reinforcement Learning”, arXiv:2206.09328, 2022 ↩
-
Chua et al., “Deep Reinforcement Learning in a Handful of Trials using Probabilistic Dynamics Models”, NeurIPS, 2018 ↩
-
Hafner et al., “Learning Latent Dynamics for Planning from Pixels”, ICLR, 2020 ↩
-
Hafner et al., “Mastering Atari with Discrete World Models”, ICLR, 2022 ↩
-
Hafner et al., “Mastering Diverse Domains through World Models”, Nature, 2025 ↩ ↩2
-
Kaiser et al., “Model-Based Reinforcement Learning for Atari”, ICLR, 2020 ↩