Constrain weight matrix rows to have zero sum

I want to calculate Wx where W is a trainable weight matrix and x is a vector of input values, but given the particularity of the input values I want to ensure that the coefficients of each linear combination of x_i's sum to zero.

What would be the best way of doing this?

I could try subtracting the mean from W before using it in calculation, something like this…

V = Variable(...)
W = V - torch.mean(V, dim=0)
out = F.linear(x, W)

Is there a better way?

what you are doing seems reasonable and efficient.

1 Like