pybop.costs.base_cost#
Classes#
Base class for defining cost functions. |
Module Contents#
- class pybop.costs.base_cost.BaseCost(problem: pybop.BaseProblem | None = None)[source]#
Base class for defining cost functions.
This class is intended to be subclassed to create specific cost functions for evaluating model predictions against a set of data. The cost function quantifies the goodness-of-fit between the model predictions and the observed data, with a lower cost value indicating a better fit.
- Parameters:
problem (object) – A problem instance containing the data and functions necessary for evaluating the cost function.
target (array-like) – An array containing the target data to fit.
n_outputs (int) – The number of outputs in the model.
has_separable_problem (bool) – If True, the problem is separable from the cost function and will be evaluated in advance of the call to self.compute() (default: False).
_de (float) – The gradient of the cost function to use if an error occurs during evaluation. Defaults to 1.0.
- __call__(inputs: pybop.parameters.parameter.Inputs | list, calculate_grad: bool = False, apply_transform: bool = False)[source]#
This method calls the forward model via problem.evaluate(inputs), and computes the cost for the given output by calling self.compute().
- Parameters:
inputs (Inputs or array-like) – The parameters for which to compute the cost and gradient.
calculate_grad (bool, optional) – A bool condition designating whether to calculate the gradient.
- Returns:
The calculated cost function value.
- Return type:
float
- Raises:
ValueError – If an error occurs during the calculation of the cost.
- abstract compute(y: dict, dy: numpy.ndarray, calculate_grad: bool = False)[source]#
Compute the cost and if calculate_grad is True, its gradient with respect to the predictions.
This method only computes the cost, without calling the problem.evaluate(). This method must be implemented by subclasses.
- Parameters:
y (dict) – The dictionary of predictions with keys designating the signals for fitting.
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
calculate_grad (bool, optional) – A bool condition designating whether to calculate the gradient.
- Returns:
The calculated cost function value.
- Return type:
float
- Raises:
NotImplementedError – If the method has not been implemented by the subclass.
- join_parameters(parameters)[source]#
Setter for joining parameters. This method sets the fail gradient if the join adds parameters.
- set_fail_gradient(de: float = 1.0)[source]#
Set the fail gradient to a specified value.
The fail gradient is used if an error occurs during the calculation of the gradient. This method allows updating the default gradient value.
- Parameters:
de (float) – The new fail gradient value to be used.