New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix optimize defaults for older scipy versions for L-BFGS-B #476
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,15 @@ | |
Scipy < 0.12. All optimizers are available for scipy >= 0.12. | ||
""" | ||
|
||
import os | ||
from tempfile import mkstemp | ||
from distutils.version import LooseVersion | ||
import numpy as np | ||
import scipy | ||
|
||
SCIPY_LESS_0_12 = LooseVersion(scipy.__version__) < '0.12' | ||
|
||
if not SCIPY_LESS_0_12: | ||
|
||
from scipy.optimize import minimize | ||
|
||
else: | ||
|
||
from scipy.optimize import fmin_l_bfgs_b, fmin_powell | ||
|
||
|
||
|
@@ -104,7 +99,7 @@ def __init__(self, fun, x0, args=(), method='L-BFGS-B', jac=None, | |
|
||
callback : callable, optional | ||
Called after each iteration, as ``callback(xk)``, where ``xk`` is | ||
the current parameter vector. | ||
the current parameter vector. Only available using Scipy >= 0.12. | ||
|
||
options : dict, optional | ||
A dictionary of solver options. All methods accept the following | ||
|
@@ -117,33 +112,65 @@ def __init__(self, fun, x0, args=(), method='L-BFGS-B', jac=None, | |
`show_options('minimize', method)`. | ||
|
||
evolution : bool, optional | ||
save history of x for each iteration | ||
save history of x for each iteration. Only available using Scipy | ||
>= 0.12. | ||
|
||
See also | ||
--------- | ||
scipy.optimize.minimize | ||
""" | ||
|
||
self.size_of_x = len(x0) | ||
self.tmp_files = [] | ||
self._evol_kx = None | ||
|
||
_eps = np.finfo(float).eps | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is eps changed to the lowest possible value? you might get convergence issues at line 155 for example, and anywhere else it's used. |
||
|
||
if SCIPY_LESS_0_12: | ||
|
||
if evolution is True: | ||
print('Saving history is available only with Scipy >= 0.12.') | ||
|
||
if method == 'L-BFGS-B': | ||
default_options = {'maxcor': 10, 'ftol': 1e-7, 'gtol': 1e-5, | ||
'eps': 1e-8, 'maxiter': 1000} | ||
|
||
if jac is None: | ||
approx_grad = True | ||
else: | ||
approx_grad = False | ||
|
||
out = fmin_l_bfgs_b(fun, x0, args, | ||
approx_grad=approx_grad, | ||
bounds=bounds, | ||
m=options['maxcor'], | ||
factr=options['ftol'] / np.finfo(float).eps, | ||
pgtol=options['gtol'], | ||
epsilon=options['eps']) | ||
if options is None: | ||
options = default_options | ||
|
||
if options is not None: | ||
for key in options: | ||
default_options[key] = options[key] | ||
options = default_options | ||
|
||
try: | ||
out = fmin_l_bfgs_b(fun, x0, args, | ||
approx_grad=approx_grad, | ||
bounds=bounds, | ||
m=options['maxcor'], | ||
factr=options['ftol']/_eps, | ||
pgtol=options['gtol'], | ||
epsilon=options['eps'], | ||
maxiter=options['maxiter']) | ||
except TypeError: | ||
|
||
msg = 'In Scipy ' + scipy.__version__ + ' `maxiter` ' | ||
msg += 'parameter is not available for L-BFGS-B. \n Using ' | ||
msg += '`maxfun` instead with value twice of maxiter.' | ||
|
||
print(msg) | ||
out = fmin_l_bfgs_b(fun, x0, args, | ||
approx_grad=approx_grad, | ||
bounds=bounds, | ||
m=options['maxcor'], | ||
factr=options['ftol']/_eps, | ||
pgtol=options['gtol'], | ||
epsilon=options['eps'], | ||
maxfun=options['maxiter'] * 2) | ||
|
||
res = {'x': out[0], 'fun': out[1], 'nfev': out[2]['funcalls']} | ||
try: | ||
|
@@ -153,6 +180,17 @@ def __init__(self, fun, x0, args=(), method='L-BFGS-B', jac=None, | |
|
||
elif method == 'Powell': | ||
|
||
default_options = {'xtol': 0.0001, 'ftol': 0.0001, | ||
'maxiter': None} | ||
|
||
if options is None: | ||
options = default_options | ||
|
||
if options is not None: | ||
for key in options: | ||
default_options[key] = options[key] | ||
options = default_options | ||
|
||
out = fmin_powell(fun, x0, args, | ||
xtol=options['xtol'], | ||
ftol=options['ftol'], | ||
|
@@ -168,7 +206,7 @@ def __init__(self, fun, x0, args=(), method='L-BFGS-B', jac=None, | |
else: | ||
|
||
msg = 'Only L-BFGS-B and Powell is supported in this class ' | ||
msg += 'for versions of Scipy < 0.11.' | ||
msg += 'for versions of Scipy < 0.12.' | ||
raise ValueError(msg) | ||
|
||
if not SCIPY_LESS_0_12: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will soon be inaccurate, as we add stuff from my SFM and LiFE PRs. I might just change this in my PR. I have a few more changes proposed on top of this. You can leave this as-is, but I wanted to give you a heads up on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, cool.