Implement Reinforce-Rej, a variant of REINFORCE used in RL finetuning of language models. For each prompt, the policy has sampled N responses, each with a binary reward (1 = correct, 0 = incorrect) and the per-response log-probability under the current policy.
The idea is that prompt groups where all N responses are correct (too easy) or all N are incorrect (too hard) provide no useful learning signal and should be discarded entirely. The remaining 'informative' groups are used to compute a standard REINFORCE policy-gradient loss.
Write a function reinforce_rej_loss(log_probs, rewards) where:
log_probs: numpy array of shape (P, N) containing log-probabilities of each sampled response.rewards: numpy array of shape (P, N) containing binary rewards in {0, 1}.Steps:
log_prob * reward over all surviving (prompt, sample) entries.0.0.Return a single Python float (do not round inside the solution).