Give two lists, P=[p_1,p_2,p_3,..,p_i]
with i positive value, N=[n_1,n_2,...,n_j]
with j negative value, give a target T, I want to select some elements from P and N so that their sum is T.
I want to use Torch to achieve this, So, the Goal is to minimize the function:
Loss= w_1*P + w_2*N - T,
where w_1 and w_2 are trainable parameters.
But the loss does not converge but decreases linearly, from positive to negative values.
data=torch.load('use_data.pkl')
delta = torch.zeros(data[0].shape, requires_grad=True, device="cuda")
`#P and delta shape: 382 `
delta2 = torch.zeros(data[2].shape, requires_grad=True, device="cuda") # N
`#N and delta2 shape: 386 `
P=data[0]
N=data[2]
T=data[4][0]
opt = torch.optim.Adam([delta,delta2], lr=0.0001)
for s in range(10000):
opt.zero_grad()
main_loss=torch.sum(delta*P)+torch.sum(delta2*N)-T
#loss = main_loss + (delta.norm(2) + delta2.norm(2))*0.01
main_loss.backward()
opt.step()