class zfit.loss.SimpleLoss(func, params=None, errordef=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.

  • func – Callable that constructs the loss and returns a tensor without taking an argument.

  • params – The dependents (independent zfit.Parameter) of the loss. Essentially the (free) parameters that the func depends on.

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


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)

Calculate the loss value with the given input for the free 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.

Return type



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.

  • cache_deps (Union[zfit.core.interfaces.ZfitGraphCachable, Iterable[zfit.core.interfaces.ZfitGraphCachable]]) –

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


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.

Return type



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


only_floating (bool) – If True, only return floating Parameter

Return type




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


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.

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

  • 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



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


cacher (Union[zfit.core.interfaces.ZfitGraphCachable, Iterable[zfit.core.interfaces.ZfitGraphCachable]]) –


Clear the cache of self and all dependent cachers.