Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-206

Frequency Penalty Apply

Problem

Apply the OpenAI-style frequency penalty: subtract penalty times the number of occurrences of each generated token from its logit.

The signature is frequency_penalty(logits, generated_ids, penalty). Return a copy of the logits.

Hint

Frequency grows with each occurrence, so it penalizes repeated tokens more than presence does.

Test cases

case 1
in: [[1,2,3],[0,0],0.5]
expected: [0,2,3]
case 2
in: [[0,0],[1],1]
expected: [0,-1]
case 3
in: [[5,5,5],[0,2,2],0.25]
expected: [4.75,5,4.5]
case 4
in: [[1,2],[],0.5]
expected: [1,2]
Starter codePython
def frequency_penalty(logits, generated_ids, penalty):
    # Your code here
    pass

4 test cases · run your code against them in the editor