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):
Normalized reward:
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 and variance to 0, and read the EMA decay and stability constant .
- For the first batch , we calculate the mean and variance , then update the running mean and variance using the EMA formulas: and .
- For the second batch , we calculate the mean and variance , then update the running mean and variance: and .
- We normalize the rewards in the last batch using the updated running mean and variance: , 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