I am attempting to constrain some outputs of my regression network, say x, y, z = model(data), where x, y, z are scalars. The constrain that I want to impose is that when predicting all three dependent variables, the condition “x + y <=1.0” must be honored. Given this description, can I implement this in a forward function?

Ok I found a way. You could do something like this

ratio = 1/(x+y)
x *= ratio
y *= ratio

Then the output of x + y will always be 1. The only problem is that if x+y is already less than one this will make it equal to one. To solve that you could just use an if else statement like this

if x+y > 1:
ratio = 1/(x+y)
x *= ratio
y *= ratio
else:
continue

Hi. Yes, indeed, it did help. So, what I did was I had one Linear layer with a single output for x, which is then connected to a loss function (x_hat, x) and to another that has to make sure that (x_hat + y, ones(size(x_hat+y)). So, in total I had three loss functions. At last, I had to weigh each respective loss function.