In Part 3: Intro to Policy Optimization from spinningup documentation, there is a formula to compute the estimate of the policy gradient:
''' This is an expectation, which means that we can estimate it with a sample mean. If we collect a set of trajectories $ D = \{\tau_i\}_{i=1,...,N} $ where each trajectory is obtained by letting the agent act in the environment using the policy $\pi_{\theta}$, the policy gradient can be estimated with
$$ \hat{g} = \frac{1}{|D|} \sum_{\tau \in D} \sum_{t=0}^{T} \nabla_{\theta} \log \pi_{\theta}(a_t |s_t) R(\tau), $$
where $|D|$ is the number of trajectories in $D$ (here, $N$). '''
Also in the code on the same page to compute loss:
# make loss function whose gradient, for the right data, is policy gradient
def compute_loss(obs, act, weights):
logp = get_policy(obs).log_prob(act)
return -(logp * weights).mean()
Here the mean() is calculated over all the (state, action) pairs. However, the formula above calculates the mean over trajectories. There seems to be a disconnect. Are these two forms equivalant?