Workspace/Coding labs
Loading progress

Compute masked next-token loss

Foundation50 min

Implement masked_nll(probabilities, mask). Both sequences must have equal lengths; each mask entry must be an exact bool. Compute mean natural-log negative likelihood over True positions. Validate probabilities only at included positions as finite numeric values in (0,1]. Masked positions may contain placeholders. Reject an all-masked or empty sequence with ValueError. The function returns a scalar loss, not perplexity.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputmasked_nll([0.5, 0, 0.25], [True, False, True])

Output1.0397207708399179

Padding probability zero is excluded before logarithms.
EXAMPLE 2

Inputmasked_nll([1], [True])

Output0.0

A target with probability one has zero loss.
solution.pyPython 3.12