loss

class zfit.loss.ExtendedUnbinnedNLL(model, data, fit_range=None, constraints=None)[source]

Bases: zfit.core.loss.UnbinnedNLL

An Unbinned Negative Log Likelihood with an additional poisson term for the

add_cache_deps(cache_deps: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]], allow_non_cachable: bool = True)

Add dependencies that render the cache invalid if they change.

Parameters:
  • cache_deps (ZfitGraphCachable) –
  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.
Raises:

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
constraints
copy(deep: bool = False, name: str = None, **overwrite_params) → zfit.core.interfaces.ZfitObject
data
dtype

The dtype of the object

errordef
fit_range
get_cache_deps(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

Return a set of all independent Parameter that this object depends on.

Parameters:only_floating (bool) – If True, only return floating Parameter
get_dependencies(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

DEPRECATED FUNCTION

Warning: 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).

get_params(floating: Optional[bool] = True, is_yield: Optional[bool] = None, extract_independent: Optional[bool] = True, only_floating=<class 'zfit.util.checks.NotSpecified'>) → Set[zfit.core.interfaces.ZfitParameter]

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.
  • 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.
Parameters:
  • floating – if a parameter is floating, e.g. if floating() returns True
  • is_yield – 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 – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.
gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor] = None) → List[tensorflow.python.framework.ops.Tensor]
graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
model
name

The name of the object.

params
register_cacher(cacher: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]])

Register a cacher that caches values produces by this instance; a dependent.

Parameters:() (cacher) –
reset_cache(reseter: zfit.util.cache.ZfitGraphCachable)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor]) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
value_gradients_hessian(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor], hessian=None) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
class zfit.loss.UnbinnedNLL(model, data, fit_range=None, constraints=None)[source]

Bases: zfit.core.loss.BaseLoss

The Unbinned Negative Log Likelihood.

add_cache_deps(cache_deps: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]], allow_non_cachable: bool = True)

Add dependencies that render the cache invalid if they change.

Parameters:
  • cache_deps (ZfitGraphCachable) –
  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.
Raises:

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
constraints
copy(deep: bool = False, name: str = None, **overwrite_params) → zfit.core.interfaces.ZfitObject
data
dtype

The dtype of the object

errordef
fit_range
get_cache_deps(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

Return a set of all independent Parameter that this object depends on.

Parameters:only_floating (bool) – If True, only return floating Parameter
get_dependencies(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

DEPRECATED FUNCTION

Warning: 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).

get_params(floating: Optional[bool] = True, is_yield: Optional[bool] = None, extract_independent: Optional[bool] = True, only_floating=<class 'zfit.util.checks.NotSpecified'>) → Set[zfit.core.interfaces.ZfitParameter]

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.
  • 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.
Parameters:
  • floating – if a parameter is floating, e.g. if floating() returns True
  • is_yield – 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 – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.
gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor] = None) → List[tensorflow.python.framework.ops.Tensor]
graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
model
name

The name of the object.

params
register_cacher(cacher: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]])

Register a cacher that caches values produces by this instance; a dependent.

Parameters:() (cacher) –
reset_cache(reseter: zfit.util.cache.ZfitGraphCachable)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor]) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
value_gradients_hessian(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor], hessian=None) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
class zfit.loss.BaseLoss(model: Union[zfit.core.interfaces.ZfitModel, Iterable[zfit.core.interfaces.ZfitModel]], data: Union[zfit.Data, Iterable[zfit.Data]], fit_range: Union[zfit.core.interfaces.ZfitLimit, tensorflow.python.framework.ops.Tensor, numpy.ndarray, Iterable[float], float, Tuple[float], List[float], bool, None] = None, constraints: Union[Iterable[Union[zfit.core.interfaces.ZfitConstraint, Callable]], zfit.core.interfaces.ZfitConstraint, Callable, None] = None)[source]

Bases: zfit.core.interfaces.ZfitLoss, zfit.core.baseobject.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 (Iterable[ZfitModel]) – The model or models to evaluate the data on
  • data (Iterable[ZfitData]) – Data to use
  • fit_range (Iterable[Space]) – 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 (Iterable[tf.Tensor) – A Tensor representing a loss constraint. Using zfit.constraint.* allows for easy use of predefined constraints.
add_cache_deps(cache_deps: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]], allow_non_cachable: bool = True)

