PIXELBANKv9.1.0
Menu

Implement reward normalization using a running mean and variance.

During RLHF training, reward scores are normalized to have zero mean and unit variance using running statistics. Given a sequence of reward batches, update running mean and variance using exponential moving average (EMA):

μt=(1−α)⋅μt−1+α⋅rˉt\mu_t = (1 - \alpha) \cdot \mu_{t-1} + \alpha \cdot \bar{r}_t σt2=(1−α)⋅σt−12+α⋅var(rt)\sigma^2_t = (1 - \alpha) \cdot \sigma^2_{t-1} + \alpha \cdot \text{var}(r_t)

Normalized reward: r^=(r−μt)/σt2+ϵ\hat{r} = (r - \mu_t) / \sqrt{\sigma^2_t + \epsilon}

Input:

  • Line 1: alpha epsilon (EMA decay, stability constant)
  • Line 2: N (number of batches)
  • Next N lines: space-separated reward values for each batch

Output: Normalized rewards of the LAST batch, rounded to 4 decimal places.

Example:

Input:
0.1 1e-8
2
1.0 2.0 3.0
4.0 5.0 6.0
Output:
0.8885 1.9438 2.9991
Reasoning:
  • We initialize the running mean μ0\mu_0 and variance σ02\sigma^2_0 to 0, and read the EMA decay α=0.1\alpha = 0.1 and stability constant ϵ=1e−8\epsilon = 1e-8.
  • For the first batch [1.0,2.0,3.0][1.0, 2.0, 3.0], we calculate the mean rˉ1=(1.0+2.0+3.0)/3=2.0\bar{r}_1 = (1.0 + 2.0 + 3.0) / 3 = 2.0 and variance var(r1)=(1.02+2.02+3.02)/3−2.02=1.0\text{var}(r_1) = (1.0^2 + 2.0^2 + 3.0^2) / 3 - 2.0^2 = 1.0, then update the running mean and variance using the EMA formulas: μ1=(1−0.1)â‹…0+0.1â‹…2.0=0.2\mu_1 = (1 - 0.1) \cdot 0 + 0.1 \cdot 2.0 = 0.2 and σ12=(1−0.1)â‹…0+0.1â‹…1.0=0.1\sigma^2_1 = (1 - 0.1) \cdot 0 + 0.1 \cdot 1.0 = 0.1.
  • For the second batch [4.0,5.0,6.0][4.0, 5.0, 6.0], we calculate the mean rˉ2=(4.0+5.0+6.0)/3=5.0\bar{r}_2 = (4.0 + 5.0 + 6.0) / 3 = 5.0 and variance var(r2)=(4.02+5.02+6.02)/3−5.02=1.0\text{var}(r_2) = (4.0^2 + 5.0^2 + 6.0^2) / 3 - 5.0^2 = 1.0, then update the running mean and variance: μ2=(1−0.1)â‹…0.2+0.1â‹…5.0=0.52\mu_2 = (1 - 0.1) \cdot 0.2 + 0.1 \cdot 5.0 = 0.52 and σ22=(1−0.1)â‹…0.1+0.1â‹…1.0=0.19\sigma^2_2 = (1 - 0.1) \cdot 0.1 + 0.1 \cdot 1.0 = 0.19.
  • We normalize the rewards in the last batch using the updated running mean and variance: r^=(r−0.52)/0.19+1e−8\hat{r} = (r - 0.52) / \sqrt{0.19 + 1e-8}, resulting in the normalized rewards $[0.8885, 1.9438, 2.999

Constraints:

  • Initialize μ₀ = 0, σ²₀ = 1
  • 0 < alpha < 1, epsilon = 1e-8
  • Round to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.