探索与利用:多试一次,什么时候值得?
Exploration and exploitation
区分当前估计与真实收益,用噪声锁定例子比较固定探索和两种递减速度。
exploration-exploitation一次失望的尝试,可能藏住更好的选项
设一台虚构的双按钮机器:A每次固定支付0.4,B以0.8概率支付1,否则支付0。各试一次,恰好看到A:0.4,B:0。如果以后总选择样本均值最大的按钮,就会一直按A。A的均值保持0.4,B的估计保持零,因为规则不再收集它的新观测。
B首次失败的概率为0.2。在这个条件下,此后每按一次A,都损失0.4的期望奖励;再按20次,相比一直选B损失8。无论多精确地计算当前平均值,都无法补出缺失的证据。
这就是探索与利用(exploration–exploitation)问题。利用根据已有信息选看起来不错的动作;探索收集可能改善后续决策的信息。Sutton与Barto工作稿通过动作价值估计解释这种权衡,Silver讲义区分随机探索、乐观估计与概率匹配。按钮例子为原创,不是来源报告的实验。
估计得好,与决策得好,是两件事
在平稳老虎机问题中,K个动作各有一个未知且固定的奖励均值mu_a。第t轮根据此前反馈选择A_t,然后只观察它的奖励R_t。有限时域T内的目标为:
pi表示整套决策规则,包括如何使用历史。信息只有通过改善未来奖励才有价值,目标本身不奖励“为了数据而收集数据”。如果确实只剩最后一次决策,数据也没有其他用途,纯粹为了学习而牺牲期望奖励便没有未来收益。
经验估计不等于未知真均值。用q_t(a)表示第t轮之前的估计,M_t包含全部最大估计动作。epsilon-greedy以epsilon概率在全部动作中均匀探索,否则在M_t内均匀选择:
H_(t−1)表示已经观察的历史。探索也可能选到贪心动作。估计为[0.2,0.5,0.5]、epsilon为0.3时,动作概率为[0.1,0.45,0.45]。只在非贪心动作中均匀探索是另一条规则;若全部估计同分,这里的约定无论epsilon多少都是均匀选择。
对平稳奖励,为每个动作保存计数与样本均值。观察动作a的奖励r后更新:
新均值等于旧总和加r,再除以新计数。没有被选中的动作,其计数与估计均不变。计数为零时的初始化估计只是一种约定,不是观测。下方程序先强制各拉一次,让后续比较不依赖这项约定。工作稿第2.2—2.3节将样本均值、动作选择及增量更新联系起来。
持续探索有成本,停止太快也有风险
设mu_*为最大真均值,Delta_a=mu_*−mu_a为非负差距。初始化之后,epsilon-greedy决策的条件期望差距为:
代入动作概率,再去掉一个非负项,即得下界。只要某个差距为正,固定正epsilon就会带来线性累计探索成本,即使贪心估计后来全都正确。它只是下界,不是总遗憾:若贪心部分误选了臂,还会额外付出成本。Silver的固定epsilon论证使用了这一区分。
减小epsilon并不自动解决问题。三次强制拉取后,比较epsilon_t=1/sqrt(t)和epsilon_t=1/t²。后者剩余的探索概率总和有限。若E_t表示进入随机探索分支,用并集上界和x⁻²的积分可得:
因此,以后再也不进入该分支的概率至少为三分之二。这本身不证明学习会失败——贪心规则可能已经正确——却使“总能修复早期坏估计”失去一般保证。较慢的方案探索概率总和发散,而探索比例趋于零;这些事实仍不能为所有任务建立特定的有限时间遗憾率。
比较探索速度,不把答案交给学习器
将这个独立Python 3程序保存为exploration.py,运行python3 exploration.py;仅使用标准库。合成Bernoulli均值为[0.25,0.45,0.70],学习器根据实际观测估计选择动作。已知均值分别用于环境生成奖励和评估器计算路径伪遗憾。
每个臂有独立种子的奖励流,动作随机化另用一条随机流。四条策略共用0—29的种子标签,便于配对比较,因此策略之间并非独立。对每条策略,30次模拟运行给出均值与标准误;若结果为z_1,…,z_m,计算为:
这表示运行之间的模拟不确定性,不是确定性性能界,也不会自动成为置信区间。每次运行800轮,含三次初始化拉取。程序另外返回开头双按钮轨迹;其中一个臂固定支付0.4,与三个Bernoulli臂的训练实验不同。
import math
import random
import statistics
def integer(x, name, minimum=0):
if isinstance(x, bool) or not isinstance(x, int) or x < minimum:
raise ValueError(name + ' must be an integer >= ' + str(minimum))
return x
def finite(x, name, low=None, high=None):
if isinstance(x, bool) or not isinstance(x, (int, float)) or not math.isfinite(x):
raise ValueError(name + ' must be finite')
if (low is not None and x < low) or (high is not None and x > high):
raise ValueError(name + ' is outside its domain')
return float(x)
def probabilities(values):
p = [finite(v, 'probability', 0, 1) for v in values]
if not p:
raise ValueError('at least one arm is required')
return p
class Bernoulli:
"""The learner receives only pull(action), never the mean vector."""
def __init__(self, means, seed):
self._means = probabilities(means)
integer(seed, 'seed')
self._streams = [random.Random(seed + 104729 * a) for a in range(len(means))]
def pull(self, action):
integer(action, 'action')
if action >= len(self._means):
raise ValueError('unknown action')
return int(self._streams[action].random() < self._means[action])
def summary(values):
if len(values) < 2:
raise ValueError('at least two independent runs are needed for an SE')
return dict(mean=statistics.mean(values), se=statistics.stdev(values) / math.sqrt(len(values)))
def action_probabilities(estimates, epsilon):
q = [finite(x, 'estimate') for x in estimates]
epsilon = finite(epsilon, 'epsilon', 0, 1)
if not q:
raise ValueError('empty estimates')
best = max(q); winners = [a for a, value in enumerate(q) if value == best]
return [epsilon / len(q) + ((1 - epsilon) / len(winners) if a in winners else 0) for a in range(len(q))]
def schedule(name, step):
integer(step, 'step', 1)
if name == 'greedy': return 0.0
if name == 'constant': return 0.1
if name == 'slow': return 1 / math.sqrt(step)
if name == 'fast': return 1 / step**2
raise ValueError('unknown schedule')
def run(name, steps=800, seed=0):
integer(steps, 'steps', 3); integer(seed, 'seed')
means = [.25, .45, .7]; env = Bernoulli(means, seed)
rng = random.Random(seed + 1000003); counts = [0] * 3; q = [0.] * 3
loss = 0.; reward = 0; curve = []
for t in range(1, steps + 1):
eps = schedule(name, t)
a = t - 1 if t <= 3 else rng.choices(range(3), weights=action_probabilities(q, eps))[0]
r = env.pull(a); counts[a] += 1; q[a] += (r - q[a]) / counts[a]
reward += r; loss += max(means) - means[a]
if t in [3, 20, 100, 400, steps]: curve.append(dict(step=t, pseudoregret=loss))
return dict(counts=counts, estimates=q, reward=reward, pseudoregret=loss, curve=curve)
def demo():
comparisons = {}
for name in ['greedy', 'constant', 'slow', 'fast']:
runs = [run(name, seed=s) for s in range(30)]
comparisons[name] = dict(pseudoregret=summary([r['pseudoregret'] for r in runs]), reward=summary([r['reward'] for r in runs]))
# Separate authored path: deterministic arm .4, better Bernoulli .8 fails on first try.
q = [.4, 0.]; counts = [1, 1]; actions = []
for _ in range(20):
a = max(range(2), key=q.__getitem__); actions.append(a)
counts[a] += 1; q[a] += (.4 - q[a]) / counts[a]
return dict(horizon=800, seeds=list(range(30)), comparisons=comparisons,
policy=action_probabilities([.2, .5, .5], .3),
constant_exploration_floor=.1 / 3 * (.7 - .25 + .7 - .45),
lock=dict(actions=actions, estimates=q, counts=counts, initial_bad_event_probability=.2),
single_seed=run('constant', seed=0))
if __name__ == '__main__':
import json
print(json.dumps(demo(), indent=2, allow_nan=False))
平均累计伪遗憾约为:贪心54.4733、固定epsilon0.1方案31.1217、较慢递减20.6567、过快递减54.5767;对应标准误约16.3177、4.1130、3.0529、16.3063。贪心方案的波动较大,这一点不能省略:某次运行看起来很成功,其他种子却可能锁定在较差的臂。
选动作需要扫描K个估计,每轮O(K);选中均值的更新O(1)。学习器保存O(K)计数与估计。演示还保留少量曲线检查点并汇总m次运行,没有存每一条奖励。测试全部奖励流或延长时域会增加真实计算,实验预算也要计入。
怎样衡量不确定性,要看任务
均匀探索把同样的概率分给几乎确定很差的臂,以及有希望但不确定的臂。UCB使用置信指数,汤普森采样使用后验样本;本例这组均值不足以给这些方法排一个普遍名次。
平稳性也很重要。奖励改变后,全历史平均适应较慢;固定更新步长可以提高新奖励的权重,但改变了估计器,也不会单独解决变化检测。在完整MDP中,动作还会改变未来状态和信息,各状态独立随机选动作不一定提供有用覆盖。上面的老虎机速度计算不是关于任意MDP探索的定理。
换种子之前,先作预测
-
两个动作估计不同,epsilon为0.2,当前贪心动作被选的概率是多少?
答案:0.9:利用贡献0.8,均匀探索贡献0.1。若采用只探索非贪心动作的规则,答案会不同。
-
均值为
[0.25,0.45,0.70],epsilon为0.1,初始化后每轮探索成本的下界是多少?答案:差距为0.45、0.25、0,得到
0.1×0.70/3 ≈ 0.0233333。贪心部分选错会让总期望损失更大。 -
探索概率总和有限,是否证明学习器会选错臂?
答案:不是。它允许未来探索不足,却没有说明当前估计已经错了。双按钮例中,首次失败先造成问题,始终贪心才使它保持下去。
-
较慢递减方案赢了这次30种子比较;在称它普遍更好之前,还要改什么?
答案:测试不同差距、奖励噪声、时域和种子,说明比较指标与不确定性。若作理论主张,应明确假设并证明相应速度;重复同一个有利实例不能代替一般论证。
随机选择只是获取信息的一种办法
Sutton与Barto用简单老虎机分离出评价性反馈,Silver将epsilon-greedy放在乐观估计、概率匹配旁比较。共同的问题是:哪些信息足以改变未来决策,从而值得付出成本?本文比较基础探索速度,精确贝叶斯规划与遗憾最优算法另有各自的问题。
先判断,再看解释
两个动作估计不同,epsilon为0.2;当前贪心动作被选的概率是多少?
学习目标
- 计算含同分及探索时仍选最优估计动作的epsilon概率。
- 区分固定探索成本、探索不足与有限种子实验证据。
先修与延伸
查看知识关联图 →参考文献
- Reinforcement Learning: An Introduction — second-edition working draft (2014–2015)
Richard S. Sutton; Andrew G. Barto · 2015 - Lecture 9: Exploration and Exploitation
David Silver · 2015
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-23 · Single AI author. Scoped reading: PDF46–48 and50: greedy/epsilon feedback tradeoff and task-dependent examples;55 distinguishes directed uncertainty exploration. Read2026-09-23; same2014–2015 working-draft Source retained. Author teaching page dates the course2015, not its2025 upload. Read PDF1,3–20,22,24–25,28,30,33; directly viewed20. Its bottom regret formula uses gaps instead of inverse gaps; use Auer Theorem1, not this slide, for the bound. Constant exploration, probability matching and contextual feedback only; no universal TS optimality claim. Alpha/beta success conventions follow Russo, not the reversed slide28/30 labels. Link-only, no slide reuse. 6 paired equations and four explained exercises. Checked epsilon probabilities including maximizing ties, selected greedy actions during exploration, independent arm streams, forced initialization, gap-count losses, seeded standard errors and a separate deterministic-arm lock. Summable exploration is not itself a failure theorem.
- 核epsilon同分概率、探索仍选贪心、逐臂随机流、强制初始化、gap计数损失、固定种子标准误及独立固定奖励臂锁定;探索可求和不直接证明失败。
- 每题四张计算SVG,12数值/几何与30响应式组合通过;10张当前选定图核看,7张最终图直接查看,3张与此前所看图字节一致并实际核对;浏览器输入由插图前SHA绑定,完整页面验收另记。
核对的来源位置:
- Reinforcement Learning: An Introduction — second-edition working draft (2014–2015) · PDF46–48 and50: greedy/epsilon feedback tradeoff and task-dependent examples;55 distinguishes directed uncertainty exploration. Read2026-09-23; same2014–2015 working-draft Source retained.
- Lecture 9: Exploration and Exploitation · Author teaching page dates the course2015, not its2025 upload. Read PDF1,3–20,22,24–25,28,30,33; directly viewed20. Its bottom regret formula uses gaps instead of inverse gaps; use Auer Theorem1, not this slide, for the bound. Constant exploration, probability matching and contextual feedback only; no universal TS optimality claim. Alpha/beta success conventions follow Russo, not the reversed slide28/30 labels. Link-only, no slide reuse.
相关主题