Add dependencies that render the cache invalid if they change.

Parameters:
  • cache_deps (ZfitGraphCachable) –
  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.
Raises:

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)[source]
constraints
copy(deep: bool = False, name: str = None, **overwrite_params) → zfit.core.interfaces.ZfitObject
data
dtype

The dtype of the object

errordef
fit_range
get_cache_deps(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

Return a set of all independent Parameter that this object depends on.

Parameters:only_floating (bool) – If True, only return floating Parameter
get_dependencies(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

DEPRECATED FUNCTION

Warning: 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).

get_params(floating: Optional[bool] = True, is_yield: Optional[bool] = None, extract_independent: Optional[bool] = True, only_floating=<class 'zfit.util.checks.NotSpecified'>) → Set[zfit.core.interfaces.ZfitParameter]

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.
  • 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.
Parameters:
  • floating – if a parameter is floating, e.g. if floating() returns True
  • is_yield – 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 – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.
gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor] = None) → List[tensorflow.python.framework.ops.Tensor][source]
graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
model
name

The name of the object.

params
register_cacher(cacher: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]])

Register a cacher that caches values produces by this instance; a dependent.

Parameters:() (cacher) –
reset_cache(reseter: zfit.util.cache.ZfitGraphCachable)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()[source]
value_gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor]) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor][source]
value_gradients_hessian(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor], hessian=None) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor][source]
class zfit.loss.SimpleLoss(func: Callable, deps: Iterable[zfit.Parameter] = <zfit.util.checks.NotSpecified object>, dependents: Iterable[zfit.Parameter] = <zfit.util.checks.NotSpecified object>, errordef: Optional[float] = None)[source]

Bases: zfit.core.loss.BaseLoss

Loss from a (function returning a) Tensor.

Parameters:
  • func – Callable that constructs the loss and returns a tensor.
  • deps – The dependents (independent zfit.Parameter) of the loss. If not given, the dependents are figured out automatically.
  • errordef – Definition of which change in the loss corresponds to a change of 1 sigma. For example, 1 for Chi squared, 0.5 for negative log-likelihood.
add_cache_deps(cache_deps: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]], allow_non_cachable: bool = True)

Add dependencies that render the cache invalid if they change.

Parameters:
  • cache_deps (ZfitGraphCachable) –
  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.
Raises:

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
constraints
copy(deep: bool = False, name: str = None, **overwrite_params) → zfit.core.interfaces.ZfitObject
data
dtype

The dtype of the object

errordef
fit_range
get_cache_deps(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

Return a set of all independent Parameter that this object depends on.

Parameters:only_floating (bool) – If True, only return floating Parameter
get_dependencies(only_floating: bool = True) -> OrderedSet(['z', 'f', 'i', 't', '.', 'P', 'a', 'r', 'm', 'e'])

DEPRECATED FUNCTION

Warning: 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).

get_params(floating: Optional[bool] = True, is_yield: Optional[bool] = None, extract_independent: Optional[bool] = True, only_floating=<class 'zfit.util.checks.NotSpecified'>) → Set[zfit.core.interfaces.ZfitParameter]

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.
  • 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.
Parameters:
  • floating – if a parameter is floating, e.g. if floating() returns True
  • is_yield – 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 – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.
gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor] = None) → List[tensorflow.python.framework.ops.Tensor]
graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
model
name

The name of the object.

params
register_cacher(cacher: Union[zfit.core.interfaces.ZfitCachable, Iterable[zfit.core.interfaces.ZfitCachable]])

Register a cacher that caches values produces by this instance; a dependent.

Parameters:() (cacher) –
reset_cache(reseter: zfit.util.cache.ZfitGraphCachable)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor]) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
value_gradients_hessian(params: Union[zfit.core.interfaces.ZfitParameter, int, float, complex, tensorflow.python.framework.ops.Tensor], hessian=None) → Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor]
zfit.loss.experimental_enable_loss_penalty(enable=True)[source]

EXPERIMENTAL! Enable a loss penalty if the loss is NaN, which can push back the minimizer.

Won’t work with every minimizer

Parameters:enable – If True, enable this feature.