Evaluate a model that labels each token. Because the sequences have varying lengths, you pad them with pad_id to batch them together. Complete masked_accuracy(preds, labels, pad_id) to return the token accuracy. Example labels = [[1, 2, 3]], preds = [[1, 9, 3]] → positions 0 and 2 correct, position 1 wrong → accuracy = 2/3
This problem evaluates a model that labels each token. Because sequences have varying lengths, they are padded with pad_id to batch them together; you receive preds and labels and complete the function that returns the token accuracy. You work with same-shape integer numpy arrays and verify in code that the accuracy comes out as intended, including the edge with no tokens to count.
Compute token-level accuracy over padded batches with PyTorch, and verify the metric on the public example and on the no-token edge case (return 0.0 when there are no tokens to count). Token accuracy is a common evaluation metric for models that label each token (token classification like named-entity recognition and part-of-speech tagging, or seq2seq like translation and summarization), and to batch sequences of different lengths together you fill the empty cells with padding (pad_id).