fitter
Abstraction for the calibration kernel.
This module contain the main class Fitter which is the code of Tunax
for the calibration of the closure parts. The classes
FittableParameter and FittableParametersSet are used to make
a link between the optimization part and the closures structures. These class
can be obtained by the prefix tunax.fitter. or directly by
tunax..
- class FittableParameter(do_fit, val=0.0)[source]
Calibration configuration for one parameter.
An instance of this class must be created for every parameter of the closure that will be calibrated and for every not default values during calibration.
- class FittableParametersSet(coef_fit_dict, closure_name)[source]
Complete closure calibration parameters.
This class is the set of all the configurations on the closure parameters for the calibration. It makes the link between the array on which the optimizer works and the closure parameters class.
- Parameters:
coef_fit_dict (Dict[str, FittableParameter]) – cf. attribute.
closure_name (str) – Name of the chosen closure, must be a key of
CLOSURES_REGISTRY, see its documentation for the available closures.
- Return type:
- coef_fit_dico
The set of all the configurations of all the parameters that will be calibrated and the one constants but not with the default value of the closure.
- Type:
Dict[str, FittableParameter]
- property n_calib: int
Number of variables that are calibrated.
- Returns:
nc – Number of variables that are calibrated.
- Return type:
- fit_to_closure(x)[source]
Transforms an fitted array in a set of closure parameters.
This method copy the fixed non-default values of
coef_fit_dictand copy the calibrated values fromx. Which is simply the parameters values in the order that is indicated bycoef_fit_dict.- Parameters:
x (Float[Array, 'nc']) – The array on which the optimize works to find the best values. It is the array of the parameters that are calibrated.
- Returns:
clo_params – The instance of the closure parameters class (child class of
ClosureParametersAbstract) with the modifications of the calibration step.- Return type:
- gen_init_val()[source]
Produce the fitted array for the first calibration step.
This method simply copy the initial values of the calibrated coefficients in an array
xwhich will be used as the first calibration step for the optimizer.- Returns:
x – The initial vector for the optimizer at the first step of calibration.
- Return type:
Float[Array, ‘nc’]
- class Fitter(coef_fit_params, database, dt, loss, nloop=100, nit_loss=-1, learning_rate=0.001, verbatim=True, output_path='./')[source]
Reprensentation of a complete calibration configuration.
A fitter is a link between the calibrated parameters configuration
coef_fit_params, adatabaseof observations to fit the model on, a loss functionlossand some optimizer parameters. An instance can be call (with no parameters) to run the calibration. The__init__method build a set of models corresponding to the given time-stepdt, the given closureclosure_nameand the different initial states and physical cases extracted from thedatabase.- Parameters:
coef_fit_params (FittableParametersSet) – cf. attribute.
database (Database) – cf. attribute.
dt (float) – The time-step used for defininf the forward model that will be calibrated \([\text s]\).
loss (Callable[[List[Trajectory], Database], float]) – cf. attribute.
nloop (int, default=100) – cf. attribute.
nit_loss (int, default = -1) – cf. attribute.
learning_rate (float, default=0.001) – cf. attribute.
verbatim (bool, default = True) – cf. attribute.
output_path (Optional[str], default = '.') – cf. attribute.
- Return type:
- coef_fit_params
Parametrization of the closure parameters that must be calibrated and the one which are fixed with non-default values.
- Type:
- database
Database of observation used for calibration, the optimizer will make the model fit to them.
- Type:
- model_list
List of model instances that represent the physical case and the initial condition for every observation in the database. At each calibration step, they will be call to compute the loss function.
- Type:
List[SingleColumnModel]
- loss
Abstraction that the user create to describe its own loss function which will be minimized by the fitter. The function should represent how much the model with its closure fits to the
database- Parameters:
trajectories (List[Trajectory]) – List of trajectories computed by the model and corresponding to each observation of the
databasecase in the same order.database (Database) – cf.parameter
- Returns:
loss – The quantity that will be minimized by the fitter. The user must compute a quantity that compares the
trajectoriesdone by the forward model (with the current values for the parameters of the closure at the current calibration step), and the trajectories from thedatabase.- Return type:
float, positive
- Type:
Callable[[List[Trajectory], Database], float]
- nit_loss
Number of iterations of every computation of the loss function in the output for diagnostic. Set to -1 to never compute it. Useless if
output_pathisNone.- Type:
int, default = -1
- learning_rate
Learning rate of the optimizer algorithm : how much it is fast at each step.
- Type:
float, default=0.001
- output_path
If
None, don’t write the evolution on numpy files ; else must finishes by.npz: in this file the compressed numpy output will be written.- Type:
Optional[str], default = ‘.’
Note
During the calibration the gradient of the loss function is computed at every iterations, but not the loss function itself. The set of
nit_lossincrease the cost of the iteration if the value of the cost funtion is computed too often.The output is written at every step so its readable by another python kernel during the calibration. To access to this data, one have to read the
.npzfile with thenumpy.load()function (withallow_pickleset oneTrue), and then access to the evolutions of the calibrated vector (the closures paramters that are calibrated) with['x'], to their gradients with['grads']these are 2 dimensional arrays, the first dimension being the calibration iterations and the second one the parameters list. Ifnit_lossis not equal to -1, one can access to the evolution of the loss function with['loss_it']which records the indexes of the iterations where the loss function is computed and['loss_values']the corresponding values of the loss function.
- loss_wrapped(x)[source]
Wrapping of
lossthat takes only an array in argument.This method runs every model for each observations with the set of closure parameters corresponding to x, then it computes and returns the the value of the loss function.
- __call__()[source]
Execute the callibration.
First the optimizer is selected and parametrized with optax and the gradient function of the loss is computed. Then in the calibration loop, the gradient is evaluated on the currents values of the closure parameters, the eventual output are computed and the optax optimizer is updated. The optmizer used is
optax.adam().- Returns:
closure_params – The instance of the closure parameters changed with the final value of the calibrated parameters.
- Return type: