4

I am trying to register my loop's length as an optimization parameter so that the optimizer will automatically adjust the length of the loop.

Here is an example code (the real problem is more complex but you will get the idea):

import torch

positive_iter = torch.tensor([10.0], requires_grad=True)
negative_iter = torch.tensor([20.0], requires_grad=True)

optimizer = torch.optim.Adam([positive_iter, negative_iter], lr=0.02, betas=(0.5, 0.999))

for i in range(100):
    loss = torch.tensor([0.0], requires_grad=True)
    for g in range(int(positive_iter)):
        loss = loss + torch.rand(1)
    for d in range(int(negative_iter)):
        loss = loss - torch.rand(1) * 2

    loss = torch.abs(loss)
    loss.backward()
    optimizer.step()
    print(i, loss.item(), positive_iter.item(), negative_iter.item())

The optimization does not seem to work, here is the output:


0 19.467784881591797 10.0 20.0
1 14.334418296813965 10.0 20.0
2 13.515042304992676 10.0 20.0
3 13.477707862854004 10.0 20.0
4 15.240434646606445 10.0 20.0
5 18.45014190673828 10.0 20.0
6 18.557266235351562 10.0 20.0
7 16.325769424438477 10.0 20.0
8 13.95105266571045 10.0 20.0
9 12.435094833374023 10.0 20.0
10 13.70322322845459 10.0 20.0
11 10.128765106201172 10.0 20.0
12 16.986034393310547 10.0 20.0
13 15.652003288269043 10.0 20.0
14 10.300052642822266 10.0 20.0
15 18.038368225097656 10.0 20.0
16 11.830389022827148 10.0 20.0
17 14.917057037353516 10.0 20.0
18 18.603071212768555 10.0 20.0
19 17.595298767089844 10.0 20.0
20 17.17181968688965 10.0 20.0
21 14.548274993896484 10.0 20.0
22 18.839675903320312 10.0 20.0
23 13.375761032104492 10.0 20.0
24 14.045333862304688 10.0 20.0
25 13.088285446166992 10.0 20.0
26 15.019135475158691 10.0 20.0
27 16.992284774780273 10.0 20.0
28 13.883159637451172 10.0 20.0
29 12.695013999938965 10.0 20.0
30 17.23816680908203 10.0 20.0
...continued

Could you advise on how to make the for loop length an optimization parameter.

Thank you

Etienne D
  • 111
  • 4
  • I also tried using tensors and a while loop but does not work either: `g = torch.tensor([0.0], requires_grad=True); while g < positive_iter: loss = loss + torch.rand(1); g = g + 1` ` – Etienne D Sep 20 '20 at 19:14
  • 3
    The reason why the loop limits are not working is because the limits are actually not taken into account when building the computational graph. Remember that to optimise variables, the forward pass must do mathematical operations with them in order for the backward pass to work. Since the loop indices are not actually being used in the graph where you're just using them as the upper or lower limits of a loop, there is nothing in the graph to backpropagate for optimising. I am not even sure if this could be framed in a linear optimisation framework. This sounds like Integer Programming. – rayryeng Sep 20 '20 at 20:11
  • Continued - perhaps try `cvxpy`: https://towardsdatascience.com/integer-programming-in-python-1cbdfa240df2 – rayryeng Sep 20 '20 at 20:12

0 Answers0