DDPG:沿着动作梯度调整控制量
Deep deterministic policy gradient
从有界旋钮出发,推导确定性演员更新,辨清回放分布和目标网络。
ddpg从三个按钮换成一个旋钮
假设一台教学装置的控制量在 −1 到 1 之间。DQN 可以分别估计“左、停、右”三个按钮的价值,却不能把区间里的每个实数都列成一个输出。如果把控制量切成一千档,多维控制时组合数还会继续膨胀。DDPG 的办法是另外训练一个演员:输入状态,直接给出一个动作。评论家回答“这个动作值多少”,演员沿着评论家给出的动作导数调整旋钮。
这个比喻有一个要紧的限制:评论家评的是自己学到的价值。若它在错误的位置画了一座高峰,演员也会朝那里走。DDPG 把连续动作的搜索变成可微优化,并没有把评论家变成真值表。原论文
DPG 是确定性策略梯度的理论路线;DDPG 将它与神经网络、经验回放和目标网络结合。所引 DDPG 工作初稿发布于2015年,论文标注ICLR2016;本文读取的是2019年修订的v6,不能把修订年写成算法诞生年。DPG DDPG版本
动作不随机,访问的状态仍然有分布
记演员为 ,评论家为 。前者参数是 ,后者是 ,不要把动作 与演员参数混为一谈。对固定起点分布、与参数无关的环境、可微且满足积分与求导交换等条件的模型,定义折扣目标和未归一化状态访问量:
有限状态时积分换为和。这里把演员Jacobian写成“参数数 × 动作维度”,乘上动作梯度得到参数梯度。确定性策略消除了对策略动作随机性的积分,没有消除状态分布。原定理使用真实策略价值与相应访问量;一般神经评论家和均匀回放不自动满足这些条件。定理与离策略讨论
实际算法从回放抽小批次 ,保存状态、动作、奖励、下一状态和真实终止标记 。带横线的参数是慢变目标副本。先计算固定目标,再更新在线评论家:
停止梯度意味着这一步只拟合目标,不顺着目标反传。 时没有未来回报;外部采集超时却未真正终止时仍可自举,并须保存重置前的最后观测。如果任务本身只有固定步数,则应把剩余时间纳入状态,在任务终点真正终止。旧接口把“超时”和“终止”压成一个布尔值,不能盲目照搬其处理方式。实现与终止约定
评论家更新后,演员最大化当前评论家在回放状态上的打分:
固定评论家参数后,这确实是该小批次代理目标的梯度。它通常只是原起点回报目标的近似更新方向。演员更新时冻结评论家参数,仍要保留“动作输入 → Q输出”的导数;若把整个Q输出detach,演员也收不到这个学习信号。演员实现
最后更新两组目标参数。这里 乘在线参数,而有些代码的 乘旧目标,两者相加为1:
目标初值应复制在线权重;每次顺序是评论家、演员、目标副本。回放扩大可复用的历史样本范围,目标副本减慢备份变化,但两者都不提供一般的收敛保证。原文使用OU探索噪声和批归一化;它们是该实现的选择,固定版本Spinning Up使用高斯探索噪声。原算法 代码约定
两步控制,真的训练演员与评论家
下面是原创小任务,只有两步,动作范围为 ,初始位置均匀采样于同一区间。第一个动作改变第二步的位置;第二步后真正终止。状态包含阶段 和位置 。演员每阶段有两个参数,评论家每阶段有六个二次特征系数:
第二步动作选 能得到1;第一步也选 ,总回报上界就是 。评论家的动作导数为 ,演员的导数包含 。这两条解析导数都用中心有限差分独立核对。
程序仅依赖Python标准库。训练6000步,前200步均匀探索;随后给演员动作加标准差0.3的高斯噪声并裁剪。容量2000的循环回放均匀有放回抽32条;评论家学习率0.04、演员0.02、目标系数0.01。评估遍历101个固定初始位置,关闭探索噪声。每个训练步最多一次批更新,因此默认5800次评论家和演员更新。
"""Original two-step control lesson; quadratic critics, not deep RL benchmark."""
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 train(seed=0, steps=6000):
explore, replay_rng = random.Random(seed), random.Random(seed+10000)
init = random.Random(seed+20000)
actor = [[0.0, 0.0] for _ in range(2)]
critic = [[init.uniform(-0.05, 0.05) for _ in range(6)] for _ in range(2)]
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
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)]
cg = [[0.0]*6 for _ in range(2)]
for h, sx, act, reward, next_x, done in batch:
future = 0.0 if done else value(target_critic[1], next_x, policy(target_actor[1], next_x))
y = bootstrap(reward, future, done)
error = value(critic[h], sx, act) - y
for j, f in enumerate(features(sx, act)):
cg[h][j] += 2*error*f/len(batch)
for h in (0, 1):
critic[h] = [w-0.04*g for w, g in zip(critic[h], cg[h])]
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])]
target_critic[h] = [(1-tau)*v+tau*w for v, w in zip(target_critic[h], critic[h])]
updates += 1
return {'seed':seed, 'initial':initial, 'final':evaluate(actor), 'optimum':1.9,
'critic_updates':updates, 'actor_updates':updates,
'actor':actor, 'critic':critic, 'target_actor':target_actor}
if __name__ == '__main__':
print(json.dumps(train(), sort_keys=True))
查看完整运行结果
{"actor": [[0.5236871225973071, -0.0004731742740617796], [0.5082939848174745, -0.000855987285543454]], "actor_updates": 5800, "critic": [[1.8944227220576786, 0.00012196580991579371, -0.0001989443020707362, -0.24013855916949278, 0.971614469550044, -0.9762872866311275], [0.9965772810361748, 0.000509452200296677, -0.0014117889767140943, -0.2327207238172958, 0.9523348881341847, -0.9684061089724543]], "critic_updates": 5800, "final": 1.8999391982975766, "initial": 1.795875, "optimum": 1.9, "seed": 0, "target_actor": [[0.5234214940786861, -0.000345302207106862], [0.5082226444811255, -0.0007721067317969688]]}这段代码训练的是低维参数模型,便于检查每个乘法,并非深神经网络DDPG或MuJoCo复现。每次更新固定特征维数时耗时随批量线性增长,回放空间随容量线性增长;深网络版本还取决于网络参数量与前后向计算量。
种子0—7全部运行并保留。无训练时固定评估回报为1.795875,训练后平均1.899931,范围约1.899904—1.899941;解析上界1.9仅属于这个任务。接近上界说明这组计算在这个任务上学到了有效动作,不证明真实机器人也稳定。
评论家的低误差,可以藏着坏导数
演员依赖导数,而通常训练评论家用的是数值误差。取真值为0,构造一个原创反例:
值误差处处很小,动作梯度仍可能很错。DPG兼容逼近讨论对动作梯度和特征结构另有条件,不能只用低均方误差替代。兼容条件
还要区分“过去动作可以用于训练评论家”和“状态权重已校正”。本批另一个两状态探针的起点目标梯度为(0.63, 0.36),行为访问量加权的局部演员方向为(0.63, 0.18);差别来自状态分布。离策略采样不是自动消除分布偏差的按钮。
高维动作、探索不足、评论家外推和tanh饱和都可能让学习失败。TD3会处理部分估计与更新不稳定问题,SAC改用带熵目标的随机演员;选择哪种仍要在自己的数据、环境、预算和多种子评估上判断。
把更新拆开检查
- 评论家冻结参数后,演员还需要对什么求导?
答案:需要对动作输入求导,再乘演员对参数的Jacobian。冻结评论家权重节省不需要的权重梯度;把Q输出整体停止梯度会同时切断演员路径。
- 奖励为1,折扣0.9,目标评论家输出2,非终止时备份是多少?若这一步真正终止呢?
答案:非终止为2.8;真正终止为1。纯采集超时仍属前一种,真实任务终点属后一种;若两个标记同时为真,以真实终止为准。
- 目标权重为0,在线权重为10,论文约定的 更新后是多少?代码若用 应填多少?
答案:新目标为0.1,。把0.01误当乘旧目标的系数,会得到9.9,目标副本几乎立即追上在线值。
- 评论家验证集的值误差下降,能否据此保证演员动作更好?
答案:不能。正文的振荡反例给出小值误差、大导数误差;还应检查动作附近的价值形状、实际策略回报、数据覆盖和多种子结果。验证值误差是证据之一,不是动作质量保证。
接着读哪里
先对照DPG的真实策略梯度与离策略近似,保留访问量和兼容条件。DPG推导 再读DDPG的目标副本与Algorithm1,注意初稿、会议和修订版本的区别。DDPG方法 固定Spinning Up实现帮助定位停止梯度、演员反传、探索与Polyak系数;本篇未执行上游代码,也未复现原论文基准。实现阅读
先判断,再看解释
训练演员时,应如何处理评论家?
学习目标
- 写出演员、评论家与目标副本各自的更新,核对动作导数。
- 解释为什么均匀回放不自动给出起点目标的精确梯度。
先修与延伸
查看知识关联图 →参考文献
- Deterministic Policy Gradient Algorithms — scoped derivation
David Silver; Guy Lever; Nicolas Heess; Thomas Degris; Daan Wierstra; Martin Riedmiller · 2014 - Continuous control with deep reinforcement learning — arXiv v6 methods
Timothy P. Lillicrap; Jonathan J. Hunt; Alexander Pritzel; Nicolas Heess; Tom Erez; Yuval Tassa; David Silver; Daan Wierstra · 2019 - Spinning Up — DDPG fixed-commit methods
OpenAI; Joshua Achiam and contributors · 2020
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-23 · Single AI author. Scoped reading: Read PDF2–5, viewed4/5; supplement web lines0–266. Unnormalized occupancy, off-policy approximation and compatible action-gradient conditions; no full experiment or appendix audit. First2015-09-09, ICLR2016; selected v6 dated2019-07-05. Read PDF2–5, viewed5: replay, deterministic chain-rule updates, targets and exploration. No benchmark replication or later-table audit. Docs1–258, main PyTorch file162–284, all61 core lines; rho convention, detached targets, actor gradients through Q and old Gym timeout handling. Fixed commit; reference only, no upstream execution or copying. 6 paired equations and four explained exercises. Deterministic chain-rule/action derivatives checked by finite differences; replay surrogate distinguished from start-state gradient. Critic/actor/target order, true termination versus collection limits and target interpolation tested. Eight actual low-dimensional two-step runs retained, not deep-network or robot reproduction.
- 核确定性链式动作梯度与差分、回放代理和起点目标区别、评论家/演员/目标顺序、真终止与截断及软更新;保留八个低维两步训练种子,不冒充深网络/机器人复现。
- 每题四张计算SVG,10数值/几何与24个当前响应式组合通过;八张当前选定图均直接实看,四组使用已检查的初次捕获,资产与稿件SHA绑定,完整页面验收另记。
核对的来源位置:
- Deterministic Policy Gradient Algorithms — scoped derivation · Read PDF2–5, viewed4/5; supplement web lines0–266. Unnormalized occupancy, off-policy approximation and compatible action-gradient conditions; no full experiment or appendix audit.
- Continuous control with deep reinforcement learning — arXiv v6 methods · First2015-09-09, ICLR2016; selected v6 dated2019-07-05. Read PDF2–5, viewed5: replay, deterministic chain-rule updates, targets and exploration. No benchmark replication or later-table audit.
- Spinning Up — DDPG fixed-commit methods · Docs1–258, main PyTorch file162–284, all61 core lines; rho convention, detached targets, actor gradients through Q and old Gym timeout handling. Fixed commit; reference only, no upstream execution or copying.
相关主题