pyopmnearwell.ml.analysis module

Analyze the sensitivity of a neural network to its inputs.

Inspiration taken from https://f0nzie.github.io/machine_learning_compilation/sensitivity-analysis-for-a-neural-network.html

pyopmnearwell.ml.analysis.plot_analysis(outputs: ndarray, inputs: ndarray, savepath: str | Path, feature_names: list[str] | None = None, main_plot: tuple[ndarray, ndarray] | None = None, **kwargs) None

Plot the analysis of the model outputs against inputs.

Args:

outputs (np.ndarray): The model outputs. inputs (np.ndarray): The model inputs. Shape is (num_inputs, resolution_1, resolution_2). savepath (str | pathlib.Path): The path to save the plot. feature_names (Optional[list[str]]): The names of the input features. If None, default names (\(x_1,x_2,\dots\)) will be used. Default is None. main_plot (Optional[tuple[np.ndarray, np.ndarray]]): Add output and input for a

main plot that is specifically highlighted. Default is None.

**kwargs:
  • legend (bool): Whether to plot a legend. Default is True.

Returns:

None

pyopmnearwell.ml.analysis.sensitivity_analysis(model: Model, resolution_1: int = 20, resolution_2: int = 20, mode: Literal['homogeneous', 'random_uniform', 'random_normal'] | float = 'homogeneous') tuple[ndarray, ndarray]

Perform a sensitivity analysis of a neural network.

For each input variable, vary from a min to a max value and measure how the output of the network changes. The other input variables are kept constant meanwhile.

Note: It is assumed that the network has a single output.

Args:

model (keras.Model): Neural network. resolution_1 (int): Number of different values that the fixed inputs take.

Equals the number of plotted lines in plot_analysis.

resolution_2 (int): Number of different values the variable input takes. Equals

the number of datapoints along the x-axis plot_analysis.

mode (Literal[“homogeneous”, “random_uniform”, “random_normal”] | float): Sets

the sampling mode for the fixed inputs. - “homogeneous”: - “random_uniform”: - “random_normal” - float: All fixed inputs are set to this value. Default is “homogeneous”.

Returns:

tuple[np.ndarray, np.ndarray]: Output and inputs from the sensitivity analysis. First axis is the input variable that is varying, second axis is the variation, third axis is the variation for the fixed variables. The input array contains an additional axis in case of input dimension > 1.