Reward Normalization
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​ σt2​=(1−α)⋅σt−12​+α⋅var(rt​)
Normalized reward: r^=(r−μt​)/σt2​+ϵ​
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:
0.1 1e-8 2 1.0 2.0 3.0 4.0 5.0 6.0
0.8885 1.9438 2.9991
- We initialize the running mean μ0​ and variance σ02​ to 0, and read the EMA decay α=0.1 and stability constant ϵ=1e−8.
- For the first batch [1.0,2.0,3.0], we calculate the mean 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, then update the running mean and variance using the EMA formulas: μ1​=(1−0.1)⋅0+0.1⋅2.0=0.2 and σ12​=(1−0.1)⋅0+0.1⋅1.0=0.1.
- For the second batch [4.0,5.0,6.0], we calculate the mean 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, then update the running mean and variance: μ2​=(1−0.1)⋅0.2+0.1⋅5.0=0.52 and σ22​=(1−0.1)⋅0.1+0.1⋅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​, 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
More from LLM 2: Training & Alignment
Background Knowledge
The problem revolves around reward normalization, a crucial concept in Reinforcement Learning from Human Feedback (RLHF). In RLHF, the goal is to train a model to align with human preferences by providing rewards for desired behaviors. However, these rewards can have varying scales and distributions, making it challenging to compare and combine them. Normalization helps to standardize these rewards, ensuring that the model learns from them effectively.
The problem uses exponential moving average (EMA) to update the running mean (μt​) and variance (σt2​) of the rewards. EMA is a technique for calculating the mean of a set of values over time, giving more weight to recent values. This is useful in tracking changes in the reward distribution over time. The normalized reward (r^) is then calculated by subtracting the running mean and dividing by the square root of the running variance plus a small stability constant (ϵ).
Understanding the mathematical formulas provided is essential to solving this problem. The formulas for updating the running mean and variance using EMA are: μt​=(1−α)⋅μt−1​+α⋅rˉt​ σt2​=(1−α)⋅σt−12​+α⋅var(rt​) where α is the EMA decay rate, rˉt​ is the mean of the current batch of rewards, and var(rt​) is the variance of the current batch of rewards.
Algorithm/Approach
The general approach to solving this problem involves:
- Initializing the running mean and variance
- Iterating over each batch of rewards, updating the running mean and variance using EMA
- Calculating the normalized rewards for each batch
- Returning the normalized rewards of the last batch
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.