aihwkit.utils.fitting module

Fitting utilities.

This module includes fitting utilities for aihwkit. Using this module has extra dependencies that can be installed via the extras mechanism:

pip install aihwkit[fitting]
aihwkit.utils.fitting.fit_measurements(parameters, pulse_data, response_data, device_config, suppress_device_noise=True, max_pulses=1, n_traces=1, fit_weights=None, method='powell', verbose=False, **fit_kwargs)[source]

Fit pulse response measurement to the given device model using lmfit.

For example:

# responses are conductance data in response to pulses (-1, 1)

# choose device model and parameter to fit
device_config = SoftBoundsDevice(w_min=-1.0, w_max=1.0)
params = {'dw_min': (0.1, 0.001, 5.0),
          'up_down': (0.0, -0.99, 0.99),
          'w_max': (1.0, 0.1, 5.0)}

# fit the response
fit_res, fit_device_config, model_response = fit_measurements(
    params, pulses, responses,
    device_config=device_config,
    suppress_device_noise=True,
    method='powell',
    fit_weights=fit_weights,
)
# fit parameter
print(fit_res.params.valuesdict())
# device of best fit
print(fit_device_config)
Parameters:
  • parameters (Dict | Parameters) – Parameter to vary. Dictionary with parameter names (attributes of the device config). Each value is either a single value (thus only set, not varied) or a tuple (x_init, x_min, x_max). lmfit.Parameters class can also given directly.

  • pulse_data (Tuple[ndarray] | ndarray) – Pulse data, ie array of number of pulses in up (pos) or down (neg) direction. Can be a tuple of multiple measurements

  • response_data (Tuple[ndarray] | ndarray) –

    Corresponfing measured responses to the pulses given by pulse_data as numpy array or list.

    Caution

    axes=1 can be used for multiple device fit. However, then all pulse data needs to have the same axis=0 dimension

  • device_config (PulsedDevice | RPUConfigGeneric) – base device configuration

  • suppress_device_noise (bool) – sets all dtod and std parameters of the device to 0

  • n_traces (int) – how many traces to simulate simulaenously

  • max_pulses (int | None) – constrain the number of pulses given.

  • fit_weights (Tuple[int] | int | None) – the weightening of the individual response traces in the loss function

  • method (str) – fitting method from lmfit (default “powell”)

  • verbose (bool) – whether to print fitting results

  • fit_kwargs (Any) – additional parameter passed to lmfit.minimize

Returns:

Result of the fit in lmfit format device_config: Device config with found parameter applied model_response: Model response of parameter fit

Return type:

fit_results

Raises:

ArgumentError – in case wrong arguments are given

aihwkit.utils.fitting.model_response(params, pulse_data, response_data, rpu_config, n_traces=1, fit_weights=None, verbose=True, only_response=False)[source]

Compute the model respunses given the pulses.

Parameters:
  • params (Parameters) – lmfit.Parameters of the current parameter setting

  • pulse_data (Tuple[ndarray] | ndarray) – Pulse data, ie array of number of pulses in up (pos) or down (neg) direction. Can be a tuple of multiple measurements

  • response_data (Tuple[ndarray] | ndarray) –

    Corresponfing measured responses to the pulses given by pulse_data as numpy array or list.

    Caution

    axes=1 can be used for multiple device fit. However, then all pulse data needs to have the same axis=0 dimension

  • rpu_config (RPUConfigGeneric) – base device configuration (will be modified)

  • fit_weights (Tuple[int] | int | None) – the weightening of the individual response traces in the loss function

  • n_traces (int) – how many traces to simulate simulaenously

  • verbose (bool) – whether to print std of deviation

  • only_response (bool) – whether to returns a list of model response instead of the deviation

Returns:

deviation vector or list of model responses (weight traces)

Return type:

ndarray | List[ndarray]

Note

overwrites the given rpu_config