Hi everyone! I’m trying to implement the global pair loss function from “Recognition of Action Units in the Wild with Deep Nets and a New Global-Local Loss”.
I have to look at all possible pairs of the predictions. If the values in a pair are the same, then g_predictions = 1. If they are different, then g_predictions = 0.
I do the same thing for the targets.
Then I need to calculate the loss, by adding them all together.
total += (g_prediction - g_target)**2
This is my implementation:
def custom_loss(preds, targs):
total = 0
for pair_p, pair_t in zip(torch.combinations(preds, r=2), torch.combinations(targs, r=2)):
g1 = torch.eq(pair_p[0], pair_p[1]).type(torch.uint8)
g2 = torch.eq(pair_t[0], pair_t[1]).type(torch.uint8)
total += (g2 - g1)**2
return total
output = model(x)
loss = custom_loss(output, targets)
loss.backward()
I’m getting this error after backward() is called:
RuntimeError: element 0 of tensors does not require grad and does not have grad_fn
Am I retaining the graph properly? Does anyone know how to fix this?
Thanks for the help