question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression in scipy.optimize when upper-bound = lower-bound to fix a parameter

See original GitHub issue

Dear Scipy team,

I am the maintainer of a pyFAI and got a bug report that a feature in my code stopped working: https://github.com/silx-kit/pyFAI/issues/1421 On my side the bug comes down to the call to scipy.optimize.fmin_slsqp here: https://github.com/silx-kit/pyFAI/blob/master/pyFAI/geometryRefinement.py#L361

The regression was introduced in scipy v1.5 an now I got a warning with:

WARNING:py.warnings:/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx

and of course the optimization did not work.

Until now my code was offering to the use to “fix” some parameter during the optimization, which helps the optimizer to converge faster to the solution. To “fix” the parameter, I was enforcing both bounds to be equal to the fixed value but since the approx_jacobian in slsqp.py changed to call approx_derivative no more optimization occures.

Reproducing code example:

If I already know the first parameter should be close to 1 and I want to fix it:

>>> f = lambda x:(x[0]-1)**2*(x[1]-3)**2  # solution expected to be (1,3)                                                                        
>>> scipy.optimize.fmin_slsqp(f, [1,1], bounds=[(1,1), (None, None)])                                            
/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx
Inequality constraints incompatible    (Exit mode 4)
            Current function value: 0.0
            Iterations: 1
            Function evaluations: 3
            Gradient evaluations: 1
Out[8]: array([1., 1.])

Error message:

There was not exception

Scipy/Numpy/Python version information:

Python: 3.8.0 Numpy: 1.18.4 Scipy: 1.5.0 - 1.5.3

You may consider I have been misusing this feature … but the I need to implement a work around on my side.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
andyfaffcommented, Nov 2, 2020

This is a duplicate of #12502, and should be fixed by#12889. Are you able to verify that the modifications made in the latter fix the issue for you?

0reactions
andyfaffcommented, Nov 2, 2020

Closed as duplicate

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.optimize.Bounds — SciPy v1.9.3 Manual
Set components of lb and ub equal to fix a variable. Use np.inf with an appropriate sign to disable bounds on all or...
Read more >
setting bounded and fixed parameters in scipy fitting routines
One caveat is that the scipy.optimize.leastsq() method requires reasonable initial parameters and sometimes it fails the fit. A more robust ...
Read more >
Linear regression with constrained intercept - Stack Overflow
Scipy's curve_fit will accept bounds. For example to set a upper bound only on a parameter, that parameter's bound would be [-numpy.inf, upper...
Read more >
Scientific Python: Using SciPy for Optimization
minimize () will always pass the current value of the solution x into the objective function, so this argument serves as a place...
Read more >
Least-Squares models and their applications - scipy.optimize
Least-Squares models and their applications - scipy. optimize Minimizing a function using Least-Squares.Solving large-scale system of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found