逆向强化学习
逆向强化学习(Inverse Reinforcement Learning, IRL)旨在从专家演示中推断潜在的奖励函数,是连接模仿学习与强化学习的重要桥梁。[^1]
问题定义
核心问题
给定:
- 专家演示轨迹:
- 其中
- 环境模型(转移概率):
目标:
- 恢复专家隐式使用的奖励函数
- 或直接学习模仿专家行为的策略
与模仿学习的对比
| 方面 | 模仿学习 | 逆向强化学习 |
|---|---|---|
| 学习目标 | 模仿行为 | 恢复奖励 |
| 可解释性 | 低 | 高 |
| 泛化能力 | 有限 | 较强 |
| 样本需求 | 较少 | 较多 |
最大边际 IRL (Max-Margin IRL)
开创性工作
由 Ng & Russell 于2000年提出,是IRL领域的奠基性工作。[^2]
核心思想
寻找一个奖励函数,使得专家策略的累积奖励严格高于其他所有策略:
数学形式化
假设奖励函数是特征的线性组合:
其中 是特征向量。
优化问题:
\min_{\theta, \xi} \quad & \frac{1}{2}\|\theta\|^2 + C\sum_i \xi_i \\ \text{s.t.} \quad & \theta^\top \mu(\pi_E) \geq \theta^\top \mu(\pi_i) + 1 - \xi_i, \quad \forall i \end{aligned}$$ 其中 $\mu(\pi) = \mathbb{E}_\pi[\sum_{t=0}^\infty \gamma^t \phi(s_t, a_t)]$ 是特征期望。 ### 算法步骤 ```python class MaxMarginIRL: def __init__(self, feature_dim, lr=0.01): self.theta = np.zeros(feature_dim) self.lr = lr self.C = 1.0 # 正则化参数 def compute_feature_expectation(self, trajectories): """计算特征期望""" fe = np.zeros(len(self.theta)) for tau in trajectories: for t, (s, a) in enumerate(tau): fe += self.gamma ** t * self.features(s, a) return fe / len(trajectories) def find_policy(self, theta): """给定奖励函数,求解最优策略(需要MDP模型)""" # 使用策略迭代或值迭代 reward = lambda s, a: theta @ self.features(s, a) return self.solve_mdp(reward) def update(self, expert_fe, policy_fe): """更新权重""" diff = expert_fe - policy_fe self.theta += self.lr * diff # 投影到可行区域 ``` --- ## 学徒学习 (Apprenticeship Learning) ### Abbeel & Ng (2004) 通过匹配**特征期望**来学习策略,保证性能不低于专家。[^3] ### 核心思想 直接学习策略而非显式恢复奖励函数,通过迭代逼近专家的特征期望。 ### 算法流程 ``` 输入: 专家演示 {τ_1, ..., τ_m} 输出: 策略 π 1. 计算专家特征期望: μ_E = average_feature_expectation(expert_trajectories) 2. 初始化: θ^0 = 0 3. 迭代: a. 使用当前 θ^t 求解MDP,得到策略 π^t b. 计算 μ(π^t) = feature_expectation(π^t) c. 如果 ||μ(π^t) - μ_E|| 太接近: 返回 π^t (收敛) d. 否则更新 θ^{t+1} 朝向 μ_E - μ(π^t) ``` ### 性能保证 **定理**:学徒学习返回的策略 $\pi$ 满足: $$\mathbb{E}[R(\pi)] \geq \mathbb{E}[R(\pi_E)] - \epsilon$$ --- ## 最大熵 IRL (Maximum Entropy IRL) ### Ziebart et al. (2008) 使用最大熵原理处理多个可能的奖励函数问题。[^4] ### 核心思想 当多条轨迹都符合专家演示时,选择**熵最大**的分布。 ### 数学框架 在最大熵IRL中,轨迹的概率定义为: $$P(\tau | \theta) = \frac{1}{Z(\theta)} \exp(R_\theta(\tau))$$ 其中归一化常数(配分函数): $$Z(\theta) = \sum_\tau \exp(R_\theta(\tau)) = \sum_\tau \exp\left(\sum_{t=0}^T \theta^\top \phi(s_t, a_t)\right)$$ ### 梯度推导 对数似然梯度: $$\nabla_\theta \mathcal{L} = \mathbb{E}_{\tau \sim \pi_E}[\nabla_\theta R_\theta(\tau)] - \mathbb{E}_{\tau \sim P(\tau|\theta)}[\nabla_\theta R_\theta(\tau)]$$ 即: $$\nabla_\theta \mathcal{L} = \mu_E - \mu_\theta$$ 其中 $\mu_E$ 是专家特征期望,$\mu_\theta$ 是当前策略的特征期望。 ### 算法实现 ```python class MaxEntIRL: def __init__(self, feature_dim, gamma=0.99): self.theta = np.zeros(feature_dim) self.gamma = gamma def compute_expert_feature(self, expert_trajectories): """计算专家特征期望""" n_samples = len(expert_trajectories) expert_fe = np.zeros_like(self.theta) for tau in expert_trajectories: for t, (s, a) in enumerate(tau): expert_fe += (self.gamma ** t) * self.features(s, a) return expert_fe / n_samples def compute_policy_feature(self, policy, env, n_samples=1000): """通过采样估计策略特征期望""" policy_fe = np.zeros_like(self.theta) for _ in range(n_samples): s, _ = env.reset() done = False t = 0 while not done: a = policy(s) policy_fe += (self.gamma ** t) * self.features(s, a) s, _, done, _, _ = env.step(a) t += 1 return policy_fe / n_samples def update(self, expert_fe, policy_fe, lr=0.01): """梯度更新""" self.theta += lr * (expert_fe - policy_fe) def reward(self, s, a): return np.dot(self.theta, self.features(s, a)) ``` --- ## 引导成本学习 (Guided Cost Learning) ### Finn et al. (2016) 将最大熵IRL扩展到高维、连续控制问题。[^5] ### 核心创新 1. **深度网络表示**:使用神经网络近似奖励函数 2. **重要性采样**:估计配分函数 $Z(\theta)$ 3. **无模型**:不需要已知环境动力学 ### 算法框架 ```python class GuidedCostLearning: def __init__(self, reward_net, policy_net): self.reward_net = reward_net # 深度网络 self.policy_net = policy_net def train_step(self, expert_trajectories, policy_trajectories): # 1. 更新奖励网络(识别专家 vs 策略) expert_rewards = [sum(self.reward_net(s, a) for s, a in tau) for tau in expert_trajectories] policy_rewards = [sum(self.reward_net(s, a) for s, a in tau) for tau in policy_trajectories] # 2. 分类器损失 reward_loss = self.compute_classifier_loss(expert_rewards, policy_rewards) # 3. 更新策略(使用当前奖励) for tau in policy_trajectories: for s, a in tau: policy_loss = -self.reward_net(s, a) return reward_loss, policy_loss ``` --- ## 贝叶斯 IRL ### Ramachundran & Amir (2007) 使用贝叶斯方法处理IRL的不确定性。 ### 核心思想 维护奖励函数的后验分布: $$P(R | \mathcal{D}) \propto P(\mathcal{D} | R) P(R)$$ **先验**:对奖励函数使用高斯先验 **似然**:基于最大熵框架 --- ## 算法对比 | 算法 | 年份 | 特点 | 局限性 | |------|------|------|---------| | Max-Margin IRL | 2000 | 理论优美 | 需要完整MDP模型 | | Apprenticeship | 2004 | 性能保证 | 仅特征匹配 | | MaxEnt IRL | 2008 | 处理歧义 | 配分函数难计算 | | GCL | 2016 | 深度网络 | 需大量采样 | | Bayesian IRL | 2007 | 不确定性 | 计算复杂 | --- ## 应用场景 ### 自动驾驶 - 车道汇入/汇出学习 - 跟车行为恢复 - 路径规划偏好 ### 机器人控制 - 人类演示模仿 - 柔顺控制策略 - 社交导航 ### 游戏AI - 策略恢复 - 风格迁移 --- ## 参考资料 [^1]: Zhizheng Liu et al., "A Survey of Inverse Reinforcement Learning: Challenges, Methods and Progress", 2025 [^2]: Ng & Russell, "Algorithms for Inverse Reinforcement Learning", ICML, 2000 [^3]: Abbeel & Ng, "Apprenticeship Learning via Inverse Reinforcement Learning", ICML, 2004 [^4]: Ziebart et al., "Maximum Entropy Inverse Reinforcement Learning", AAAI, 2008 [^5]: Finn et al., "Guided Cost Learning: Deep Inverse Optimal Control via Policy Optimization", ICML, 2016 --- ## 相关链接 - [[gail-generative-adversarial-irl|生成对抗模仿学习]] — GAIL与对抗IRL - [[imitation-learning|模仿学习]] — 行为克隆与DAgger - [[rlhf|RLHF]] — 人类反馈强化学习