Prioritized Replay Weights

Reinforcement LearningMedium+15 XP+15

Prioritized Replay Weights

A uniform replay buffer wastes most of its samples on transitions the agent already understands. Prioritized replay samples the surprising ones more often — but sampling unevenly biases what the network learns, so it then has to correct for its own bias. Both halves are pure arithmetic.

Sampling probabilities. Raise each priority to the power alpha and normalise:

P(i)=piα∑jpjαP(i) = \frac{p_i^{\alpha}}{\sum_j p_j^{\alpha}}P(i)=∑j​pjα​piα​​

alpha controls how aggressive the prioritisation is. At alpha = 0 every priority becomes 1 and you're back to uniform sampling.

Importance-sampling weights. A transition drawn too often must count for less:

wi=(N⋅P(i))−βw_i = \big(N \cdot P(i)\big)^{-\beta}wi​=(N⋅P(i))−β

where N is how many transitions are in the buffer. Then divide every weight by the largest one, so the biggest weight is exactly 1 and no update is ever scaled up.

Task: write priority_weights(priorities, alpha, beta) returning [probabilities, weights] — two lists, each rounded to 4 decimal places.

  • priorities holds positive numbers, one per transition in the buffer.
  • Round only at the end. Normalise the weights using the unrounded maximum.
  • No sampling happens here, and none is needed: these two lists are the algorithm.

Note how the two halves pull against each other by design. A high-priority transition gets a large P(i), which gives it a small weight — it's seen often, so each sighting counts for less. beta sets how completely that correction is applied, and in practice it's annealed from small to 1 over training, so early learning gets the speed of biased sampling and later learning gets the correctness.

Examples

priorities = [9.0, 1.0, 2.0]
alpha = 1.0
beta = 0.5
returns [[0.75, 0.0833, 0.1667], [0.3333, 1.0, 0.7071]]
priorities = [1.0, 1.0, 1.0]
alpha = 0.6
beta = 0.4
returns [[0.3333, 0.3333, 0.3333], [1.0, 1.0, 1.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run