diff --git a/docs/chapter4/chapter4_questions&keywords.md b/docs/chapter4/chapter4_questions&keywords.md index 2402a6f..b58d456 100644 --- a/docs/chapter4/chapter4_questions&keywords.md +++ b/docs/chapter4/chapter4_questions&keywords.md @@ -68,3 +68,40 @@ - 请详细描述REINFORCE的计算过程。 答:首先我们需要根据一个确定好的policy model来输出每一个可能的action的概率,对于所有的action的概率,我们使用sample方法(或者是随机的方法)去选择一个action与环境进行交互,同时环境就会给我们反馈一整个episode数据。对于此episode数据输入到learn函数中,并根据episode数据进行loss function的构造,通过adam等优化器的优化,再来更新我们的policy model。 + + +## 3 Something About Interview + +- 高冷的面试官:同学来吧,给我手工推导一下策略梯度公式的计算过程。 + + 答:首先我们目的是最大化reward函数,即调整 $\theta$ ,使得期望回报最大,可以用公式表示如下 + $$ + J(\theta)=E_{\tau \sim p_{\theta(\mathcal{T})}}[\sum_tr(s_t,a_t)] + $$ + 对于上面的式子, $\tau$ 表示从从开始到结束的一条完整路径。通常,对于最大化问题,我们可以使用梯度上升算法来找到最大值,即 + $$ + \theta^* = \theta + \alpha\nabla J({\theta}) + $$ + 所以我们仅仅需要计算(更新)$\nabla J({\theta})$ ,也就是计算回报函数 $J({\theta})$ 关于 $\theta$ 的梯度,也就是策略梯度,计算方法如下: + $$ + \nabla_{\theta}J(\theta) = \int {\nabla}_{\theta}p_{\theta}(\tau)r(\tau)d_{\tau} + =\int p_{\theta}{\nabla}_{\theta}logp_{\theta}(\tau)r(\tau)d_{\tau}=E_{\tau \sim p_{\theta}(\tau)}[{\nabla}_{\theta}logp_{\theta}(\tau)r(\tau)] + $$ + 接着我们继续讲上式展开,对于 $p_{\theta}(\tau)$ ,即 $p_{\theta}(\tau|{\theta})$ : + $$ + p_{\theta}(\tau|{\theta}) = p(s_1)\prod_{t=1}^T \pi_{\theta}(a_t|s_t)p(s_{t+1}|s_t,a_t) + $$ + 取对数后为: + $$ + logp_{\theta}(\tau|{\theta}) = logp(s_1)+\sum_{t=1}^T log\pi_{\theta}(a_t|s_t)p(s_{t+1}|s_t,a_t) + $$ + 继续求导: + $$ + \nabla logp_{\theta}(\tau|{\theta}) = \sum_{t=1}^T \nabla_{\theta}log \pi_{\theta}(a_t|s_t) + $$ + 带入第三个式子,可以将其化简为: + $$ + \nabla_{\theta}J(\theta) =E_{\tau \sim p_{\theta}(\tau)}[{\nabla}_{\theta}logp_{\theta}(\tau)r(\tau)] = E_{\tau \sim p_{\theta}}[(\nabla_{\theta}log\pi_{\theta}(a_t|s_t))(\sum_{t=1}^Tr(s_t,a_t))] = \frac{1}{N}\sum_{i=1}^N[(\sum_{t=1}^T\nabla_{\theta}log \pi_{\theta}(a_{i,t}|s_{i,t}))(\sum_{t=1}^Nr(s_{i,t},a_{i,t}))] + $$ + +