Use Pytorch optimizer to minimize a user function

Jean-Eric_Campagne: <p>Dear all,<br>
I have read many tutorials on how to use PyTorch to make a regression over a data set using  for instance a model composed of several linear layers and the MSE loss.</p>
<p>Well, imagine that a known  function F which depends on a variable x and some unknown parameters (p_j: j=0,…, P-1) with P relatively small,  is a composition of special functions that cannot be modelized by the usual layers. So, my problem is a classical minimization problem knowing the data {x_i,y_i}_i&lt;=N</p>
<pre><code class="lang-auto"> C(p_0,p_1,...,p_P) = Sum_i (F(x_i;{p_j}) - y_i)^2 
</code></pre>
<p>The “loss”  <code>C({p_j})</code> that I want to minimize  is the only function that I can call with a set of parameters.</p>
<p>Is there a way to use the PyTorch minimizers to get the minimum of <code>C({p_j})</code> ?</p>

albanD: <p>Hi,</p>
<p>If <code>F</code> is implemented with pytorch Tensors and is differentiable. Yes you will be able to do it.</p>

Jean-Eric_Campagne: <p>So, if I have just access to <code>C({p_i})</code> through a python function, I cannot use the optimizers?</p>

albanD: <p>You don’t have access to <code>{p_i}</code> ?<br>
You can do :</p>
<pre><code class="lang-python">params = [p_0, p_1...]
opt = optim.SGD(params, lr=0.1)

for e in range(10):
  loss = C(params)
  opt.zero_grad()
  loss.backward()
  opt.step() 
</code></pre>

Jean-Eric_Campagne: <p>yes yes I can feed numerical values to <code>C</code>(ie the parameters to determine).<br>
So, in your code snippet the params is a list of initial values eg.</p>
<pre><code class="lang-auto">params  = [0.1, 0.0001, -2., 1e3,...]
</code></pre>
<p>ok?</p>

albanD: <p>No, as I mentioned above, the function <strong>must</strong> work with pytorch Tensors.<br>
So <code>params = torch.tensor([0.1, 0.0001, -2., 1e3, ...], requires_grad=True)</code> (or a list of Tensors as in my example.<br>
Also, <code>C</code> <strong>must</strong> work with Tensors, if it converts it to python numbers or numpy arrays, gradients cannot be computed.</p>

Jean-Eric_Campagne: <p>ha yes, I understand, in F or C, the parameters must appear as torch.tensors. ok.</p>

Javier_Rodriguez_Pui: <p>Hi <a class="mention" href="/u/alband">@albanD</a> and <a class="mention" href="/u/jean-eric_campagne">@Jean-Eric_Campagne</a>  , i am facing the same problem here, but the  parameters are not being updated although i used requires grad = True.</p>
<pre><code class="lang-auto">    def minimize():
        xi = torch.tensor([1e-3, 1e-3, 1e-3, 1e-3, 1e-3, 1e-3], requires_grad=True)
        optimizer = torch.optim.Adam([xi], lr=0.1)
        for i in range(400):
            loss = self.f(xi)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        return xi
</code></pre>
<p>self.f(xi) is implemented in pytorch Tensors. Do you have any suggestions?</p>

Jean-Eric_Campagne: <p>Hello,<br>
I guess the experts will ask you to provide a complete working snippet of your example. try to keep it as simple as possible, ie. no class, just functions (ie. I see self.f so I imagine that you have extracted the code from a class). Then, expose your problem with some outputs for instance.</p>
<p>Then, I can run your example and try to reproduce your problem.</p>