PPO、GAE笔记

一、 重要性采样

TRPO和PPO主要思想的数学基础是重要性采样

  • 重要性采样: xix_ixi 是从p(x)p(x)p(x)分布中采样得到的, 但是p(x)p(x)p(x)的值往往无法直接获得,需要通过其他分布q(x)q(x)q(x)进行间接采样获得。

Ex∼p[f(x)]=∫f(x)p(x)dx=∫f(x)p(x)q(x)q(x)dx=Ex∼q[f(x)p(x)q(x)] \begin{aligned} \mathbb{E}_{x\sim p}[f(x)] &=\int f(x)p(x) dx \\ &=\int f(x) \frac{p(x)}{q(x)}q(x)dx \\ &=\mathbb{E}_{x\sim q}[f(x)\frac{p(x)}{q(x)}] \end{aligned} Exp[f(x)]=f(x)p(x)dx=f(x)q(x)p(x)q(x)dx=Exq[f(x)q(x)p(x)]

  • 条件

    • ppp分布与qqq分布需要相近,才能得到较好的效果。
  • 用在强化学习里面:

    • 由于策略梯度原始公式中的 新策略分布难以得到,因而使用旧策略进行间接采样,以使得未知项变成可估计的已知项进行计算。


二、 梯度与参数更新

1. 回报的期望: 最大化全部采样轨迹上的策略回报值
Rθˉ=∑τR(τ)pθ(τ) \bar{R_\theta}=\sum_\tau R(\tau)p_\theta(\tau) Rθˉ=τR(τ)pθ(τ)
2. 回报的期望的梯度:(第三个等号用到的公式:∇f(x)=f(x)∇log⁡f(x)\nabla f(x) = f(x) \nabla \log f(x)f(x)=f(x)logf(x))

∇Rθˉ=∑τR(τ)∇pθ(τ)=∑τR(τ)pθ(τ)∇pθ(τ)pθ(τ)=∑τR(τ)pθ(τ)∇log⁡pθ(τ)=Eτ∼pθτ[R(τ)∇log⁡pθ(τ)]≈1N∑n=1NR(τn)∇log⁡pθ(τn)=1N∑n=1N∑t=1TnR(τn)∇log⁡pθ(atn∣stn) \begin {aligned} \nabla \bar{R_\theta}&=\sum_\tau R(\tau) \nabla p_{\theta}(\tau) \\ &= \sum_\tau R(\tau)p_\theta(\tau)\frac{\nabla p_\theta(\tau)}{p_\theta(\tau)} \\ &= \sum_\tau R(\tau)p_\theta(\tau){\nabla \log p_\theta(\tau)}\\ &= \mathbb{E}_{\tau \sim p_\theta{\tau}}[R(\tau){\nabla \log p_\theta(\tau)}] \\ &≈ \frac{1}{N} \sum_{n=1}^{N}R(\tau^n)\nabla \log p_{\theta}(\tau^n) \\ &=\frac{1}{N}\sum_{n=1}^{N} \sum_{t=1}^{T_n} R(\tau^n)\nabla \log p_\theta(a_t^n|s_t^n) \end {aligned} Rθˉ=τR(τ)pθ(τ)=τR(τ)pθ(τ)pθ(τ)pθ(τ)=τR(τ)pθ(τ)logpθ(τ)=Eτpθτ[R(τ)logpθ(τ)]N1n=1NR(τn)logpθ(τn)=N1n=1Nt=1TnR(τn)logpθ(atnstn)

式中

  • NNN表示采样了NNN条trajectory, TnT_nTn表示每条trajectory的step数量。

  • 关于pθ(τ)p_{\theta}(\tau)pθ(τ)
    pθ(τ)=p(s1)pθ(a1∣s1)p(s2∣s1,a1)pθ(a2∣s2)p(s3∣s2,a2)  ...  =p(s1)∏t=1Tpθ(at∣st)p(st+1∣st,at) \begin{aligned} p_{\theta}(\tau) &= p(s_1)p_\theta(a_1|s_1)p(s_2|s_1,a_1)p_\theta(a_2|s_2)p(s_3|s_2,a_2) \space\space...\space\space \\ &=p(s_1) \prod_{t=1}^T p_{\theta}(a_t|s_t)p(s_{t+1}|s_t, a_t) \end{aligned} pθ(τ)=p(s1)pθ(a1s1)p(s2s1,a1)pθ(a2s2)p(s3s2,a2)  ...  =p(s1)t=1Tpθ(atst)p(st+1st,at)
    由两部分组成一部分是来自环境的 pθ(st+1∣st,a)p_\theta(s_{t+1}|s_t, a)pθ(st+1st,a), 一部分是来自agent的 pθ(at∣st)p_\theta {(a_t|s_t)}pθ(atst), 其中来自环境的部分不带入计算,策略更新只考虑agent这部分。所以最后一步并没有t+1t+1t+1这部分。

