BaseLoss#
- class zfit.loss.BaseLoss(model, data, fit_range=None, constraints=None, options=None)[source]#
Bases:
ZfitLoss
,BaseNumeric
A “simultaneous fit” can be performed by giving one or more
model
,data
,fit_range
to the loss. The length of each has to match the length of the others.- Parameters:
model (
Union
[ZfitModel
,Iterable
[ZfitModel
]]) – The model or models to evaluate the data onfit_range (
Union
[ZfitLimit
,Tensor
,ndarray
,Iterable
[float
],float
,Tuple
[float
],List
[float
],bool
,None
]) – The fitting range. It’s the norm_range for the models (if they have a norm_range) and the data_range for the data.constraints (
Union
[Iterable
[Union
[ZfitConstraint
,Callable
]],ZfitConstraint
,Callable
,None
]) – A Tensor representing a loss constraint. Usingzfit.constraint.*
allows for easy use of predefined constraints.options (
Optional
[Mapping
]) – Different options for the loss calculation.
- __call__(_x=None)[source]#
Calculate the loss value with the given input for the free parameters.
- Parameters:
*positional* – Array-like argument to set the parameters. The order of the values correspond to the position of the parameters in
get_params()
(called without any arguments). For more detailed control, it is always possible to wrapvalue()
and set the desired parameters manually.full – If True, return the full loss value, otherwise allow for the removal of constants and only return the part that depends on the parameters. Constants don’t matter for the task of optimization, but they can greatly help with the numerical stability of the loss function.
- Return type:
array
- Returns:
Calculated loss value as a scalar.
- value(*, full=None)[source]#
Calculate the loss value with the current values of the free parameters.
- Parameters:
full (
bool
) – If True, return the full loss value, otherwise allow for the removal of constants and only return the part that depends on the parameters. Constants don’t matter for the task of optimization, but they can greatly help with the numerical stability of the loss function.- Return type:
Tensor
- Returns:
Calculated loss value as a scalar.
- gradient(params=None)[source]#
Calculate the gradient of the loss with respect to the given parameters.
- Parameters:
params (
TypeVar
(ParamTypeInput
, zfit.core.interfaces.ZfitParameter,Union
[int
,float
,complex
,Tensor
, zfit.core.interfaces.ZfitParameter])) – The parameters with respect to which the gradient is calculated. If None, all parameters are used.- Return type:
list
[Tensor
]- Returns:
The gradient of the loss with respect to the given parameters.
- value_gradient(params=None, *, full=None)[source]#
Calculate the loss value and the gradient with the current values of the free parameters.
- Parameters:
params (
TypeVar
(ParamTypeInput
, zfit.core.interfaces.ZfitParameter,Union
[int
,float
,complex
,Tensor
, zfit.core.interfaces.ZfitParameter])) – The parameters to calculate the gradient for. If not given, all free parameters are used.full (
bool
) – If True, return the full loss value, otherwise allow for the removal of constants and only return the part that depends on the parameters. Constants don’t matter for the task of optimization, but they can greatly help with the numerical stability of the loss function.
- Return type:
tuple
[Tensor
,Tensor
]- Returns:
Calculated loss value as a scalar and the gradient as a tensor.
- value_gradient_hessian(params=None, hessian=None, numgrad=None, *, full=None)[source]#
Calculate the loss value, the gradient and the hessian with the current values of the free parameters.
- Parameters:
params (
TypeVar
(ParamTypeInput
, zfit.core.interfaces.ZfitParameter,Union
[int
,float
,complex
,Tensor
, zfit.core.interfaces.ZfitParameter])) – The parameters to calculate the gradient for. If not given, all free parameters are used.hessian – Can be ‘full’ or ‘diag’.
numgrad – If
True
, calculate the numerical gradient/Hessian instead of using the automatic one. This is usually slower if called repeatedly but can be used if the automatic gradient fails (e.g. if the model is not differentiable, written not in znp.* etc).full (
bool
) – If True, return the full loss value, otherwise allow for the removal of constants and only return the part that depends on the parameters. Constants don’t matter for the task of optimization, but they can greatly help with the numerical stability of the loss function.
- Return type:
tuple
[Tensor
,Tensor
,Tensor
]- Returns:
Calculated loss value as a scalar, the gradient as a tensor and the hessian as a tensor.
- add_cache_deps(cache_deps, allow_non_cachable=True)#
Add dependencies that render the cache invalid if they change.
- Parameters:
cache_deps (ztyping.CacherOrCachersType) –
allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitGraphCachable will raise an error.
- Raises:
TypeError – if one of the cache_dependents is not a ZfitGraphCachable _and_ allow_non_cachable if False.
- property dtype: DType#
The dtype of the object.
- get_cache_deps(only_floating=True)#
Return a set of all independent
Parameter
that this object depends on.- Parameters:
only_floating (
bool
) – IfTrue
, only return floatingParameter
- Return type:
OrderedSet
- get_dependencies(only_floating: bool = True) ztyping.DependentsType #
DEPRECATED FUNCTION
Deprecated: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use get_params instead if you want to retrieve the independent parameters or get_cache_deps in case you need the numerical cache dependents (advanced).
- Return type:
OrderedSet
- get_params(floating=True, is_yield=None, extract_independent=True, only_floating=<class 'zfit.util.checks.NotSpecified'>)#
Recursively collect parameters that this object depends on according to the filter criteria.
- Which parameters should be included can be steered using the arguments as a filter.
- None: do not filter on this. E.g.
floating=None
will return parameters that are floating as well as parameters that are fixed.
- None: do not filter on this. E.g.
True: only return parameters that fulfil this criterion
- False: only return parameters that do not fulfil this criterion. E.g.
floating=False
will return only parameters that are not floating.
- False: only return parameters that do not fulfil this criterion. E.g.
- Parameters:
floating (
Optional
[bool
]) – if a parameter is floating, e.g. iffloating()
returns Trueis_yield (
Optional
[bool
]) – if a parameter is a yield of the _current_ model. This won’t be applied recursively, but may include yields if they do also represent a parameter parametrizing the shape. So if the yield of the current model depends on other yields (or also non-yields), this will be included. If, however, just submodels depend on a yield (as their yield) and it is not correlated to the output of our model, they won’t be included.extract_independent (
Optional
[bool
]) – If the parameter is an independent parameter, i.e. if it is aZfitIndependentParameter
.
- Return type:
set
[ZfitParameter
]
- register_cacher(cacher)#
Register a cacher that caches values produces by this instance; a dependent.
- Parameters:
cacher (ztyping.CacherOrCachersType) –
- reset_cache_self()#
Clear the cache of self and all dependent cachers.