Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-316

Class-Weighted Token Loss

Problem

Compute the class-weighted mean token loss. losses and labels are parallel lists, and weights maps each class label to its weight.

Return sum(weight * loss) / sum(weight), using a default weight of 1.0 for labels missing from weights. Return 0.0 when there are no tokens or the total weight is 0.

Hint

Normalize by the sum of applied weights, not by the number of tokens.

Test cases

case 1
in: [[1,2,3],["a","b","a"],{"a":2,"b":1}]
expected: 2
case 2
in: [[1,2,3],["a","b","a"],{}]
expected: 2
case 3
in: [[],[],{}]
expected: 0
case 4
in: [[1,1],["x","y"],{"x":0,"y":0}]
expected: 0
case 5
in: [[2,4],["a","b"],{"a":3}]
expected: 2.5
Starter codePython
def class_weighted_token_loss(losses, labels, weights):
    # Your code here
    pass

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