TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

179. Repetition Penalty

Easy

Apply the repetition penalty to a logits vector. For each token id that has appeared in prev_tokens:

  • If logits[t] > 0: logits[t] /= penalty
  • Else: logits[t] *= penalty

This pushes already-seen tokens toward zero either way (when penalty > 1).

Signature: def apply_repetition_penalty(logits: np.ndarray, prev_tokens: list, penalty: float) -> np.ndarray

Return a new array; do not modify the input. Each unique token in prev_tokens is penalized exactly once (duplicate ids do not stack).

Math

z^t​=⎩⎨⎧​zt​/ρzt​⋅ρzt​​zt​>0, t∈prevzt​≤0, t∈prevotherwise​

Asked at

NumPy

import numpy as np

 

def apply_repetition_penalty(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?