Dear all,
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.

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<=N

C(p_0,p_1,...,p_P) = Sum_i (F(x_i;{p_j}) - y_i)^2

The “loss” C({p_j}) that I want to minimize is the only function that I can call with a set of parameters.

Is there a way to use the PyTorch minimizers to get the minimum of C({p_j}) ?

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

Hi @albanD and @Jean-Eric_Campagne , i am facing the same problem here, but the parameters are not being updated although i used requires grad = True.

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

self.f(xi) is implemented in pytorch Tensors. Do you have any suggestions?

Hello,
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.

Then, I can run your example and try to reproduce your problem.