Package: neuralsens
NeuralSensJaime Pizarroso Gonzalo,jpizarroso@comillas.eduAntonio Muñoz San Roque,Antonio.Munoz@iit.comillas.eduJosé Portela González,jose.portela@iit.comillas.eduThis is the development repository for theneuralsenspackage.  Functions within this package can be used for the analysis of neural network models created in Python.The last version of this package can be installed using pip:$pipinstallneuralsensIt can also be installed with conda:$condainstall-cjaipizgonneuralsensBug reportsPlease submit any bug reports (or suggestions) using theissuestab of the GitHub page.FunctionsOne function is available to analyze the sensitivity of a multilayer perceptron, evaluating variable importance and plotting the analysis results.# Import necessary packages to reproduce the exampleimportneuralsens.partial_derivativesasnsfromneuralsens.daily_demand_trimportload_daily_data_demand_trimportpandasaspdfromsklearn.neural_networkimportMLPRegressor# Load data and scale variablesdaily_demand_tr=load_daily_data_demand_tr()X_train=daily_demand_tr[["WD","TEMP"]]X_train.iloc[:,1]=X_train.iloc[:,1]/10y_train=daily_demand_tr["DEM"]/100Thejacobian_mlpfunction analyze the sensitivity of the output to the input and  plots three graphics with information about this analysis. To calculate this sensitivity it calculates the partial derivatives of the output to the inputs using the training data.
The first plot shows information between the mean and the standard deviation of the sensitivity among the training data:if the mean is different from zero, it means that the output depends on the input because the output changes when the input change.if the mean is nearly zero, it means that the output could not depend on the input. If the standard deviation is also near zero it almost sure that the output does not depend on the variable because for all the training data the partial derivative is zero.if the standard deviation is different from zero it means the the output has a non-linear relation with the input because the partial derivative derivative of the output depends on the value of the input.if the standard deviation is nearly zero it means that the output has a linear relation with the input because the partial derivative of the output does not depend on the value of the input.
The second plot gives an absolute measure to the importance of the inputs, by calculating the sum of the squares of the partial derivatives of the output to the inputs.
The third plot is a density plot of the partial derivatives of the output to the inputs among the training data, giving similar information as the first plot.### Create MLP modelmodel=MLPRegressor(solver='sgd',# Update functionhidden_layer_sizes=[40],# #neurons in hidden layerslearning_rate_init=0.1,# initial learning rateactivation='logistic',# Logistic sigmoid activation functionalpha=0.005,# L2 regularization termlearning_rate='adaptive',# Type of learning rate used in trainingmax_iter=500,# Maximum number of iterationsbatch_size=10,# Size of batch when trainingrandom_state=150)# Train modelmodel.fit(X_train,y_train)# Obtain parameters to perform jacobianwts=model.coefs_bias=model.intercepts_actfunc=['identity',model.get_params()['activation'],model.out_activation_]X=pd.DataFrame(X_train,columns=["WD","TEMP"])y=pd.DataFrame(y_train,columns=["DEM"])sens_end_layer='last'sens_end_input=Falsesens_origin_layer=0sens_origin_input=True# Perform jacobian of the modeljacobian=ns.jacobian_mlp(wts,bias,actfunc,X,y)jacobian.plot("sens")Apart from the plot created with the"sens"argument by an internal call
tosensitivity_plots(), other plots can be obtained to analyze the neural
network model (although they are yet to be coded, thanks for your patience!).CitationPlease, to cite NeuralSens in publications use:Pizarroso J, Portela J, Muñoz A (2022). “NeuralSens: Sensitivity Analysis of Neural Networks.”Journal of
Statistical Software,102(7), 1-36. doi: 10.18637/jss.v102.i07 (URL:https://doi.org/10.18637/jss.v102.i07).LicenseThis package is released in the public domain under the General Public LicenseGPL.AssociationPackage created in the Institute for Research in Technology (IIT),link to homepage