title: How to use a for loop length as an optimization parameter in Pytorch
body: <p>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.</p>
  <p>Here is an example code (the real problem is more complex but you will get the idea):</p>
  <pre><code>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())
  </code></pre>
  <p>The optimization does not seem to work, here is the output:</p>
  <pre><code>
  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
  </code></pre>
  <p>Could you advise on how to make the for loop length an optimization parameter.</p>
  <p>Thank you</p>