Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-023

Unigram Log Probability

Problem

Return the total unigram log probability of a token sequence: the sum of math.log(probs[token]) over all tokens.

Treat a missing token as probability 0 and return float('-inf') in that case. Return 0.0 for an empty token list.

Hint

Logs turn products of probabilities into sums.

Test cases

case 1
in: [["a","b"],{"a":0.5,"b":0.25}]
expected: -2.0794415416798357
case 2
in: [["a","a","a"],{"a":0.1}]
expected: -6.907755278982137
case 3
in: [[],{"a":0.5}]
expected: 0
case 4
in: [["the","cat"],{"the":1,"cat":1}]
expected: 0
Starter codePython
import math
def unigram_logprob(tokens, probs):
    # Your code here
    pass

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