Closed KryeKuzhinieri closed 1 year ago
My first instinct would be to suspect that the Python package is built with a different (older) nlopt library, so I would try to ensure that I compiled the Python side of things locally the same way as the R side. Next might be parameterization.
Also, if you run Python from pre-made wheels they are built for maximum distribution and will have minimal optimisation. But that would likely not explain behavior like fewer calls to the eval.
Hi @eddelbuettel. Your feedback was invaluable because you made me profile the entire function which helped identify the bottleneck.
It appears that the issue was with gradients = nd.Gradient(formula)(x)
because it was taking too much time to find the gradient of the function. Changing it to gradients = nd.Gradient(formula, method="complex", order=0)(x)
reduced the time quite significantly.
Moreover, this helped me realize that the code could be optimized to make things much faster and I changed the pd.apply
function to np.where
which also increased the speed. But even though the code was much faster, it was still slower than the R version.
Eventually, I thought maybe using .backwards()
from the pytorch library could help. And oh boy it did since the implementation now is much faster than the R version.
So, for people who might encounter similar problems. The steps I took where:
requires_grad
for the x
variable.if grad.size > 0:
inputs = torch.tensor(x, requires_grad=True)
gradients = formula(inputs, grad=True)
gradients.backward()
grad[0] = inputs.grad[0].item()
grad[1] = inputs.grad[1].item()
sse = formula(x)
Thanks a lot for the suggestions!
Hi,
We have an old R code which we are trying to convert to python and it uses nloptr. Although both the implementations have the same input parameters, the converted python code works significantly slower than the R code. To demonstrate let's compare the R code with the python one.
On the other hand, the python code is as follows:
Now, both of these functions yield the same result in the end. However, there are the two issues:
On first inspection, I can see that the python code calls the eval grad on every iteration whereas the R code calls the eval grad function every 7 or so iterations.
I went ahead and compiled the nloptr package on my local machine and I see that more operations are going under the hood. Whereas the python code is just calling the standard nlopt package. Would it be possible to help me out with some pointers on the extra steps that are taken in the R code so that we can speed up our algorithm in python?
Thanks a lot!
Note: This is not an issue or a bug in nloptr but I am asking for suggestions.