Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-288

GNMT Length Penalty Beam Score

Problem

Apply the GNMT length penalty to a beam log probability:

score = logprob / (((5 + length) / 6) ** alpha)

length is the hypothesis length including the end token, and alpha is the penalty exponent. Return 0.0 when length is negative.

Hint

The penalty factor is 1.0 when length is 1, and it grows with longer hypotheses.

Test cases

case 1
in: [-3,1,1]
expected: -3
case 2
in: [-3,7,1]
expected: -1.5
case 3
in: [-3,7,0]
expected: -3
case 4
in: [0,3,2]
expected: 0
case 5
in: [-2.5,1,2]
expected: -2.5
Starter codePython
def beam_score_gnmt(logprob, length, alpha):
    # Your code here
    pass

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