Skip to content

Commit

Permalink
Merge pull request #273 from tBuLi/fix_271
Browse files Browse the repository at this point in the history
Fixed #271 by lazily creating Hessian and Jacobian models
  • Loading branch information
tBuLi committed Oct 7, 2019
2 parents efabe2d + af6897e commit f6aa382
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions symfit/core/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,14 @@ def _init_minimizer(self, minimizer, **minimizer_options):
# that, otherwise we let the minimizer estimate it itself.
# Hence the check of jacobian_model, as this is the
# py function version of the analytical jacobian.
if hasattr(self.model, 'jacobian_model') and hasattr(self.objective, 'eval_jacobian'):
if hasattr(self.model, 'eval_jacobian') and hasattr(self.objective, 'eval_jacobian'):
minimizer_options['jacobian'] = self.objective.eval_jacobian
if issubclass(minimizer, HessianMinimizer):
# If an analytical version of the Hessian exists we should use
# that, otherwise we let the minimizer estimate it itself.
# Hence the check of hessian_model, as this is the
# py function version of the analytical hessian.
if hasattr(self.model, 'hessian_model') and hasattr(self.objective, 'eval_hessian'):
if hasattr(self.model, 'eval_hessian') and hasattr(self.objective, 'eval_hessian'):
minimizer_options['hessian'] = self.objective.eval_hessian

if issubclass(minimizer, ConstrainedMinimizer):
Expand Down
2 changes: 1 addition & 1 deletion symfit/core/fit_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def stdev(self, param):
"""
try:
return np.sqrt(self.variance(param))
except AttributeError:
except (AttributeError, TypeError):
# This happens when variance returns None.
return None

Expand Down
33 changes: 15 additions & 18 deletions symfit/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,14 @@ def numerical_components(self):
'`numerical_components`.').format(self.__class__)
)

def _get_params(self):
@property
def params(self):
return self._params

def _set_params(self, value):
@params.setter
def params(self, value):
self._params = value
self.__signature__ = self._make_signature()
params = property(_get_params, _set_params) # Properties cannot use `super`

def _make_signature(self):
# Handle args and kwargs according to the allowed names.
Expand Down Expand Up @@ -789,14 +790,12 @@ class GradientModel(CallableModel, BaseGradientModel):
"""
def __init__(self, *args, **kwargs):
super(GradientModel, self).__init__(*args, **kwargs)
self.jacobian_model = jacobian_from_model(self)

def _set_params(self, value):
super(GradientModel, self)._set_params(value)
if hasattr(self, 'jacobian_model'):
self.jacobian_model.params = value
# Properties cannot use `super` unless when used in this way
params = property(CallableModel._get_params, _set_params)
@cached_property
def jacobian_model(self):
jac_model = jacobian_from_model(self)
jac_model.params = self.params
return jac_model

@cached_property
def jacobian(self):
Expand Down Expand Up @@ -843,14 +842,12 @@ class HessianModel(GradientModel):
"""
def __init__(self, *args, **kwargs):
super(HessianModel, self).__init__(*args, **kwargs)
self.hessian_model = hessian_from_model(self)

def _set_params(self, value):
super(HessianModel, self)._set_params(value)
if hasattr(self, 'hessian_model'):
self.hessian_model.params = value
# Properties cannot use `super` unless when used in this way
params = property(GradientModel._get_params, _set_params)

@cached_property
def hessian_model(self):
hess_model = hessian_from_model(self)
hess_model.params = self.params
return hess_model

@property
def hessian(self):
Expand Down

0 comments on commit f6aa382

Please sign in to comment.