3. 参数更新:
θ=θ+η∇Rθˉ \theta = \theta+\eta \nabla \bar{R_\theta} θ=θ+ηRθˉ


三、 实际算法中对策略梯度的处理方法

1. 策略梯度方法:

加入baseline
∇Rθˉ=1N∑n=1N(R(τn)−b)∇log⁡pθ(τn)b≈E[R(τ)] \nabla \bar{R_\theta}=\frac{1}{N} \sum_{n=1}^{N}(R(\tau^n)-b)\nabla \log p_{\theta}(\tau^n) \\ b≈\mathbb{E}[R(\tau)] Rθˉ=N1n=1N(R(τn)b)logpθ(τn)bE[R(τ)]

bbb 的加入保证reward不是恒大于0的,若reward一直大于0,则会导致未被采样的action无法得到提升,但其实该action并不是不好而是未被采样。

2. 状态值函数估计轨迹回报:

R(τn)−bR(\tau^n)-bR(τn)b 部分使用状态值函数来替代
q(s,a) q(s,a) q(s,a)

3. 优势函数估计轨迹回报(Actor-Critic):

R(τn)−bR(\tau^n)-bR(τn)b 部分用以下Advantage function来替代

A(st,at)=q(s,a)−V(s) A(s_t,a_t)= q(s,a)-V(s) A(st,at)=q(s,a)V(s)

4. TD-Error估计轨迹回报:

R(τn)−bR(\tau^n)-bR(τn)b 部分用以下TD-Error 代替
r(st.at)+v(st+1)−v(s)​ r(s_t. a_t)+v(s_{t+1})-v(s)​ r(st.at)+v(st+1)v(s)


四、GAE(Generalized Advantage Estimation)

  1. GAE的作用

    • GAE的意思是泛化优势估计,因而他是用来优化Advantage Function优势函数的。
    • GAE的存在是用来权衡variance和bias问题的:
      • On-policy直接交互并用每一时刻的回报作为长期回报的估计∑t′=tTγt′−trt′\sum_{t'=t}^{T} \gamma^{t'-t}r_{t'}t=tTγttrt 会产生较大的方差,Variance较大。
      • 而通过基于优势函数的AC方法来进行回报值估计,则会产生方差较小,而Bias较大的问题。
  2. GAE 推导

    满足γ\gammaγ-just条件。(未完待续)

  3. GAE形式

    GAE的形式为多个价值估计的加权平均数。
    TDError=δt=rt+γv(st+1)−v(st) TD Error=\delta_t=r_t+\gamma v(s_{t+1})-v(s_t) TDError=δt=rt+γv(st+1)v(st)
    运用GAE公式进行优势函数的估计:

∑l=0∞(γλ)lδt+1V \sum_{l=0}^\infin(\gamma\lambda)^l \delta_{t+1}^V l=0(γλ)lδt+1V

​ 为了快速估计序列中所有时刻的估计值,采用倒序计算,从t+1时刻估计t时刻:
At^GAE(γ,λ)=∑l=0∞(γλ)lδt+1V=δtV+γλA^t+1GAE(γ,λ) \hat{A_t}^{GAE(\gamma,\lambda)}=\sum_{l=0}^{\infin}(\gamma\lambda)^l \delta_{t+1}^V=\delta_t^V+\gamma\lambda\hat{A}_{t+1}^{GAE(\gamma,\lambda)} At^GAE(γ,λ)=l=0(γλ)lδt+1V=δtV+γλA^t+1GAE(γ,λ)


五、PPO关于策略梯度的目标函数

以上所述的策略梯度算法属于on-policy的算法,而PPO属于近似on-policy的算法

  • on-policy: 使用当前策略πθ\pi_\thetaπθ收集数据,当参数θ\thetaθ更新后,必须重新采样。
    ∇Rθˉ=Eτ∼pθτ[R(τ)∇log⁡pθ(τ)] \nabla \bar{R_\theta}=\mathbb{E}_{\tau \sim p_\theta{\tau}}[R(\tau){\nabla \log p_\theta(\tau)}] Rθˉ=Eτpθτ[R(τ)logpθ(τ)]

  • off-policy: 可以从可重用的样本数据中获取样本来训练当前的策略πθ\pi _\thetaπθ,下式用了重要性采样。
    ∇Rθˉ=Eτ∼pθ′τ[pθ(τ)pθ′(τ)R(τ)∇log⁡pθ(τ)] \nabla \bar{R_\theta}=\mathbb{E}_{\tau \sim p_{\theta^\prime}{\tau}}[\frac{p_\theta(\tau)}{p_{\theta^\prime}(\tau)} R(\tau){\nabla \log p_\theta(\tau)}] Rθˉ=Eτpθτ[pθ(τ)pθ(τ)R(τ)logpθ(τ)]

1. PPO目标函数

对于PPO而言,轨迹回报通过采用Advantage function的方式进行估计,因而其梯度更新方式为:
∇Rθˉ=E(st,at)∼πθ[Aθ(st,at)∇log⁡pθ(atn∣stn)]=E(st,at)∼πθ′[pθ(st,at)pθ′(st,at)Aθ′(st,at)∇log⁡pθ(atn∣stn)]=E(st,at)∼πθ′[pθ(at∣st)pθ′(at∣st)pθ(st)pθ′(st)Aθ′(st,at)∇log⁡pθ(atn∣stn)]≈E(st,at)∼πθ′[∇pθ(at∣st)pθ′(at∣st)Aθ′(st,at)] \begin{aligned} \nabla \bar{R_\theta} &=\mathbb{E}_{(s_t,a_t)\sim\pi_\theta}[A^\theta(s_t,a_t)\nabla \log p_\theta({a_t^n|s_t^n})] \\ &=\mathbb{E}_{(s_t,a_t)\sim\pi_\theta^\prime}[\frac{p_\theta(s_t,a_t)}{p_\theta^\prime(s_t,a_t)}A^{\theta^\prime}(s_t,a_t)\nabla \log p_\theta({a_t^n|s_t^n})] \\ &=\mathbb{E}_{(s_t,a_t)\sim\pi_\theta^\prime}[\frac{p_\theta(a_t|s_t)}{p_\theta^\prime(a_t|s_t)}\frac{p_\theta(s_t)}{p_\theta^\prime(s_t)}A^{\theta^\prime}(s_t,a_t)\nabla \log p_\theta({a_t^n|s_t^n})] \\ &≈\mathbb{E}_{(s_t,a_t) \sim \pi_\theta^\prime}[\frac{\nabla p_\theta(a_t|s_t)}{p_\theta^\prime(a_t|s_t)}A^{\theta^\prime}(s_t,a_t)] \end{aligned} Rθˉ=E(st,at)πθ[Aθ(st,at)logpθ(atnstn)]=E(st,at)πθ[pθ(st,at)pθ(st,at)Aθ(st,at)logpθ(atnstn)]=E(st,at)πθ[pθ(atst)pθ(atst)pθ(st)pθ(st)Aθ(st,at)logpθ(atnstn)]E(st,at)πθ[pθ(atst)pθ(atst)Aθ(st,at)]
​ 其中,从第二个等式用的是重要性采样,第三到第四个约等式由于pθ(st)pθ′(st)\frac{p_\theta(s_t)}{p_\theta^\prime(s_t)}pθ(st)pθ(st)这一项来源于重要性采样,前提假设两个分布差别不大,近似为1,且不易计算,故省略,后面的∇log⁡pθ(atn∣stn)\nabla \log p_\theta({a_t^n|s_t^n})logpθ(atnstn) ,根据公式∇f(x)=f(x)∇log⁡f(x)\nabla f(x) = f(x) \nabla \log f(x)f(x)=f(x)logf(x)转换。

​ 因而,定义目标函数为:
Jθ′(θ)=E(st,at)∼πθ′[pθ(at∣st)pθ′(at∣st)Aθ′(st,at)] J^{\theta^{\prime}} (\theta)=\mathbb{E}_{(s_t,a_t) \sim \pi_\theta^\prime}[\frac{p_\theta(a_t|s_t)}{p_\theta^\prime(a_t|s_t)}A^{\theta^\prime}(s_t,a_t)] Jθ(θ)=E(st,at)πθ[pθ(atst)pθ(atst)Aθ(st,at)]

2. PPO对于重要性采样约束的处理

​ 为了保证$p_\theta(s_t,a_t) $ 与 pθ′(st,at)p_\theta^\prime(s_t,a_t)pθ(st,at) 分布的差别不会太大,采用以下约束:

  • TRPO: 使用约束 KL(θ,θ′)<δKL(\theta,\theta')<\deltaKL(θ,θ)<δ,在分布上进行约束。
  • PPO1(Adaptive KL):使用JPPOθ′(θ)=Jθ′(θ)−βKL(θ,θ′)J_{PPO}^{\theta'}(\theta)=J^{\theta'}(\theta)-\beta KL(\theta,\theta')JPPOθ(θ)=Jθ(θ)βKL(θ,θ),在目标函数上加一个正则项进行约束,注意,这里KL散度衡量的是action之间的距离,而不是参数θ\thetaθθ′\theta'θ之间的距离。
  • PPO2 (Clip,论文中推荐的):使用JPPO2θ′(θ)=∑(st,at)min⁡{([pθ(at∣st)pθ′(at∣st)Aθ′(st,at)],[clip(pθ(at∣st)pθ′(at∣st),1−ϵ,1+ϵ)Aθ′(st,at)])}J_{PPO_2}^{\theta'}(\theta)=\sum_{(s_t,a_t)}\min\{([\frac{p_\theta(a_t|s_t)}{p_\theta^\prime(a_t|s_t)}A^{\theta^\prime}(s_t,a_t)], [clip(\frac{p_\theta(a_t|s_t)}{p_\theta^\prime(a_t|s_t)},1-\epsilon,1+\epsilon)A^{\theta^\prime}(s_t,a_t)])\}JPPO2θ(θ)=(st,at)min{([pθ(atst)pθ(atst)Aθ(st,at)],[clip(pθ(atst)pθ(atst),1ϵ,1+ϵ)Aθ(st,at)])}, 来约束分布距离。

3. 使用GAE对优势函数进行优化

def get_gaes(self, rewards, v_preds, v_preds_next):
    """
    GAE
    :param rewards: r(t)
    :param v_preds: v(st)
    :param v_preds_next: v(st+1)
    :return:
    """
    deltas = [r_t + self.gamma * v_next - v for r_t, v_next, v in zip(rewards, v_preds_next, v_preds)]

    #计算GAE(lambda = 1), 参见 ppo paper eq(11)
    gaes = copy.deepcopy(deltas)
    
    # 倒序计算GAE
    for t in reversed(range(len(gaes) - 1)):
        gaes[t] = gaes[t] + self.gamma * gaes[t + 1]
    return gaes

六、 PPO的目标函数

PPO的最终目标函数由三部分组成,可使用梯度下降求解,而不是像TRPO一样使用共轭梯度法:

  • 策略梯度目标函数: LtCLIP(θ)L_t^{CLIP}(\theta)LtCLIP(θ)
  • 值函数目标函数:LtVF(θ)=(Vθ(st)−Vttarget)2L_t^{VF}(\theta)=(V_\theta(s_t)-V_t^{target})^2LtVF(θ)=(Vθ(st)Vttarget)2
  • 策略模型的熵: S[πθ](st)S_[\pi_\theta](s_t)S[πθ](st)

完整的形式如下:
LtPPO2(θ)=E^t[LtCLIP(θ)−c1LtVF(θ)+c2S[πθ](st)] L_t^{PPO_2}(\theta)=\hat{\mathbb{E}}_t[L_t^{CLIP}(\theta)-c_1L_t^{VF}(\theta)+c_2S_[\pi_\theta](s_t)] LtPPO2(θ)=E^t[LtCLIP(θ)c1LtVF(θ)+c2S[πθ](st)]
这部分相应的代码如下:

with tf.variable_scope('assign_op'):
    self.assign_ops = []
    for v_old, v in zip(old_pi_trainable, pi_trainable):
        self.assign_ops.append(tf.assign(v_old, v))

# inputs for train_op
with tf.variable_scope('train_inp'):
    self.actions = tf.placeholder(dtype=tf.int32, shape=[None], name='actions')
    self.rewards = tf.placeholder(dtype=tf.float32, shape=[None], name='rewards')
    self.v_preds_next = tf.placeholder(dtype=tf.float32, shape=[None], name='v_preds_next')
    self.gaes = tf.placeholder(dtype=tf.float32, shape=[None], name='gaes')

act_probs = self.Policy.act_probs
act_probs_old = self.Old_Policy.act_probs

# agent通过新策略选择action的概率 probabilities of actions which agent took with policy
act_probs = act_probs * tf.one_hot(indices=self.actions, depth=act_probs.shape[1])
act_probs = tf.reduce_sum(act_probs, axis=1)

# agent通过旧策略选择action的概率 probabilities of actions which agent took with old policy
act_probs_old = act_probs_old * tf.one_hot(indices=self.actions, depth=act_probs_old.shape[1])
act_probs_old = tf.reduce_sum(act_probs_old, axis=1)

with tf.variable_scope('PPO_loss'):
    """
        策略目标函数
    """
    #
    # ratios = tf.divide(act_probs, act_probs_old)
    # r_t(θ) = π/πold 为了防止除数为0,这里截取一下值,然后使用e(log减法)来代替直接除法
    ratios = tf.exp(
        tf.log(tf.clip_by_value(act_probs, 1e-10, 1.0)) - tf.log(tf.clip_by_value(act_probs_old, 1e-10, 1.0)))
    # L_CLIP 裁剪优势函数值
    clipped_ratios = tf.clip_by_value(ratios, clip_value_min=1 - clip_value, clip_value_max=1 + clip_value)
    self.loss_clip = tf.minimum(tf.multiply(self.gaes, ratios), tf.multiply(self.gaes, clipped_ratios))
    self.loss_clip = tf.reduce_mean(self.loss_clip)

    """
        策略模型的熵
    """
    # 计算新策略πθ的熵 S = -p log(p) 这里裁剪防止p=0
    self.entropy = -tf.reduce_sum(
        self.Policy.act_probs * tf.log(tf.clip_by_value(self.Policy.act_probs, 1e-10, 1.0)), axis=1)
    self.entropy = tf.reduce_mean(self.entropy, axis=0)  # mean of entropy of pi(obs)

    """
        值目标函数
    """
    # L_vf = [(r+γV(π(st+1))) - (V(π(st)))]^2
    v_preds = self.Policy.v_preds
    self.loss_vf = tf.squared_difference(self.rewards + self.gamma * self.v_preds_next, v_preds)
    self.loss_vf = tf.reduce_mean(self.loss_vf)

    # construct computation graph for loss
    # L(θ) = E_hat[L_CLIP(θ) - c1 L_VF(θ) + c2 S[πθ](s)]
    # L = 策略目标函数 + 值目标函数 + 策略模型的熵
    self.loss = self.loss_clip - c_1 * self.loss_vf + c_2 * self.entropy
    # minimize -loss == maximize loss
    self.loss = -self.loss

optimizer = tf.train.RMSPropOptimizer(learning_rate=args.ppo_lr, epsilon=1e-5)
self.gradients = optimizer.compute_gradients(self.loss, var_list=pi_trainable)
self.train_op = optimizer.minimize(self.loss, var_list=pi_trainable)
03-27
<think>好的,用户想了解PPOGAE的实现和原理。首先,我需要回顾一下PPOGAE的基本概念。PPO(Proximal Policy Optimization)是一种强化学习算法,属于策略梯度方法,主要用于优化策略以最大化累积奖励。GAE(Generalized Advantage Estimation)则是一种优势估计方法,帮助更有效地估计策略梯度中的优势函数,减少方差,同时保持偏差的可控。 用户提到的引用中有几个关键点需要注意。引用[2]提到PPO基于AC(Actor-Critic)框架,采用重要性采样实现离线策略,并且解决了传统AC方法中数据只能使用一次的问题。引用[3]则提到了使用GAE来计算优势函数,需要等到一个episode结束后才能得到回报Gt,这可能涉及到蒙特卡洛方法或TD误差的结合。而GAE正是结合了这两种方法的优点,通过指数加权平均多个TD误差来估计优势函数,从而平衡偏差和方差。 接下来,我需要将PPOGAE结合起来解释它们的实现原理。首先,PPO的核心在于限制策略更新的幅度,通过裁剪概率比来避免过大的策略变化,确保训练的稳定性。而GAE的作用是提供一个更准确的优势估计,帮助Actor网络更好地更新策略,Critic网络更好地估计状态值函数。 在实现步骤方面,可能需要分几个部分:数据收集、优势计算(使用GAE)、策略更新(使用PPO的裁剪目标函数)、值函数更新。同时,代码示例部分需要展示如何实现这些步骤,但由于引用[4]提到无法提供具体代码,可能需要给出伪代码或关键代码片段的结构,比如如何计算GAE,如何构建损失函数等。 还需要注意用户可能存在的疑问点,比如为什么PPO要使用重要性采样GAE的具体计算公式是怎样的,以及两者如何结合使用。此外,用户可能对PPO中的clip机制和GAE中的λ参数调整感兴趣,这些都需要在解释中涵盖。 最后,生成的相关问题需要围绕PPOGAE的扩展应用、参数调整、与其他算法的比较等,帮助用户深入理解。例如,如何调整GAE中的λ参数,PPO与其他策略梯度方法的区别等。</think>### PPOGAE的实现原理 #### 1. **PPO的核心机制** PPO通过**裁剪概率比**限制策略更新幅度,目标函数为: $$L^{CLIP}(\theta) = \mathbb{E}_t \left[ \min(r_t(\theta)A_t, \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon)A_t) \right]$$ 其中$r_t(\theta)=\frac{\pi_\theta(a|s)}{\pi_{\theta_{old}}(a|s)}$是策略更新前后的概率比,$A_t$为优势函数,$\epsilon$是裁剪阈值(通常取0.1-0.3)[^1]。 #### 2. **GAE的优势估计** GAE结合了TD误差的**多步估计**,公式为: $$A_t^{GAE} = \sum_{l=0}^{T-t} (\gamma \lambda)^l \delta_{t+l}$$ 其中$\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t)$,$\lambda$控制偏差-方差权衡($\lambda=0$为低方差高偏差,$\lambda=1$为蒙特卡洛估计)[^3]。 --- ### 关键实现步骤 1. **数据收集** - 通过当前策略$\pi_\theta$与环境交互,存储轨迹$(s_t,a_t,r_t,s_{t+1})$ - 使用Critic网络计算状态价值$V(s_t)$ 2. **GAE计算** ```python # 伪代码示例 advantages = [] gae = 0 for t in reversed(range(buffer_size)): delta = rewards[t] + gamma * values[t+1] * (1 - dones[t]) - values[t] gae = delta + gamma * lambda * gae * (1 - dones[t]) advantages.insert(0, gae) ``` 3. **策略优化** - 计算概率比$r_t(\theta)$ - 构建裁剪目标函数,同时优化策略损失和值函数损失: $$L_{total} = L^{CLIP} - c_1 L^{VF} + c_2 S[\pi_\theta](s_t)$$ 其中$L^{VF}$是值函数均方误差,$S$是策略熵正则项[^2] --- ### 实现要点说明 | 组件 | 功能描述 | 超参数示例 | |-------------|----------------------------------|--------------------| | Actor网络 | 输出动作分布参数 | 学习率: 3e-4 | | Critic网络 | 估计状态价值函数$V(s)$ | GAE $\lambda$: 0.95| | 经验回放池 | 存储轨迹数据用于批量更新 | 批量大小: 64-4096 | | 优化器 | 通常使用Adam优化器 | 熵系数$c_2$: 0.01 | --- ### 典型代码结构 ```python class PPO: def __init__(self): self.actor = ActorNetwork() self.critic = CriticNetwork() def update(self, states, actions, advantages): # 计算新旧策略概率比 old_probs = self.old_actor(states).gather(1, actions) new_probs = self.actor(states).gather(1, actions) ratio = new_probs / old_probs # 裁剪目标函数 surr1 = ratio * advantages surr2 = torch.clamp(ratio, 1-eps, 1+eps) * advantages actor_loss = -torch.min(surr1, surr2).mean() # 值函数损失 values = self.critic(states) critic_loss = F.mse_loss(values, targets) # 总损失 total_loss = actor_loss + 0.5*critic_loss return total_loss ``` ---
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值