SimpleLoss#

class zfit.loss.SimpleLoss(func, params=None, errordef=None, *, gradient=None, hessian=None, jit=None, deps=None, dependents=None)[source]#

Bases: BaseLoss

Loss from a (function returning a) Tensor. (deprecated arguments)

Deprecated: SOME ARGUMENTS ARE DEPRECATED: (deps). They will be removed in a future version. Instructions for updating: Use params instead.

This allows for a very generic loss function as the functions only restriction is that is should depend on zfit.Parameter.

Parameters:
  • func (Callable) – Callable that evaluates the loss and takes the parameter values as a single, array-like argument

  • params (Iterable[Parameter] | None) – The parameters (independent zfit.Parameter) of the loss. Essentially the (free) parameters that the func depends on.

  • errordef (float | None) – 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.

  • gradient (Callable | str | None) – Function that calculates the gradient of the loss with respect to the parameters. If not given, the gradient will be calculated automatically. Can be a string to determine the method, either ‘num’ (default) to calculate the gradient numerically, or ‘zfit’ to use automatic differentiation. The latter requires that the whole function is written using znp or tf operations

  • hessian (Callable | str | None) – Function that calculates the hessian of the loss with respect to the parameters. If not given, the hessian will be calculated automatically.C an be a string to determine the method, either ‘num’ (default) to calculate the hessian numerically, or ‘zfit’ to use automatic differentiation. The latter requires that the whole function is written using znp or tf operations

  • jit (Optional[bool]) –

    If true, jit compiles the different functions. Only use if function is fully built using znp or tf operations and no Python conditionals that depend on the concrete value of inputs.

    Defaults to False.

Usage:

import zfit
import zfit.z.numpy as znp
import tensorflow as tf


param1 = zfit.Parameter('param1', 5, 1, 10)
# we can build a model here if we want, but in principle, it's not necessary

x = znp.random.uniform(size=(100,))
y = x * tf.random.normal(mean=4, stddev=0.1, shape=x.shape, dtype=znp.float64)


def squared_loss(params):
    param1 = params[0]
    y_pred = x*param1  # this is very simple, but we can of course use any
    # zfit PDF or Func inside
    squared = (y_pred - y)**2
    mse = znp.mean(squared)
    return mse


loss = zfit.loss.SimpleLoss(squared_loss, param1, errordef=1)

which can then be used in combination with any zfit minimizer such as Minuit

minimizer = zfit.minimize.Minuit()
result = minimizer.minimize(loss)
__call__(_x=None)#

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 wrap value() 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.

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.

add_constraints(constraints)#

DEPRECATED FUNCTION

Deprecated: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use create_new instead and fill the constraints there.

property dtype: DType#

The dtype of the object.

get_params(floating=True, is_yield=None, extract_independent=True, *, autograd=None)#

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 (bool | None) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (bool | None) – 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 (bool | None) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type:

set[ZfitParameter]

gradient(params=None, *, numgrad=None, paramvals=None)#

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.

  • 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). Default will fall back to what the loss is set to.​

  • paramvals (TypeVar(ParamTypeInput, zfit.core.interfaces.ZfitParameter, Union[int, float, complex, Tensor, zfit.core.interfaces.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that is returned by get_params(). If no params are given, the current default values of the parameters are used.​

Return type:

Tensor

Returns:

The gradient of the loss with respect to the given parameters.

hessian(params=None, hessian=None, *, numgrad=None, paramvals=None)#

Calculate the hessian of the loss with respect to the given parameters.

Return type:

Tensor

Args: params: The parameters with respect to which the hessian is calculated. If None, all 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). Default will fall back to what the loss is set to.​

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.

value(*, params=None, full=None)#

Calculate the loss value 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])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that is returned by get_params(). If no params are given, the current default values of the parameters are used.​

  • full (bool | None) – ​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.

value_gradient(params=None, *, full=None, numgrad=None, paramvals=None)#

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 | None) – ​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.​

  • numgrad (bool | None) – ​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). Default will fall back to what the loss is set to.​

  • paramvals (TypeVar(ParamTypeInput, zfit.core.interfaces.ZfitParameter, Union[int, float, complex, Tensor, zfit.core.interfaces.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that is returned by get_params(). If no params are given, the current default values of the parameters are used.​

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, full=None, numgrad=None, paramvals=None)#

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’.

  • full (bool | None) – ​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.​

  • 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). Default will fall back to what the loss is set to.​

  • paramvals (TypeVar(ParamTypeInput, zfit.core.interfaces.ZfitParameter, Union[int, float, complex, Tensor, zfit.core.interfaces.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that is returned by get_params(). If no params are given, the current default values of the parameters are used.​

Return type:

tuple[Tensor, Tensor, Tensor]

Returns:

Calculated loss value as a scalar, the gradient as a tensor and the hessian as a tensor.