Hi Frida!



 Frida:

Does anyone know how we can enforce the sum of few parameters to be equal to a given value?


You can add an L1 or L2 penalty (regularizing term) to your loss
function:
l1_reg = torch.abs (param1 + param2 + param3 - desired_sum)
# or l2_reg = (param1 + param2 + param3 - desired_sum)**2
loss_total = loss + l1_reg
# or loss_total = loss + l2_reg

Then call loss_total.backward() and optimize as you normally would.
Best.
K. Frank