What, exactly, does the REINFORCE update equation mean?

I understand that this is the update for the parameters of a policy in REINFORCE:

$$\Delta \theta_{t}=\alpha \nabla_{\theta} \log \pi_{\theta}\left(a_{t} \mid s_{t}\right) v_{t}$$

Where 𝑣𝑡 is usually the discounted future reward and 𝜋𝜃(𝑎𝑡|𝑠𝑡) is the probability of taken the action that the agent took at time 𝑡. (Tell me if something is wrong here)

But I don’t understand how that is passed into a neural network for back propagation.
I have this pseudocode
probs = policy.feedforward(state)
This returns the probabilities if taking each actions, like: [0.6,0.4]

action = choose_action_from(probs) this will return the index of the probability chosen. For example, if it chose 0.6, the action would be 0.

Then later when it is time to update, is it:

gradient = policy.backpropagate(total_discounted_reward*log(probs[action])


Is this the right way to calculate the derivative of the loss and backpropagate it? And I only backpropagate this through one output neuron?

If you need more explanation, I have this question on SO.

• I use a cross entropy loss when I implement this in PyTorch. I tried doing a forward pass of the network evaluated at the current action and taking the log but it didn't seem to work when I implemented it, so I stuck with cross entropy loss. – David Ireland Sep 16 '20 at 16:31
• Is this question related to a specific library (eg. pytorch or tensorflow)? Or is this just a question for a more specific but still general pseudocode? – Hai Nguyen Sep 16 '20 at 16:42
• @David Ireland Cross entropy with what? What would be the label, y hat, or correct answer in the equation? – S2673 Sep 16 '20 at 16:51
• @Hai Nguyen It is a lot about the general pseudocode, but if you click on the link you can see I built my own neural network out of just NumPy. – S2673 Sep 16 '20 at 16:53
• @S2673 It is cross-entropy loss using as the label the action you actually took during sampling. Multiplying by the value function is really critical addition though, it can even reverse the sign of all the gradients (which makes sense - if you have chosen an action and it was a bad choice, you want to reduce the chance of taking it again) – Neil Slater Sep 16 '20 at 17:05

• @S2673 The algorithm doesn't change in this situation. Say your NN outputs the mean parameter of the Gaussian, then $\log_\pi(a_t | s_t)$ is just the log of the normal density evaluated at the action you took where the mean parameter in the density is the output of your NN. You are then able to backpropagate through this to update the weights of your network. – David Ireland Jan 7 at 11:22