F7EasyPart F · 大模型对齐与强化学习
多臂老虎机探索策略(epsilon-Greedy 与 UCB1)
强化学习与推荐系统探索/利用(Exploration vs Exploitation)核心算法,置信区间上界打分。
⏱️ 时间复杂度:
O(K) 决策单步💾 空间复杂度:
O(K)🎯 免登录实战自测闭环(先做再看答案)
按「机制回忆 → 关键行填空 → 边界反例 → 追问与错题记录」顺序自测,进度自动保存在本地。
Step 1 · 机制口述与数值稳定性自检
Step 2 · 核心代码关键行补全
import numpy as np
class UCB1Agent:
def __init__(self, num_arms: int, c: float = 1.0):
self.num_arms = num_arms
self.c = c
self.counts = np.zeros(num_arms, dtype=int)
self.values = np.zeros(num_arms, dtype=np.float64)
self.total_steps = 0
def select_arm(self) -> int:
self.total_steps += 1
# 前 num_arms 步每个臂先试一次
for arm in range(self.num_arms):
if self.counts[arm] == 0:
return arm
# 计算每个臂的 UCB 打分
exploration =
ucb_scores = self.values + exploration
return
def update(self, arm: int, reward: float):
self.counts[arm] += 1
# 增量更新均值
n = self.counts[arm]
self.values[arm] += (reward - self.values[arm]) / n
Step 3 · 反例与边界断言核对
💡
实战一句话记忆口诀
经验均值算底薪,尝试次数算奖金,探索少了置信大,乐观决策挑上界
📐 核心数学公式与机理推导
### 面对不确定性的乐观探索(Optimism in the Face of Uncertainty)
在多臂老虎机(MAB)中,选择一个臂 获得奖励:
- $\epsilon$-Greedy 策略:以 的概率选择当前历史经验均值最高的臂(利用),以 概率完全随机瞎猜一个臂(盲目探索);
- UCB1 策略(Upper Confidence Bound):
基于霍夫丁不等式(Hoeffding's Inequality)建立理论置信区间:
总打分 = 历史经验均值 + 不确定性置信宽度 。
其中 。若某个臂被尝试次数 很少,分母极小使得探索奖励暴增,系统会优先给予其尝试机会;一旦尝试多次后置信区间收窄,打分回归真实均值。
在多臂老虎机(MAB)中,选择一个臂 获得奖励:
- $\epsilon$-Greedy 策略:以 的概率选择当前历史经验均值最高的臂(利用),以 概率完全随机瞎猜一个臂(盲目探索);
- UCB1 策略(Upper Confidence Bound):
基于霍夫丁不等式(Hoeffding's Inequality)建立理论置信区间:
总打分 = 历史经验均值 + 不确定性置信宽度 。
其中 。若某个臂被尝试次数 很少,分母极小使得探索奖励暴增,系统会优先给予其尝试机会;一旦尝试多次后置信区间收窄,打分回归真实均值。
🔄 张量维度与形状流转说明
counts, values -> ucb = values + c * sqrt(2*ln(t)/counts) -> argmax -> arm 选择
🛡️ 数值稳定性自检与边界要点
- 未被探索过的臂(count=0)置信度无穷大,必须优先至少初始化拉动一次
- 在推荐系统动态场景中,可用汤普森采样(Thompson Sampling)作为贝叶斯平滑替代
💻 完整参考代码实现
import numpy as np
class UCB1Agent:
def __init__(self, num_arms: int, c: float = 1.0):
self.num_arms = num_arms
self.c = c
self.counts = np.zeros(num_arms, dtype=int)
self.values = np.zeros(num_arms, dtype=np.float64)
self.total_steps = 0
def select_arm(self) -> int:
self.total_steps += 1
# 前 num_arms 步每个臂先试一次
for arm in range(self.num_arms):
if self.counts[arm] == 0:
return arm
# 计算每个臂的 UCB 打分
exploration = self.c * np.sqrt(2.0 * np.log(self.total_steps) / self.counts)
ucb_scores = self.values + exploration
return int(np.argmax(ucb_scores))
def update(self, arm: int, reward: float):
self.counts[arm] += 1
# 增量更新均值
n = self.counts[arm]
self.values[arm] += (reward - self.values[arm]) / n
🧪 本地测试与正确性断言
可直接复制到本地 Python / Jupyter 验证:
import numpy as np
agent = UCB1Agent(num_arms=3)
# 模拟拉动
for _ in range(30):
arm = agent.select_arm()
# 臂 2 真实收益最高 (0.9)
reward = 1.0 if (arm == 2 and np.random.rand() < 0.9) else 0.0
agent.update(arm, reward)
assert agent.counts[2] >= agent.counts[0], "收益最高臂应被探索更多"
print("✓ UCB1 探索策略自测通过")🎯 核心工程追问与标准应答
Q1:汤普森采样(Thompson Sampling)与 UCB1 的核心哲学区别是什么?
UCB1 是基于频率派统计的置信区间上界确定性决策;而汤普森采样是贝叶斯派方法,为每个臂维护一个奖励概率分布(如 Beta 分布),每次决策时直接从后验分布中各抽取一个随机样本取最大值,计算更加平滑且自然融入先验知识。
⚡
想检验本题的肌肉记忆?进入 3 阶闪卡主动回忆盲测
隐藏答案遮罩,自测:审题锚点 ➔ 数理公式机理 ➔ 工业标杆代码与连环追问。3 分钟攻破认知盲区!