探索与记忆
一句话定位
在 model-based RL 中,有效的探索策略和记忆机制是解决部分可观测环境和提高数据效率的关键,内在动机驱动的探索能显著提升世界模型质量。
前置依赖
- 强化学习基础(探索-利用权衡、ε-greedy、UCB)
- 世界模型基础
- 1-Model-based RL总览
核心思想
探索与记忆是 MBRL 中两个紧密相关的问题:
- 探索问题:如何高效地收集多样化经验来改进世界模型
- 记忆问题:如何在部分可观测环境中利用历史信息构建完整状态表示
内在动机(Intrinsic Motivation) 是驱动探索的核心思想:
其中
模型结构图
┌─────────────────────────────────────────────────────────────┐
│ 探索与记忆增强的 World Model 架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ 记忆模块 Memory │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │ │
│ │ │ Working │ │ Episodic│ │ Procedural │ │ │
│ │ │ Memory │ │ Memory │ │ Memory │ │ │
│ │ │ (LSTM) │ │ (Keys- │ │ (Replay Buffer)│ │ │
│ │ │ │ │ Values) │ │ │ │ │
│ │ └─────────┘ └─────────┘ └─────────────────┘ │ │
│ └───────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ 世界模型 World Model │ │
│ │ p(s'|s, a, memory), p(r|s, a) │ │
│ │ 需要记忆来恢复完整状态 │ │
│ └───────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ 探索模块 Exploration │ │
│ │ ┌─────────────┐ ┌─────────────────────────────┐ │ │
│ │ │ Intrinsic │ │ Exploration Bonus │ │ │
│ │ │ Reward │ │ R = r + α·r_int │ │ │
│ │ └─────────────┘ └─────────────────────────────┘ │ │
│ └───────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
数学推导
1. 世界模型如何与探索奖励交互
总奖励分解:
其中
世界模型在探索中的作用:
- 提供
的计算基础 - 识别模型不确定性
- 指导朝向未知区域的探索
2. 预测误差驱动的内在奖励
Curiosity-based Exploration:
设世界模型预测为
或者用
Eigenerror 版本(更稳定):
其中
3. 访问计数(Visit Count)奖励
基于计数的探索:
状态访问计数
或者基于伪计数:
其中
4. POMDP 中的记忆机制
部分可观测环境的数学形式:
- 真实状态
- 观测
, 或 - 动作
- 回报
记忆增强的世界模型:
其中
5. 记忆增强的规划
Memory-Predicate Neural Network:
记忆查询:
query = encoder(o_t)
memory_output = attention(query, memory_keys, memory_values)
z_t = combine(z_t, memory_output)
完整的状态更新:
6. 内在动机与 MBRL 的结合
ICM (Intrinsic Curiosity Module):
ICM 同时学习:
- 世界模型:
- 反向模型:
- 内在奖励:
Forward Model Loss:
总损失:
7. 记忆如何帮助部分可观测环境
问题定义:
对于 POMDP,智能体无法直接观测完整状态
记忆增强的好处:
- 状态推断:通过记忆恢复被遮挡的信息
- 时序依赖:利用历史动作-观测序列
- 长程依赖:处理跨时间步的因果关系
Memory Bank 的实现:
class MemoryBank:
def __init__(self, size=10000, key_dim=64, value_dim=128):
self.keys = np.zeros((size, key_dim))
self.values = np.zeros((size, value_dim))
self.count = np.zeros(size)
self.ptr = 0
def add(self, key, value):
"""添加记忆"""
self.keys[self.ptr] = key
self.values[self.ptr] = value
self.count[self.ptr] = 1
self.ptr = (self.ptr + 1) % len(self.keys)
def query(self, query, top_k=5):
"""查询记忆"""
similarities = query @ self.keys.T
top_indices = np.argsort(similarities)[-top_k:]
return self.values[top_indices], self.count[top_indices]训练细节
Exploration 策略分类
| 策略 | 描述 | 适用场景 |
|---|---|---|
| 随机探索 | ε-greedy, Gaussian | 简单任务 |
| 内在奖励 | curiosity, count-based | 复杂探索 |
| 基于不确定 | ensemble disagreement | 模型不确定性高 |
| 信息增益 | Bayesian exploration | 需要模型不确定性 |
| 技能发现 | DIAYN, DICER | 多模态任务 |
内在奖励的设计
设计原则:
- 可计算性:内在奖励必须高效可计算
- 可微性:最好可微以支持 policy gradient
- 归一化:内在奖励需要归一化到与外激励相当
- 去相关:避免外激励与内在激励重叠
典型参数设置:
| 参数 | 典型值 | 说明 |
|---|---|---|
| 0.01-0.1 | 内在奖励权重 | |
| 0.99 | 内在奖励折扣 | |
| 0.01-0.1 | 熵正则权重 |
记忆增强的训练
** Episodic Memory:**
- 存储每个 episode 的关键观测
- 使用 key-value attention 检索
- 与 working memory 结合
Procedural Memory(Replay Buffer):
- 存储所有经验的压缩表示
- 用于训练世界模型
- 使用_prioritized_ replay 优先采样模型误差大的样本
推理/rollout/planning 过程
探索驱动的推理
class ExplorationMBRL:
def __init__(self, world_model, curiosity_module, memory_bank):
self.model = world_model
self.curiosity = curiosity_module
self.memory = memory_bank
def select_action(self, obs, epsilon=0.01):
"""带探索的动作选择"""
z = self.model.encode(obs)
# 检查记忆
if self.memory.size > 0:
memory_context = self.memory.query(z)
z = combine(z, memory_context)
# 以 epsilon 概率随机探索
if np.random.random() < epsilon:
return self.random_action()
# 计算好奇心奖励
if self.last_state is not None:
r_int = self.curiosity.compute(self.last_state, self.last_action, z)
else:
r_int = 0
# 利用世界模型规划
a = self.planner.plan(z, intrinsic_reward=r_int)
return a
def update(self, obs, action, reward, obs_next):
"""更新记忆和世界模型"""
z = self.model.encode(obs)
z_next = self.model.encode(obs_next)
# 更新好奇心模块
self.curiosity.update(self.last_state, action, z_next)
# 更新记忆
self.memory.add(z, obs)
# 更新世界模型
self.model.update(z, action, reward, z_next)
self.last_state = z
self.last_action = actionMemory-augmented Planning
规划时考虑历史:
1. 编码当前观测 o_t
2. 查询 episodic memory 获取相关历史
3. 结合 working memory (LSTM hidden state)
4. 构建完整状态 z_t = combine(o_t, memory, h)
5. 基于 z_t 进行规划
优点与局限
优点
- 更好的探索:内在动机引导智能体探索未知区域
- 更完整的表示:记忆帮助恢复被遮挡的信息
- 更高的数据效率:优先探索高价值/高不确定性区域
- 处理 POMDP:记忆机制使 MBRL 适用于部分可观测环境
局限
- 内在奖励设计困难:设计不当可能导致 سلوك 不稳定
- 记忆开销:大规模记忆增加计算和存储成本
- 遗忘问题:简单记忆可能遗忘重要历史
- 探索-利用平衡:内在奖励可能过度探索
与前后内容的衔接
- 前置:6-连续控制中的world model - 连续控制的探索挑战
- 后置:4-强化学习应用 - 世界模型在 RL 中的应用
可复现实现要点
import torch
import torch.nn as nn
import numpy as np
class CuriosityModule(nn.Module):
"""好奇心驱动的内在奖励"""
def __init__(self, obs_dim, action_dim, hidden_dim=256):
super().__init__()
# Forward model: 预测下一状态
self.forward_net = nn.Sequential(
nn.Linear(obs_dim + action_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, obs_dim)
)
def compute_intrinsic_reward(self, obs, action, next_obs):
"""计算好奇心奖励"""
pred_next = self.forward_net(torch.cat([obs, action], dim=-1))
error = torch.norm(pred_next - next_obs, dim=-1)
return error
class MemoryBank(nn.Module):
"""记忆银行"""
def __init__(self, size=10000, key_dim=128, value_dim=128):
super().__init__()
self.size = size
self.keys = torch.zeros(size, key_dim)
self.values = torch.zeros(size, value_dim)
self.counts = torch.zeros(size)
self.ptr = 0
def add(self, key, value):
self.keys[self.ptr] = key
self.values[self.ptr] = value
self.counts[self.ptr] = 1
self.ptr = (self.ptr + 1) % self.size
def query(self, query, top_k=5):
"""查询最近的记忆"""
similarities = query @ self.keys[:self.ptr].T
top_indices = torch.argsort(similarities, dim=-1)[-top_k:]
return self.values[top_indices], self.counts[top_indices]
class ExplorationMBRL:
def __init__(self, world_model, curiosity_module, memory_bank):
self.model = world_model
self.curiosity = curiosity_module
self.memory = memory_bank
self.alpha = 0.05 # 内在奖励权重
def update(self, obs, action, next_obs, extrinsic_reward):
"""更新并计算总奖励"""
r_int = self.curiosity.compute_intrinsic_reward(obs, action, next_obs)
r_total = extrinsic_reward + self.alpha * r_int
# 更新各模块
self.model.update(obs, action, next_obs)
self.curiosity.update(obs, action, next_obs)
self.memory.add(self.model.encode(obs), obs)
return r_total章节摘要
探索与记忆是 MBRL 的两个核心问题。探索通过内在动机(curiosity)驱动智能体主动探索未知区域,从而提高世界模型质量;记忆机制使智能体能够在部分可观测环境中利用历史信息构建完整状态表示。Curiosity-based exploration 通过预测误差驱动探索,记忆增强的世界模型通过 key-value attention 等机制检索历史信息。二者结合使 MBRL 能够处理更复杂的实际任务。
关键词
exploration, intrinsic motivation, curiosity, memory, POMDP, episodic memory, working memory, replay buffer, visit count, ICM, curiosity-driven exploration, model-based RL, uncertainty exploration, information gain