当前位置:网站首页>强化学习——策略梯度理解点
强化学习——策略梯度理解点
2022-07-23 07:53:00 【小陈phd】
策略梯度计算公式
目的是最大化reward函数,即调整 θ ,使得期望回报最大,可以用公式表示如下
J ( θ ) = E τ ∼ p ( T ) [ ∑ t r ( s t , a t ) ] \mathrm{J}(\theta)=\mathrm{E}_{\tau \sim p}(\mathcal{T})\left[\sum_{\mathrm{t}} \mathrm{r}\left(\mathrm{s}_{t}, \mathrm{a}_{\mathrm{t}}\right)\right] J(θ)=Eτ∼p(T)[t∑r(st,at)]
对于上面的式子, τ \tau τ 表示从从开始到结束的一条完整路径。通常,对于最大化问题,我们可以使用梯度上升算法来找到最大值,即
θ ∗ = θ + α ∇ J ( θ ) \theta^{*}=\theta+\alpha \nabla \mathrm{J}(\theta) θ∗=θ+α∇J(θ)
所以我们仅仅需要计算 (更新) ∇ J ( θ ) \nabla J(\theta) ∇J(θ) ,也就是计算回报函数 J ( θ ) J(\theta) J(θ) 关于 θ \theta θ 的梯度,也就是策略梯度,计算方法如下:
∇ θ J ( θ ) = ∫ ∇ θ p θ ( τ ) r ( τ ) d τ = ∫ p θ ∇ θ log p θ ( τ ) r ( τ ) d τ = E τ ∼ p θ ( τ ) [ ∇ θ log p θ ( τ ) r ( τ ) ] \begin{aligned} \nabla_{\theta} \mathrm{J}(\theta) &=\int \nabla_{\theta \mathrm{p}_{\theta}}(\tau) \mathrm{r}(\tau) \mathrm{d}_{\tau} \\ &=\int \mathrm{p}_{\theta} \nabla_{\theta} \log \mathrm{p}_{\theta}(\tau) \mathrm{r}(\tau) \mathrm{d}_{\tau} \\ &=\mathrm{E}_{\tau \sim \mathrm{p} \theta(\tau)}\left[\nabla_{\theta} \log \mathrm{p}_{\theta}(\tau) \mathrm{r}(\tau)\right] \end{aligned} ∇θJ(θ)=∫∇θpθ(τ)r(τ)dτ=∫pθ∇θlogpθ(τ)r(τ)dτ=Eτ∼pθ(τ)[∇θlogpθ(τ)r(τ)]
接着我们继续讲上式展开,对于 p θ ( τ ) \mathrm{p}_{\theta}(\tau) pθ(τ) ,即 p θ ( τ ∣ θ ) \mathrm{p}_{\theta}(\tau \mid \theta) pθ(τ∣θ) :
p θ ( τ ∣ θ ) = p ( s 1 ) ∏ t = 1 T π θ ( a t ∣ s t ) p ( s t + 1 ∣ s t , a t ) \mathrm{p}_{\theta}(\tau \mid \theta)=\mathrm{p}\left(\mathrm{s}_{1}\right) \prod_{t=1}^{\mathrm{T}} \pi_{\theta}\left(\mathrm{a}_{t} \mid \mathrm{s}_{\mathrm{t}}\right) \mathrm{p}\left(\mathrm{s}_{t+1} \mid \mathrm{s}_{t}, \mathrm{a}_{\mathrm{t}}\right) pθ(τ∣θ)=p(s1)t=1∏Tπθ(at∣st)p(st+1∣st,at)
取对数后为:
log p θ ( τ ∣ θ ) = log p ( s 1 ) + ∑ t = 1 T log π θ ( a t ∣ s t ) p ( s t + 1 ∣ s t , a t ) \log p_{\theta}(\tau \mid \theta)=\log p\left(s_{1}\right)+\sum_{t=1}^{T} \log \pi_{\theta}\left(a_{t} \mid s_{t}\right) p\left(s_{t+1} \mid s_{t}, a_{t}\right) logpθ(τ∣θ)=logp(s1)+t=1∑Tlogπθ(at∣st)p(st+1∣st,at)
继续求导:
∇ log p θ ( τ ∣ θ ) = ∑ t = 1 T ∇ θ log π θ ( a t ∣ s t ) \nabla \log p_{\theta}(\tau \mid \theta)=\sum_{t=1}^{T} \nabla_{\theta} \log \pi_{\theta}\left(a_{t} \mid s_{t}\right) ∇logpθ(τ∣θ)=t=1∑T∇θlogπθ(at∣st)
带入第三个式子,可以将其化简为:
∇ θ J ( θ ) = E τ ∼ p θ ( τ ) [ ∇ θ log p θ ( τ ) r ( τ ) ] = E τ ∼ p θ [ ( ∇ θ log π θ ( a t ∣ s t ) ) ( ∑ t = 1 T r ( s t , a t ) ) ] = 1 N ∑ i = 1 N [ ( ∑ t = 1 T ∇ θ log π θ ( a i , t ∣ s i , t ) ) ( ∑ t = 1 N r ( s i , t , a i , t ) ) ] \begin{aligned} \nabla_{\theta} \mathrm{J}(\theta) &=\mathrm{E}_{\tau \sim p \theta(\tau)}\left[\nabla_{\theta} \log \mathrm{p}_{\theta}(\tau) \mathrm{r}(\tau)\right] \\ &=\mathrm{E}_{\tau \sim p \theta}\left[\left(\nabla_{\theta} \log \pi_{\theta}\left(\mathrm{a}_{\mathrm{t}} \mid \mathrm{s}_{\mathrm{t}}\right)\right)\left(\sum_{\mathrm{t}=1}^{\mathrm{T}} \mathrm{r}\left(\mathrm{s}_{\mathrm{t}}, \mathrm{a}_{\mathrm{t}}\right)\right)\right] \\ &=\frac{1}{N} \sum_{\mathrm{i}=1}^{\mathrm{N}}\left[\left(\sum_{\mathrm{t}=1}^{\mathrm{T}} \nabla_{\theta} \log \pi_{\theta}\left(\mathrm{a}_{\mathrm{i}, \mathrm{t}} \mid \mathrm{s}_{\mathrm{i}, \mathrm{t}}\right)\right)\left(\sum_{\mathrm{t}=1}^{\mathrm{N}} \mathrm{r}\left(\mathrm{s}_{\mathrm{i}, \mathrm{t}}, \mathrm{a}_{\mathrm{i}, \mathrm{t}}\right)\right)\right] \end{aligned} ∇θJ(θ)=Eτ∼pθ(τ)[∇θlogpθ(τ)r(τ)]=Eτ∼pθ[(∇θlogπθ(at∣st))(t=1∑Tr(st,at))]=N1i=1∑N[(t=1∑T∇θlogπθ(ai,t∣si,t))(t=1∑Nr(si,t,ai,t))]
重要性重采样
使用另外一种数据分布,来逼近所求分布的一种方法,算是一种期望修正的方法,公式是:
∫ f ( x ) p ( x ) d x = ∫ f ( x ) p ( x ) q ( x ) q ( x ) d x = E x ∼ q [ f ( x ) p ( x ) q ( x ) ] = E x ∼ p [ f ( x ) ] \begin{aligned} \int \mathrm{f}(\mathrm{x}) \mathrm{p}(\mathrm{x}) \mathrm{dx} &=\int \mathrm{f}(\mathrm{x}) \frac{\mathrm{p}(\mathrm{x})}{\mathrm{q}(\mathrm{x})} \mathrm{q}(\mathrm{x}) \mathrm{dx} \\ &=\mathrm{E}_{\mathrm{x} \sim \mathrm{q}}\left[\mathrm{f}(\mathrm{x}) \frac{\mathrm{p}(\mathrm{x})}{\mathrm{q}(\mathrm{x})}\right] \\ &=\mathrm{E}_{\mathrm{x} \sim \mathrm{p}}[\mathrm{f}(\mathrm{x})] \end{aligned} ∫f(x)p(x)dx=∫f(x)q(x)p(x)q(x)dx=Ex∼q[f(x)q(x)p(x)]=Ex∼p[f(x)]
在已知 q q q 的分布后,可以使用上述公式计算出从 p 分布的期望值。也就可以使用 q q q 来对于 p 进行采样了,即为重要性采样。
边栏推荐
- Swift 16进制字符串与UIColor互转
- js 实现 encode64 加密
- Connaissance détaillée du GRE, du mgre; Connaissance de la configuration de base de l'OSPF
- Comment creo 9.0 modifie - t - il rapidement le système de coordonnées Cao?
- 200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
- OSPF details (1)
- How hot is July? Crawl through the crawler to get the temperature information of the current month, and use Matplotlib to draw the temperature line chart
- [激光器原理与应用-7]: 半导体制冷片与TEC温控器
- Detailed introduction of RIP
- PyTorch到底好用在哪里?
猜你喜欢

