Commit 026e670e authored by Martin Reinecke's avatar Martin Reinecke
Browse files

while we are at it: some orthography policing

parent 276ca31b
Pipeline #14537 passed with stage
in 6 minutes and 44 seconds
...@@ -146,14 +146,14 @@ class DescentMinimizer(Loggable, object): ...@@ -146,14 +146,14 @@ class DescentMinimizer(Loggable, object):
break break
# current position is encoded in energy object # current position is encoded in energy object
descend_direction = self.get_descend_direction(energy) descent_direction = self.get_descent_direction(energy)
# compute the step length, which minimizes energy.value along the # compute the step length, which minimizes energy.value along the
# search direction # search direction
step_length, f_k, new_energy = \ step_length, f_k, new_energy = \
self.line_searcher.perform_line_search( self.line_searcher.perform_line_search(
energy=energy, energy=energy,
pk=descend_direction, pk=descent_direction,
f_k_minus_1=f_k_minus_1) f_k_minus_1=f_k_minus_1)
f_k_minus_1 = energy.value f_k_minus_1 = energy.value
...@@ -195,5 +195,5 @@ class DescentMinimizer(Loggable, object): ...@@ -195,5 +195,5 @@ class DescentMinimizer(Loggable, object):
return energy, convergence return energy, convergence
@abc.abstractmethod @abc.abstractmethod
def get_descend_direction(self, energy): def get_descent_direction(self, energy):
raise NotImplementedError raise NotImplementedError
...@@ -25,31 +25,31 @@ from nifty import LineEnergy ...@@ -25,31 +25,31 @@ from nifty import LineEnergy
class LineSearch(Loggable, object): class LineSearch(Loggable, object):
"""Class for determining the optimal step size along some descent direction. """Class for determining the optimal step size along some descent direction.
Initialize the line search procedure which can be used by a specific line Initialize the line search procedure which can be used by a specific line
search method. Its finds the step size in a specific direction in the search method. Its finds the step size in a specific direction in the
minimization process. minimization process.
Attributes Attributes
---------- ----------
line_energy : LineEnergy Object line_energy : LineEnergy Object
LineEnergy object from which we can extract energy at a specific point. LineEnergy object from which we can extract energy at a specific point.
f_k_minus_1 : Field f_k_minus_1 : Field
Value of the field at the k-1 iteration of the line search procedure. Value of the field at the k-1 iteration of the line search procedure.
prefered_initial_step_size : float preferred_initial_step_size : float
Initial guess for the step length. Initial guess for the step length.
""" """
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
def __init__(self): def __init__(self):
self.line_energy = None self.line_energy = None
self.f_k_minus_1 = None self.f_k_minus_1 = None
self.prefered_initial_step_size = None self.preferred_initial_step_size = None
def _set_line_energy(self, energy, pk, f_k_minus_1=None): def _set_line_energy(self, energy, pk, f_k_minus_1=None):
"""Set the coordinates for a new line search. """Set the coordinates for a new line search.
...@@ -58,13 +58,13 @@ class LineSearch(Loggable, object): ...@@ -58,13 +58,13 @@ class LineSearch(Loggable, object):
---------- ----------
energy : Energy object energy : Energy object
Energy object from which we can calculate the energy, gradient and Energy object from which we can calculate the energy, gradient and
curvature at a specific point. curvature at a specific point.
pk : Field pk : Field
Unit vector pointing into the search direction. Unit vector pointing into the search direction.
f_k_minus_1 : float f_k_minus_1 : float
Value of the fuction (energy) which will be minimized at the k-1 Value of the fuction (energy) which will be minimized at the k-1
iteration of the line search procedure. (Default: None) iteration of the line search procedure. (Default: None)
""" """
self.line_energy = LineEnergy(position=0., self.line_energy = LineEnergy(position=0.,
energy=energy, energy=energy,
......
...@@ -120,8 +120,8 @@ class LineSearchStrongWolfe(LineSearch): ...@@ -120,8 +120,8 @@ class LineSearchStrongWolfe(LineSearch):
# set alphas # set alphas
alpha0 = 0. alpha0 = 0.
if self.prefered_initial_step_size is not None: if self.preferred_initial_step_size is not None:
alpha1 = self.prefered_initial_step_size alpha1 = self.preferred_initial_step_size
elif old_phi_0 is not None and phiprime_0 != 0: elif old_phi_0 is not None and phiprime_0 != 0:
alpha1 = min(1.0, 1.01*2*(phi_0 - old_phi_0)/phiprime_0) alpha1 = min(1.0, 1.01*2*(phi_0 - old_phi_0)/phiprime_0)
if alpha1 < 0: if alpha1 < 0:
......
...@@ -32,9 +32,9 @@ class RelaxedNewton(DescentMinimizer): ...@@ -32,9 +32,9 @@ class RelaxedNewton(DescentMinimizer):
convergence_level=convergence_level, convergence_level=convergence_level,
iteration_limit=iteration_limit) iteration_limit=iteration_limit)
self.line_searcher.prefered_initial_step_size = 1. self.line_searcher.preferred_initial_step_size = 1.
def get_descend_direction(self, energy): def get_descent_direction(self, energy):
""" Calculates the descent direction according to a Newton scheme. """ Calculates the descent direction according to a Newton scheme.
The descent direction is determined by weighting the gradient at the The descent direction is determined by weighting the gradient at the
...@@ -50,12 +50,9 @@ class RelaxedNewton(DescentMinimizer): ...@@ -50,12 +50,9 @@ class RelaxedNewton(DescentMinimizer):
Returns Returns
------- -------
descend_direction : Field descent_direction : Field
Returns the descent direction with proposed step length. In a Returns the descent direction with proposed step length. In a
quadratic potential this corresponds to the optimal step. quadratic potential this corresponds to the optimal step.
""" """
gradient = energy.gradient return -energy.curvature.inverse_times(energy.gradient)
curvature = energy.curvature
descend_direction = curvature.inverse_times(gradient)
return descend_direction * -1
...@@ -20,7 +20,7 @@ from .descent_minimizer import DescentMinimizer ...@@ -20,7 +20,7 @@ from .descent_minimizer import DescentMinimizer
class SteepestDescent(DescentMinimizer): class SteepestDescent(DescentMinimizer):
def get_descend_direction(self, energy): def get_descent_direction(self, energy):
""" Implementation of the steepest descent minimization scheme. """ Implementation of the steepest descent minimization scheme.
Also known as 'gradient descent'. This algorithm simply follows the Also known as 'gradient descent'. This algorithm simply follows the
...@@ -34,10 +34,9 @@ class SteepestDescent(DescentMinimizer): ...@@ -34,10 +34,9 @@ class SteepestDescent(DescentMinimizer):
Returns Returns
------- -------
descend_direction : Field descent_direction : Field
Returns the descent direction. Returns the descent direction.
""" """
descend_direction = energy.gradient return -energy.gradient
return descend_direction * -1
...@@ -40,7 +40,7 @@ class VL_BFGS(DescentMinimizer): ...@@ -40,7 +40,7 @@ class VL_BFGS(DescentMinimizer):
self._information_store = None self._information_store = None
return super(VL_BFGS, self).__call__(energy) return super(VL_BFGS, self).__call__(energy)
def get_descend_direction(self, energy): def get_descent_direction(self, energy):
"""Implementation of the Vector-free L-BFGS minimization scheme. """Implementation of the Vector-free L-BFGS minimization scheme.
Find the descent direction by using the inverse Hessian. Find the descent direction by using the inverse Hessian.
...@@ -57,7 +57,7 @@ class VL_BFGS(DescentMinimizer): ...@@ -57,7 +57,7 @@ class VL_BFGS(DescentMinimizer):
Returns Returns
------- -------
descend_direction : Field descent_direction : Field
Returns the descent direction. Returns the descent direction.
References References
...@@ -80,11 +80,11 @@ class VL_BFGS(DescentMinimizer): ...@@ -80,11 +80,11 @@ class VL_BFGS(DescentMinimizer):
b = self._information_store.b b = self._information_store.b
delta = self._information_store.delta delta = self._information_store.delta
descend_direction = delta[0] * b[0] descent_direction = delta[0] * b[0]
for i in xrange(1, len(delta)): for i in xrange(1, len(delta)):
descend_direction += delta[i] * b[i] descent_direction += delta[i] * b[i]
return descend_direction return descent_direction
class InformationStore(object): class InformationStore(object):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment