Cost functions like CrossEntropyLoss have a parameter reduction that you can set to none so that you get the loss per instance, as I understand it. You could maybe use that and weight the relevant instances yourself before combining all losses and calling backward.