Skip to content
DeepForgeby svx
All problems
HardNLPnlp-313

Sequence Likelihood with Temperature

Problem

Compute the total log probability of a token sequence under temperature-scaled softmax distributions. logit_rows is a list of per-step logit lists and targets is the list of chosen token indices, one per row.

Sum log softmax(logits / temperature)[target] over all steps using the max-subtraction trick. Return 0.0 when the row and target counts differ.

Hint

Work in log space with the max-subtraction trick, then add each step's target log probability.

Test cases

case 1
in: [[[1,1]],[0],1]
expected: -0.6931471805599453
case 2
in: [[[2,0]],[1],1]
expected: -2.1269280110429727
case 3
in: [[[2,0]],[1],2]
expected: -1.3132616875182228
case 4
in: [[[0,0],[0,0]],[0,0],1]
expected: -1.3862943611198906
case 5
in: [[[1,2,3]],[2],1]
expected: -0.4076059644443804
Starter codePython
import math
def sequence_likelihood_temperature(logit_rows, targets, temperature):
    # Your code here
    pass

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