TD3:两位评论家与慢半拍的演员
Twin delayed DDPG
拆开双Q最小值、目标动作平滑与延迟更新,并实测它们改变了哪些计算。
td3两位评论家,为什么还不能放心?
先看一个虚构的控制任务:真实动作价值为1,两位评论家分别给1.1和1.2。取较小值仍然高估。若它们分别给0.7和1.2,取较小值又会低估。TD3 的双评论家不是一个带置信保证的区间;它用更保守的训练目标,减少演员追逐虚假高峰的机会。
TD3 在DDPG上做三处修改:备份使用两位目标评论家的较小值;给目标动作加小噪声;演员与目标网络更新得比评论家慢。三处分别作用于价值估计、动作附近的平滑和更新时序。只加第二个Q网络,还不能算完整TD3。原论文
论文发表于ICML2018。其结果属于作者报告的实验条件,本文用同一个两步教学环境对照DDPG,不把本地小任务称为论文复现。出版与方法
沿着一条转移看完三处改动
从回放取 , 表示真实终止。先用目标演员在下一状态产生动作,再执行两次不同的裁剪:内层限制噪声,外层满足环境动作边界。多维动作按分量处理:
例如目标动作0.95、原始噪声0.8、噪声上限0.2、动作上限1,先得到0.95+0.2=1.15,再裁成1。只裁噪声仍会越界;只裁最终动作则可能施加过大的局部扰动。这个噪声用来计算备份,不是送进环境的探索动作,两者可以有不同标准差。平滑与实现 两次裁剪
两位目标评论家必须在同一个下一状态、同一个扰动动作上评分。用较小值构造一个共同目标,分别拟合在线评论家:
目标计算不反传;真实终止时仅剩奖励。两网络各有参数,但共享数据与目标,不满足“互相独立”这个更强条件。最小值不可能大于任意一个输入,只能由此推出相对于这些输入更保守,不能推出小于真实Q。相关误差与低估
经典TD3演员优化第一位在线评论家,不对双Q取最小值:
这是与SAC演员公式的一个具体区别。评论家参数在演员步骤固定,动作导数仍保留。演员看到的是本次评论家更新后的值。演员更新
用 表示已完成的评论家批更新次数, 为正整数。每次都更新两位评论家,仅在相应时钟触发时更新演员和全部目标副本:
本例从第 次评论家更新开始触发,并在所有采集批次之间保留计数。固定上游实现使用每段更新循环从0开始的局部计数;默认配置相容,但随意改变每段长度后,不一定等价于持久时钟。比较实现时要说明计数的含义。固定实现
平滑为何可能有帮助?先只考察单个评论家、远离动作边界、未裁剪的零均值小噪声。在二阶Taylor近似下:
尖锐峰顶的二阶导数为负,周围平均值低于峰顶。实际TD3有裁剪、边界、有限采样和双Q最小值,不能把这个局部近似当作它的精确备份。它解释的是平滑动机,不是所有环境都能改善的证明。目标平滑
把三个开关放进真实训练循环
沿用DDPG篇的原创两步环境:初始 均匀采样于 ,每步奖励为 ,下一位置为 ,第二步后终止。演员是每阶段一个tanh线性模型,评论家各有六个二次特征。它们是低维教学模型,不是深网络基准。
默认训练6000步、前200步均匀探索、容量2000回放、批量32、折扣0.9。探索噪声标准差0.3;目标平滑标准差0.1,噪声裁剪0.2,动作裁剪到 。评论家学习率0.04,演员0.02,目标系数0.01,延迟2。随机初始化、探索、回放与目标平滑各自使用固定种子的随机流。
"""Original TD3 control lesson; same environment as DDPG, quadratic critics."""
import json
import math
import random
def clip(x, low=-1.0, high=1.0):
return min(high, max(low, x))
def features(x, a):
return [1.0, x, a, x*x, x*a, a*a]
def value(w, x, a):
return sum(u*v for u, v in zip(w, features(x, a)))
def action_gradient(w, x, a):
return w[2] + w[4]*x + 2*w[5]*a
def policy(w, x):
return math.tanh(w[0]*x + w[1])
def transition(phase, x, a):
if phase not in (0, 1) or not -1 <= a <= 1:
raise ValueError('phase must be 0/1 and action in [-1,1]')
return 1.0-(a-0.5*x)**2, 0.5*(x+a), phase == 1
def bootstrap(reward, future, terminated, truncated=False, gamma=0.9):
# External collection limits do not erase the continuation value.
# A real terminal state takes precedence even if both flags are true.
return reward if terminated else reward + gamma*future
def evaluate(actor):
returns = []
for i in range(101):
x = -1.0 + 2.0*i/100
total = 0.0
for h in (0, 1):
a = policy(actor[h], x)
r, x, _ = transition(h, x, a)
total += (0.9**h)*r
returns.append(total)
return sum(returns)/len(returns)
def smooth_action(action, noise, noise_clip=0.2):
if noise_clip < 0:
raise ValueError('noise clip must be nonnegative')
return clip(action + clip(noise, -noise_clip, noise_clip))
def actor_due(critic_updates, delay):
if not isinstance(delay, int) or isinstance(delay, bool) or delay < 1:
raise ValueError('delay must be a positive integer')
return critic_updates > 0 and critic_updates % delay == 0
def train(seed=0, steps=6000, delay=2):
actor_due(0, delay)
explore, replay_rng = random.Random(seed), random.Random(seed+10000)
init = random.Random(seed+20000)
smoothing = random.Random(seed+30000)
actor = [[0.0, 0.0] for _ in range(2)]
critic = [[init.uniform(-0.05, 0.05) for _ in range(6)] for _ in range(2)]
critic2 = [[init.uniform(-0.05, 0.05) for _ in range(6)] for _ in range(2)]
target2 = [w[:] for w in critic2]
target_actor = [w[:] for w in actor]
target_critic = [w[:] for w in critic]
replay, capacity, tau = [], 2000, 0.01
initial = evaluate(actor)
phase, x, updates = 0, explore.uniform(-1, 1), 0
actor_updates = 0
for t in range(steps):
a = explore.uniform(-1, 1) if t < 200 else clip(policy(actor[phase], x)+explore.gauss(0, 0.3))
r, nx, terminal = transition(phase, x, a)
item = (phase, x, a, r, nx, terminal)
if len(replay) < capacity:
replay.append(item)
else:
replay[t % capacity] = item
phase, x = (0, explore.uniform(-1, 1)) if terminal else (1, nx)
if t < 200:
continue
batch = [replay_rng.choice(replay) for _ in range(32)]
gradients = [[[0.0]*6 for _ in range(2)] for _ in range(2)]
for h, sx, act, reward, next_x, done in batch:
future = 0.0
if not done:
na = smooth_action(policy(target_actor[1], next_x), smoothing.gauss(0, 0.1))
future = min(value(target_critic[1], next_x, na), value(target2[1], next_x, na))
y = bootstrap(reward, future, done)
for k, q in enumerate((critic, critic2)):
error = value(q[h], sx, act)-y
for j, f in enumerate(features(sx, act)):
gradients[k][h][j] += 2*error*f/len(batch)
for k, q in enumerate((critic, critic2)):
for h in (0, 1):
q[h] = [w-0.04*g for w, g in zip(q[h], gradients[k][h])]
updates += 1
if actor_due(updates, delay):
ag = [[0.0]*2 for _ in range(2)]
for h, sx, *_ in batch:
act = policy(actor[h], sx)
dz = action_gradient(critic[h], sx, act)*(1-act*act)
ag[h][0] += dz*sx/len(batch)
ag[h][1] += dz/len(batch)
for h in (0, 1):
actor[h] = [w+0.02*g for w, g in zip(actor[h], ag[h])]
target_actor[h] = [(1-tau)*v+tau*w for v, w in zip(target_actor[h], actor[h])]
for qt, q in ((target_critic, critic), (target2, critic2)):
qt[h] = [(1-tau)*v+tau*w for v, w in zip(qt[h], q[h])]
actor_updates += 1
return {'seed':seed, 'initial':initial, 'final':evaluate(actor), 'optimum':1.9,
'critic_updates':updates, 'actor_updates':actor_updates,
'actor':actor, 'critic':critic, 'critic2':critic2, 'target_actor':target_actor}
if __name__ == '__main__':
print(json.dumps(train(), sort_keys=True))
查看完整运行结果
{"actor": [[0.5232718313788149, -0.0007796796527325128], [0.5086183652401205, -0.0009166998126641008]], "actor_updates": 2900, "critic": [[1.8869688017392403, -0.00039767402824984684, -0.0009110872288137134, -0.24113535516044643, 0.9729107049799418, -0.9779522879107702], [0.9969185013450774, 0.00045395050038467554, -0.001278276659474881, -0.23464326958515813, 0.957136870694855, -0.9713519620899428]], "critic2": [[1.8869708033014703, -0.0003977828419043931, -0.0009108852585462347, -0.24114390620903162, 0.9729361013395702, -0.9779720127070964], [0.996997855431188, 0.0004404197732804965, -0.001246455632007738, -0.23507418502130198, 0.9582090541959876, -0.9720174064417539]], "critic_updates": 5800, "final": 1.8999386953554782, "initial": 1.795875, "optimum": 1.9, "seed": 0, "target_actor": [[0.523085656155751, -0.0006567184456597295], [0.5082779866306146, -0.0007494336540563322]]}5800次评论家批更新对应2900次演员更新;每次触发时同时软更新目标演员与两个目标评论家。程序检查了首次触发前目标不变、触发时系数正确、动作两次裁剪、真实终止优先,以及解析动作梯度与有限差分一致。均匀回放有放回抽样,不声称样本相互独立。
种子0—7中,TD3固定网格、无探索评估的平均回报为1.899931,范围约1.899908—1.899942;同任务DDPG平均为1.899931。二者都接近解析上界1.9,这个实验没有显示有意义的TD3回报优势。延迟确实减少了演员更新次数,却额外训练第二位评论家,不能仅按演员次数断言总耗时减半。固定模型大小时每次更新仍随批量线性增长;额外评论家增加模型与梯度计算成本,回放容量不需要翻倍。
相关误差、低估与改变后的目标
取两个零均值误差,各以相同概率为 ±0.2。若它们独立,四种组合的最小误差平均为−0.1;若两误差完全相同,平均最小误差为0:
因此网络数量本身不能告诉你偏差减少多少,低估也不是免费收益。原论文讨论了共享回放造成的相关性;本文没有把其有限情形证明推广到任意神经网络。误差讨论
目标动作噪声还改变了备份所评价的后续动作分布。边界处裁剪会让扰动不再对称;过强平滑可能抹掉确实有用的窄动作区域。演员更新更慢也可能拖慢适应。DDPG的分布覆盖、评论家动作导数错误与离策略近似仍然存在。
论文PDF第6页算法框的目标动作使用了 ,而同页式14/15使用 ;条件简写成“if t mod d”,式13还未写折扣。本文按Bellman关系、式14/15和固定实现统一为下一状态、整除条件及折扣,不照搬不一致记号。原页与实现对照 代码
预测一次更新
- 目标动作0.95、噪声−0.8、噪声限制0.2、动作区间 ,最终目标动作是多少?
答案:噪声先裁为−0.2,动作变为0.75,不再触及动作边界。噪声限制与动作限制分别约束不同量。
- 奖励1、折扣0.9、非终止、两个目标Q分别3和2,备份是多少?
答案:取较小Q=2,得到2.8;两个在线评论家拟合同一个2.8。若真实终止,则目标为1。
- 已做7次评论家更新,延迟为3,本例应做几次演员更新?把计数在每两次评论家更新后重置会怎样?
答案:在第3、6次触发,共2次。如果从1计数且每两次就重置,永远达不到3;如果每段从0计数并在0触发,又会过于频繁。要明确全局更新时钟。
- 两位评论家均预测10,能否说真实Q至少为10?若TD3小任务分数与DDPG相同,应如何报告?
答案:不能,二者可能共享同一高估错误。应报告相同条件下的结果、种子范围和计算差别,不用TD3名气把相同分数写成性能提升。
把动机、算法与成绩分开读
原论文第4—6页把相关误差、延迟和目标平滑放在一起;本篇核读方法部分,未完成所有基准与补充证明审计。原始方法 固定Spinning Up提交提供两次裁剪、Q1演员与实际更新计时;阅读它能发现伪代码和运行循环的约定差异,上游源码未被执行或复制。固定代码
先判断,再看解释
经典TD3的演员更新使用什么?
学习目标
- 区分探索噪声与目标平滑,分别执行噪声和动作裁剪。
- 用持久计数实现延迟更新,并说明双Q最小值为何不是可信下界。
先修与延伸
查看知识关联图 →参考文献
- Addressing Function Approximation Error in Actor-Critic Methods — scoped methods
Scott Fujimoto; Herke van Hoof; David Meger · 2018 - Spinning Up — TD3 fixed-commit methods
OpenAI; Joshua Achiam and contributors · 2020
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-23 · Single AI author. Scoped reading: PMLR80:1587–1596. Read PDF2–6, viewed6: correlated critics, underestimation, delay and smoothing. Eqs14/15 and code resolve Algorithm1 state/divisibility notation and missing gamma in Eq13; no complete benchmark or supplement audit. Docs1–28 and32–140, main file181–325, all62 core lines; two clips, twin Q, Q1 actor and local update clock. Fixed commit; reference only, no upstream execution or copying. 6 paired equations and four explained exercises. Separate noise/action clips, twin target minimum, Q1 actor and persistent delayed clock tested. Independent versus correlated error counterexample and all eight runs retained. No certified lower bound or meaningful return advantage over DDPG demonstrated in this teaching task.
- 核噪声/动作两次裁剪、双目标Q最小值、Q1演员和持久延迟时钟;保留独立/相关误差反例及八种子。本教学任务不提供真实下界或有意义的DDPG对比收益优势。
- 每题四张计算SVG,10数值/几何与24个当前响应式组合通过;八张当前选定图均直接实看,四组使用已检查的初次捕获,资产与稿件SHA绑定,完整页面验收另记。
核对的来源位置:
- Addressing Function Approximation Error in Actor-Critic Methods — scoped methods · PMLR80:1587–1596. Read PDF2–6, viewed6: correlated critics, underestimation, delay and smoothing. Eqs14/15 and code resolve Algorithm1 state/divisibility notation and missing gamma in Eq13; no complete benchmark or supplement audit.
- Spinning Up — TD3 fixed-commit methods · Docs1–28 and32–140, main file181–325, all62 core lines; two clips, twin Q, Q1 actor and local update clock. Fixed commit; reference only, no upstream execution or copying.
相关主题