Gradient of the PG metric

Introduction

预告

这节将是笔者认为这个笔记最精彩的一节,会有很多证明,放心食用👀

  • Given a metric, we next derive its gradient and then, apply gradient-based methods to optimize the metric.

Simply put, the unified gradient of the metric is given by

重要

赵老师PPT没有阐述的很清楚这个公式的推导,自己花了差不多一天时间来写的证明,用了matrix form的形式来证的,结合了很多Sutton书中的内容 policy_gradient_proof

Turn into Expectation

注意上面是如何把一个 逐渐提出来变成

  1. 我们首先提出来的是,为了清晰表示,这里在外面的加上了下标说明是对哪个random variable做期望的,同时将所有小写s替换成大写S,也说明了S此时的分布(on-policy distribution)
  2. 第二步提出来之前我们先凑出action probability,所以就又能把提出来,同时在外面的加上下标,同时将所有小写a替换成大写A
  3. 为什么要这样提出来变成一个期望形式?因为我们最终还是希望可以通过MC的方法去轻松采样到大量state-action pair的数据的,提出来之后我们就可以像之前一样update by samples

About the policy gradient

where

  • is the feature vector of state-action pair
  • is the parameter vector of the policy

Remarks:

  • such a form based on the softmax function can be realized by a neural network
    • input is , (continuous)
    • output is , (discrete, outputs)
    • the activation of the output layer is the softmax function
  • for all and
    • the parameterized policy is stochastic and hence exploratory

一些矩阵想法

按照之前的做法,我们这里看到也是可以写成矩阵形式的,由上面定义,我们知道

  • is a (n,m,k) tensor
  • is a (k,1) vector
  • is a (n,m) matrix

我们可以直接写成下面这样子:

但是我们的的shape应该是(k,1)的,容易看出:在前向传播过程中,我们的是和 (n,m,k)中的每一个(1,k)都做了向量dot-product-sum的操作(这里的广播操作),所以在反向传播过程中,就应该对我们最后得到的一个梯度(n,m,k)mean(dim=0,dim=1),这样就得到了一个(k,)的梯度;或者可以直接将上面的矩阵形式reshape成(k,n*m)的形式,然后再取mean(dim=1)即可;

TL;DR: 前向传播中的广播操作使得复制了nxm份,所以在反向传播中,我们需要对nxm份的梯度取平均