In Reinforcement Learning from Human Feedback (RLHF), a key challenge is preventing the policy model from diverging too far from the reference model while maximizing reward. KL-penalized reward shaping modifies the raw reward signal by subtracting a penalty proportional to the KL divergence between the policy and reference distributions.
In practice, systems often use an adaptive KL coefficient (beta) that automatically adjusts based on whether the observed KL divergence is within a desired target range.
Implement a function adaptive_kl_penalized_reward that processes multiple responses generated by a policy model. For each response, you are given:
Your function should:
beta_update_factorbeta_update_factorReturn a dictionary containing per-response KL values, the mean KL, shaped rewards, and the updated beta. All values should be rounded to 4 decimal places.