# How to use torch to achieve a search algorithm？

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):
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()

``````

It sounds a bit as if your goal might be to get to zero rather than minimize.
In that case, you could, for example minimize the square of what you currently have, i.e.
`loss = (w_1 * P + w_ 2 * N - T) ** 2`

Best regards

Thomas

1 Like

Thanks for your reply, and there has another problem: why are the element values of w_1 and w_2 the same? Each element in w_1 is close to 0.6, and w_2 is close to -0.6 ?