hnn_core.optimization.Optimizer#
- class hnn_core.optimization.Optimizer(initial_net, tstop, constraints, set_params, initial_params=None, solver='bayesian', obj_fun='dipole_rmse', max_iter=200)[source]#
Parameter optimization.
- Parameters:
- initial_netinstance of Network
The network object.
- tstopfloat
The simulated dipole’s duration.
- constraintsdict
The user-defined constraints.
- set_paramsfunc
User-defined function that sets parameters in network drives.
set_params(net, params) -> Nonewhere
netis a Network object andparamsis a dictionary of the parameters that will be set inside the function.- initial_paramsdict, optional
Initial parameters for the objective function. Keys are parameter names, values are initial parameters. The default is None. If None, the parameters will be set to the midpoints of parameter ranges.
- solverstr
The optimizer, ‘bayesian’, ‘cobyla’, or ‘cma’.
- obj_funstr | func
The objective function to be minimized. Can be ‘dipole_rmse’, ‘maximize_psd’, ‘dipole_corr’, ‘custom’, or a user-defined function. See the docstring for
Optimizer.fitto view the required and optional arguments for each of these cases. If a user-defined function is provided, it must have the same function signature as the existing objective functions (i.e. _rmse_evoked and _maximize_psd in objective_functions.py).The default is ‘dipole_rmse’.- max_iterint, optional
The max number of calls to the objective function. The default is 200.
- Attributes:
- constraintsdict
The user-defined constraints.
- initial_paramsdict, None
Initial parameters for the objective function. If None, initial_params is set to the midpoint of upper/lower bounds defined in constraints.
- max_iterint
The max number of calls to the objective function.
- solverfunc
The optimization function.
- obj_funfunc
The objective function to be minimized.
- obj_fun_namestr
The name of the template objective function.
- tstopfloat
The simulated dipole’s duration.
- net_instance of Network
The network object with optimized drives.
- obj_list
The objective function values.
- opt_params_list
The list of optimized parameter values.
Methods
fit(**obj_fun_kwargs)Runs optimization routine.
plot_convergence([ax, show])Convergence plot.
- fit(**obj_fun_kwargs)[source]#
Runs optimization routine.
- Parameters:
- targetinstance of Dipole (Required if obj_fun=’dipole_corr’, ‘dipole_rmse’, or ‘dipole_rmse_corr’)
A dipole object with experimental data.
- n_trialsint (Optional if obj_fun=’dipole_corr’, ‘dipole_rmse’, or ‘dipole_rmse_corr’)
Number of trials to simulate and average.
- tstartfloat (Optional if obj_fun=’dipole_corr’, ‘dipole_rmse’, or ‘dipole_rmse_corr’)
Time at beginning of range over which to calculate the objective function
- weightsarray (Optional if obj_fun=’dipole_corr’, ‘dipole_rmse’, or ‘dipole_rmse_corr’)
An array of weights to be applied to each point in simulated dpl. Must have length >= dpl.data . If None, weights will be replaced with 1’s for typical objective function calculation.
- f_bandslist of tuples (Required if obj_fun=’maximize_psd’)
Lower and higher limit for each frequency band in Hz.
- relative_bandpowerlist of float | float (Required if obj_fun=’maximize_psd’)
Weight for each frequency band in f_bands. If a single float is provided, the same weight is applied to all frequency bands.
- loss_funcallable (Required if obj_fun=’custom’)
Objective loss function to be provided by user. The function must accept a single
Dipoleobject as its first argument, and obj_fun_kwargs as its second argument, where obj_fun_kwargs is the same as the kwargs passed to this ``Optimizer.fit` function.- sigma0float| array-like (Only used if solver=’cma’)
Initial standard deviation of CME-ES algorithm. If float, sigma0 is scaled by bounds defined in the constraints for each parameter. If array-like, The length of sigma0 must equal the length of constraints. Default: 0.25
- popsizeint (Only used if solver=’cma’)
Number of parameter samples simulated per epoch. Default: 16
- n_jobsint (Only used if solver=’cma’)
The number of jobs to start in parallel. If None, then 1 trial will be started without parallelism.
- seedint, optional (Only used if solver=’cma’)
Optional seed for random number generator of optimizer.
- tolfunfloat (Only used if solver=’cma’)
Termination criteria. Stops if the range of the best objective function values of the last 10 + ((30 * n_parameters) / popsize) generations and all function values of the recent generation is below tolfun. Default 0.01.
- dtfloat, default=0.025
The integration time step of h.CVode (in ms)
- scale_factorfloat, optional
The dipole scale factor to use after every optimization iteration before data comparison. There is no scaling applied by default, so you must pass a value if you want any scaling.
- baseline_correctionbool, default=True
Whether to apply the baseline correction to the simulated dipole after every optimization iteration (which correction is used depends on
Network._model_variant). Defaults to True, applying the appropriate correction.- smooth_window_lenfloat, optional
The smooth window length to use after every optimization iteration before data comparison. There is no smoothing applied by default, so you must pass a value if you want any smoothing.
- verbosebool, default=True
If True, print build steps and simulation progress to console.
Notes
When defining sigma0 for CMA-ES as a float, the sigma0 applied to each parameter is calculated as sigma0 * (upper_bound - lower_bound) based on the constraints. It is recommended to choose a sigma0 such that the optimum is expected to lie within about initial_params +- 3*sigma0. A smaller sigma0 searches closer to initial_params.
When defining popsize for CMA-ES, it is recommended to increase popsize relative to the number of parameters being optimized (N). 4+3*log(N)