使用Stream流来进行分类展示。

PyTorch到底好用在哪里?

天玑920相当于骁龙什么 天玑920相当于骁龙多少 天玑920怎么样

rtx3080ti和rtx3080差距 3080和3080ti参数对比

Ansible first knowledge of learning one

Static comprehensive experiment (HCIA)

第四天笔记

Rip experiment

Which is the difference between iqoo 10 pro and vivo X80 pro? Detailed parameter configuration comparison

GRE,MGRE的详细了解;OSPF基础配置知识
随机推荐
script之type=module
GRE,MGRE的詳細了解;OSPF基礎配置知識
第十二天笔记
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
Notes on animal farm
Configure the firetracker process, i.e. stepping on the pit record
ERP production operation control
200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
How to judge whether an object is empty
考研题库小程序中如何实现打开考研思维导图pdf
[激光器原理与应用-7]: 半导体制冷片与TEC温控器
rtx3090ti什么水平 rtx3090ti显卡什么级别 rtx3090ti显卡怎么样
Golang remote server debugging
Swift 16进制字符串与UIColor互转
Connaissance détaillée du GRE, du mgre; Connaissance de la configuration de base de l'OSPF
Kingbaseesv8r6 difference of xmin under different isolation levels
OSPF comprehensive experiment
MGRE综合实验
excel随笔记录
使用Stream流来进行分类展示。