Discounted returns with episode ends
Lesson: Policy Gradient — REINFORCE from ScratchCheat sheetTrack: RL basics
Problem
REINFORCE scales the gradient of each action by the return that follows it. This return is the reward to go: the discounted sum of the rewards from that step to the end of the episode. A rollout buffer often holds several episodes in one array, with a done flag at the last step of each episode.
Task
Write three functions.
returns_to_go(rewards, gamma)returns the reward to go of one episode. For an episode of length T, G_{T-1} = r_{T-1} and G_t = r_t + \gamma G_{t+1} for t < T - 1.batch_returns(rewards, dones, gamma)returns the reward to go for a batch of episodes that are joined in one array. Whendones[t]isTrue, step t is the last step of its episode, so G_t = r_t. Otherwise G_t = r_t + \gamma G_{t+1}. The last step of the batch has no next step. Use G_{t+1} = 0 there, also when its done flag isFalse.normalize(returns)returns (G - \mu) / (\sigma + 10^{-8}). Here \mu is the mean and \sigma is the population standard deviation (ddof = 0). When the array has fewer than 2 values, return a copy of the values unchanged.
Input and output
| Name | Type | Shape | Meaning |
|---|---|---|---|
rewards | float array | (T,) | the reward at each step |
dones | bool array | (T,) | True at the last step of each episode |
gamma | float | scalar | the discount factor, from 0 to 1 |
returns | float array | (T,) | returns to normalize |
Each function returns a float array of shape (T,).
Constraints
1 <= T <= 1000and0 <= gamma <= 1.- Do not change the input arrays.
- Use numpy only.
Examples
pythonreturns_to_go(np.array([1.0, 0.0, 2.0]), 0.9)text[2.62 1.8 2. ]pythonrewards = np.array([1.0, 1.0, 1.0, 1.0, 1.0])
dones = np.array([False, True, False, False, True])
batch_returns(rewards, dones, 0.5)text[1.5 1. 1.75 1.5 1. ]pythonnormalize(np.array([1.0, 2.0, 3.0, 4.0]))text[-1.34164077 -0.44721359 0.44721359 1.34164077]Hints
4 hintsSolution and notes
LockedPass every check with Submit to open the reference solution and the notes.
Run it on your computer
Clone the repository, install numpy and run the same checks in a terminal:
python3 scripts/practice/run.py discounted-returns my_answer.py
python3 scripts/practice/run.py discounted-returns my_answer.py --examplesStart my_answer.py from starter.py.
Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.
Keyboard shortcuts
- Ctrl Enter
- Run the examples
- Ctrl Shift Enter
- Submit
- Tab / Shift Tab
- Indent or outdent the lines
- Esc then Tab
- Move the focus out of the editor
- Alt H
- Show the next hint
On a Mac, use Cmd in place of Ctrl.
Select Run to check the examples, or Submit to run every check.