Your loss function is programmatically correct except for below:
# the number of tokens is the sum of elements in mask
num_tokens = int(torch.sum(mask).data[0])
When you do torch.sum it returns a 0-dimensional tensor and hence the warning that it can’t be indexed. To fix this do int(torch.sum(mask).item()) as suggested or int(torch.sum(mask)) will work too.
Now, are you trying to emulate the CE loss using the custom loss? If yes, then you are missing the log_softmax
To fix that add outputs = torch.nn.functional.log_softmax(outputs, dim=1) before statement 4. Note that in case of tutorial that you have attached, log_softmax is already done in the forward call. You can do that too.