Compute masked next-token loss
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
- Complete the starter function using the contract above.
- Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
- 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
EXAMPLE 2
Inputmasked_nll([1], [True])
Output0.0
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor