Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
4ebe2364
Commit
4ebe2364
authored
Mar 16, 2018
by
Martin Reinecke
Browse files
do not use iteration controllers for Scipy minimizers
parent
5623a2a9
Pipeline
#26190
failed with stages
in 4 minutes and 54 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
nifty4/minimization/scipy_minimizer.py
View file @
4ebe2364
...
...
@@ -21,37 +21,11 @@ from .minimizer import Minimizer
from
..field
import
Field
from
..
import
dobj
from
..logger
import
logger
from
.iteration_controller
import
IterationController
class
ScipyMinimizer
(
Minimizer
):
"""Scipy-based minimizer
Parameters
----------
controller : IterationController
Object that decides when to terminate the minimization.
method : str
The selected Scipy minimization method.
options : dictionary
A set of custom options for the selected minimizer.
"""
def
__init__
(
self
,
controller
,
method
,
options
,
need_hessp
):
super
(
ScipyMinimizer
,
self
).
__init__
()
if
not
dobj
.
is_numpy
():
raise
NotImplementedError
self
.
_controller
=
controller
self
.
_method
=
method
self
.
_options
=
options
self
.
_need_hessp
=
need_hessp
def
__call__
(
self
,
energy
):
class
_MinimizationDone
(
BaseException
):
pass
class
_MinHelper
(
object
):
def
__init__
(
self
,
controller
,
energy
):
self
.
_controller
=
controller
class
_MinHelper
(
object
):
def
__init__
(
self
,
energy
):
self
.
_energy
=
energy
self
.
_domain
=
energy
.
position
.
domain
...
...
@@ -59,9 +33,6 @@ class ScipyMinimizer(Minimizer):
pos
=
Field
(
self
.
_domain
,
x
.
reshape
(
self
.
_domain
.
shape
))
if
(
pos
.
val
!=
self
.
_energy
.
position
.
val
).
any
():
self
.
_energy
=
self
.
_energy
.
at
(
pos
.
locked_copy
())
status
=
self
.
_controller
.
check
(
self
.
_energy
)
if
status
!=
self
.
_controller
.
CONTINUE
:
raise
_MinimizationDone
def
fun
(
self
,
x
):
self
.
_update
(
x
)
...
...
@@ -77,51 +48,68 @@ class ScipyMinimizer(Minimizer):
res
=
self
.
_energy
.
curvature
(
vec
)
return
res
.
val
.
flatten
()
class
ScipyMinimizer
(
Minimizer
):
"""Scipy-based minimizer
Parameters
----------
method : str
The selected Scipy minimization method.
options : dictionary
A set of custom options for the selected minimizer.
"""
def
__init__
(
self
,
method
,
options
,
need_hessp
,
bounds
):
super
(
ScipyMinimizer
,
self
).
__init__
()
if
not
dobj
.
is_numpy
():
raise
NotImplementedError
self
.
_method
=
method
self
.
_options
=
options
self
.
_need_hessp
=
need_hessp
self
.
_bounds
=
bounds
def
__call__
(
self
,
energy
):
import
scipy.optimize
as
opt
hlp
=
_MinHelper
(
self
.
_controller
,
energy
)
energy
=
None
status
=
self
.
_controller
.
start
(
hlp
.
_energy
)
if
status
!=
self
.
_controller
.
CONTINUE
:
return
hlp
.
_energy
,
status
x
=
hlp
.
_energy
.
position
.
val
.
flatten
()
try
:
if
self
.
_need_hessp
:
r
=
opt
.
minimize
(
hlp
.
fun
,
x
,
method
=
self
.
_method
,
jac
=
hlp
.
jac
,
hessp
=
hlp
.
hessp
,
options
=
self
.
_options
)
hlp
=
_MinHelper
(
energy
)
energy
=
None
# drop handle, since we don't need it any more
bounds
=
None
if
self
.
_bounds
is
not
None
:
if
len
(
self
.
_bounds
)
==
2
:
lo
=
self
.
_bounds
[
0
]
hi
=
self
.
_bounds
[
1
]
bounds
=
[(
lo
,
hi
)]
*
hlp
.
_energy
.
position
.
size
else
:
r
=
opt
.
minimize
(
hlp
.
fun
,
x
,
method
=
self
.
_method
,
jac
=
hlp
.
jac
,
options
=
self
.
_options
)
except
_MinimizationD
one
:
status
=
self
.
_controller
.
check
(
hlp
.
_energy
)
return
hlp
.
_energy
,
self
.
_controller
.
check
(
hlp
.
_energy
)
r
aise
ValueError
(
"unrecognized bounds"
)
x
=
hlp
.
_energy
.
position
.
val
.
flatten
(
)
hessp
=
hlp
.
hessp
if
self
.
_need_hessp
else
N
one
r
=
opt
.
minimize
(
hlp
.
fun
,
x
,
method
=
self
.
_method
,
jac
=
hlp
.
jac
,
hessp
=
hessp
,
options
=
self
.
_options
,
bounds
=
bounds
)
if
not
r
.
success
:
logger
.
error
(
"Problem in Scipy minimization:"
,
r
.
message
)
else
:
logger
.
error
(
"Problem in Scipy minimization"
)
return
hlp
.
_energy
,
self
.
_controller
.
ERROR
return
hlp
.
_energy
,
IterationController
.
ERROR
return
hlp
.
_energy
,
IterationController
.
CONVERGED
def
NewtonCG
(
controller
):
def
NewtonCG
(
xtol
,
maxiter
,
disp
=
False
):
"""Returns a ScipyMinimizer object carrying out the Newton-CG algorithm.
See Also
--------
ScipyMinimizer
"""
return
ScipyMinimizer
(
controller
,
"Newton-CG"
,
{
"xtol"
:
1e-20
,
"maxiter"
:
None
},
Tru
e
)
options
=
{
"xtol"
:
xtol
,
"maxiter"
:
maxiter
,
"disp"
:
verbose
}
return
ScipyMinimizer
(
"Newton-CG"
,
options
,
True
,
Non
e
)
def
L_BFGS_B
(
controller
,
maxcor
=
10
):
def
L_BFGS_B
(
ftol
,
gtol
,
maxiter
,
maxcor
=
10
,
disp
=
False
,
bounds
=
None
):
"""Returns a ScipyMinimizer object carrying out the L-BFGS-B algorithm.
See Also
--------
ScipyMinimizer
"""
return
ScipyMinimizer
(
controller
,
"L-BFGS-B"
,
{
"ftol"
:
1e-20
,
"gtol"
:
1e-20
,
"maxcor"
:
maxcor
},
False
)
options
=
{
"ftol"
:
ftol
,
"gtol"
:
gtol
,
"maxiter"
:
maxiter
,
"maxcor"
:
maxcor
,
"disp"
:
verbose
}
return
ScipyMinimizer
(
"L-BFGS-B"
,
options
,
False
,
bounds
)
test/test_minimization/test_minimizers.py
View file @
4ebe2364
...
...
@@ -30,11 +30,11 @@ spaces = [ift.RGSpace([1024], distances=0.123), ift.HPSpace(32)]
minimizers
=
[
'ift.VL_BFGS(IC)'
,
'ift.NonlinearCG(IC, "Polak-Ribiere")'
,
#
ift.NonlinearCG(IC, "Hestenes-Stiefel"),
#
'
ift.NonlinearCG(IC, "Hestenes-Stiefel"),
'ift.NonlinearCG(IC, "Fletcher-Reeves")'
,
'ift.NonlinearCG(IC, "5.49")'
,
'ift.NewtonCG(
IC
)'
,
'ift.L_BFGS_B(
IC
)'
,
'ift.NewtonCG(
xtol=1e-5,maxiter=1000
)'
,
'ift.L_BFGS_B(
ftol=1e-10,gtol=1e-5,maxiter=1000
)'
,
'ift.L_BFGS(IC)'
]
newton_minimizers
=
[
'ift.RelaxedNewton(IC)'
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment