I have a similar situation. I’m trying to only keep samples for which the model’s confidence is about a certain threshold. Should the following work?
output = model(images)
loss = loss_function(output, targets)

output = output / softmax_temp
output_probs = torch.softmax(output, dim=-1)
largest_prob, _ = torch.max(output_probs, dim=-1)
mask = torch.gt(output_probs > thr)
loss = loss[mask]
loss = torch.mean(loss)
loss.backward()