Consider you have simple classifier whose input size is [20,5] with 20 being the batch_size. After you forward the input to the model, it will give the output of size [20, num_classes].
Now, when you calculate the loss using the nn.CrossEntropyLoss() or nn.MSELoss(), you have an argument reduction, if you pass its value as 'none', the resulting loss-tensor would be a vector of size 25. Each entry in the loss would be the corresponding loss for each image in the batch.
You can apply the Boolean-mask here and then average it or sum it up.