neuralsens: sensitivity analysis of neural networks · for r (r core team(2017)) that was developed...

28
NeuralSens: Sensitivity Analysis of Neural Networks Jaime Pizarroso Universidad Pontificia Comillas José Portela Universidad Pontificia Comillas Antonio Muñoz Universidad Pontificia Comillas Abstract Neural networks are important tools for data-intensive analysis and are commonly applied to model non-linear relationships between dependent and independent variables. However, neural networks are usually seen as “black boxes” that offer minimal information about how the input variables are used to predict the response in a fitted model. This article describes the NeuralSens package that can be used to perform sensitivity analysis of neural networks using the partial derivatives method. Functions in the package can be used to obtain the sensitivities of the output with respect to the input variables, evaluate variable importance based on sensitivity measures and characterize relationships between input and output variables. Methods to calculate sensitivities are provided for objects from common neural network packages in R, including neuralnet, nnet, RSNNS, h2o, neural, forecast and caret. The article presents an overview of the techniques for obtaining information from neural network models, a theoretical foundation of how are calculated the partial derivatives of the output with respect to the inputs of a multi- layer perceptron model, a description of the package structure and functions, and applied examples to compare NeuralSens functions with analogous functions from other available R packages. Keywords : neural networks, sensitivity, analysis, variable importance, R, NeuralSens. 1. Introduction The application of data-intensive analysis allows to identify patterns or trends from unstruc- tured data, extracting useful information and supporting decision-making (Sun, Sun, and Strang (2018)). As the volume of available information increase in diverse fields, the number of situations in which this type of analysis can be applied grows simultaneously (Michener and Jones (2012), Kelling, Hochachka, Fink, Riedewald, Caruana, Ballard, and Hooker (2009)). Machine-learning algorithms are common methods used in data-intensive analysis (Hastie, Tibshirani, and Friedman (2001),Butler, Davies, Cartwright, Isayev, and Walsh (2018), Vu, AdalÄś, Ba, BuzsÃąki, Carlson, Heller, Liston, Rudin, Sohal, Widge, Mayberg, Sapiro, and Dzirasa (2018)), because they are able to detect emergent patterns in the data without di- rect human supervision. Artificial Neural Networks (ANN) are one of the most popular machine-learning algorithms. ANNs are designed to mimic biological neural structures of animal brains (Mcculloch and Pitts (1943)) by “learning” inherent data patterns through iterative algorithms (Rojas (1996)). The form of ANN that is discussed in this paper is the feed-forward multilayer perceptron (MLP) trained using the backpropagation algorithm (Rumelhart, Hintont, and Williams (1986)). MLPs is one of the most popular form of ANNs arXiv:2002.11423v1 [cs.LG] 26 Feb 2020

Upload: others

Post on 14-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

NeuralSens: Sensitivity Analysis of Neural Networks

Jaime PizarrosoUniversidad Pontificia

Comillas

José PortelaUniversidad Pontificia

Comillas

Antonio MuñozUniversidad Pontificia

Comillas

Abstract

Neural networks are important tools for data-intensive analysis and are commonlyapplied to model non-linear relationships between dependent and independent variables.However, neural networks are usually seen as “black boxes” that offer minimal informationabout how the input variables are used to predict the response in a fitted model. Thisarticle describes the NeuralSens package that can be used to perform sensitivity analysisof neural networks using the partial derivatives method. Functions in the package canbe used to obtain the sensitivities of the output with respect to the input variables,evaluate variable importance based on sensitivity measures and characterize relationshipsbetween input and output variables. Methods to calculate sensitivities are provided forobjects from common neural network packages in R, including neuralnet, nnet, RSNNS,h2o, neural, forecast and caret. The article presents an overview of the techniques forobtaining information from neural network models, a theoretical foundation of how arecalculated the partial derivatives of the output with respect to the inputs of a multi-layer perceptron model, a description of the package structure and functions, and appliedexamples to compare NeuralSens functions with analogous functions from other availableR packages.

Keywords: neural networks, sensitivity, analysis, variable importance, R, NeuralSens.

1. IntroductionThe application of data-intensive analysis allows to identify patterns or trends from unstruc-tured data, extracting useful information and supporting decision-making (Sun, Sun, andStrang (2018)). As the volume of available information increase in diverse fields, the numberof situations in which this type of analysis can be applied grows simultaneously (Michener andJones (2012), Kelling, Hochachka, Fink, Riedewald, Caruana, Ballard, and Hooker (2009)).Machine-learning algorithms are common methods used in data-intensive analysis (Hastie,Tibshirani, and Friedman (2001),Butler, Davies, Cartwright, Isayev, and Walsh (2018), Vu,AdalÄś, Ba, BuzsÃąki, Carlson, Heller, Liston, Rudin, Sohal, Widge, Mayberg, Sapiro, andDzirasa (2018)), because they are able to detect emergent patterns in the data without di-rect human supervision. Artificial Neural Networks (ANN) are one of the most popularmachine-learning algorithms. ANNs are designed to mimic biological neural structures ofanimal brains (Mcculloch and Pitts (1943)) by “learning” inherent data patterns throughiterative algorithms (Rojas (1996)). The form of ANN that is discussed in this paper isthe feed-forward multilayer perceptron (MLP) trained using the backpropagation algorithm(Rumelhart, Hintont, and Williams (1986)). MLPs is one of the most popular form of ANNs

arX

iv:2

002.

1142

3v1

[cs

.LG

] 2

6 Fe

b 20

20

Page 2: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

2 NeuralSens: Sensitivity Analysis of Neural Networks

and have been used in a wide variety of applications (Mosavi, Salimi, Faizollahzadeh Ardabili,Rabczuk, Shamshirband, and Varkonyi-Koczy (2019), Smalley (2017), Hornik, Stinchcombe,and White (1989)). This model consists of interconnected units, called nodes or perceptrons,that are arranged in layers. The first layer consists of inputs (or independent variables), thefinal layer is the output layer, and the layers in between are known as hidden layers (ÃŰzesmiand ÃŰzesmi (1999)). Assuming that a relationship between the outputs and the inputs exist,the goal of the MLP is to characterize this relationship. Therefore, MLPs can be understoodas a hyper-parameterized non-linear function approximator able to fit a smooth function thatrelates the response variables with the input variables of a given dataset with minimal residualerror (Hornik (1991), Cybenko (1989)).Neural networks provide predictive advantages comparing to other models, such as the abil-ity to implicitly detect complex non-linear relationships between dependent and independentvariables. However, neural networks are usually seen as “black boxes” that offer minimal infor-mation about relationships among variables and, therefore, are difficult to interpret. Findingmethods for extracting information about the importance and the effect of the variables hasbeen a recurrent research topic in neural networks (Olden, Joy, and Death (2004), Zhang,Beck, Winkler, Huang, Sibanda, and Goyal (2018)). Some examples are:

1. Neural Interpretation Diagram (NID) as described by ÃŰzesmi and ÃŰzesmi (1999) forplotting the ANN structure. A NID is a modification of the standard representationof neural networks that changes the color and thickness of the connections betweenneurons based on the sign and magnitude of its weight.

2. Garson’s algorithm for variable importance (Garson (1991)). It consists in summingthe product of the absolute value of the weights connecting the input variable to theresponse variable through the hidden layer. Afterwards, the result is scaled relative toall other input variables. The relative importance of each input variable is given as avalue from zero to one.

3. Connections weights algorithm as described in Olden et al. (2004). This algorithm issimilar to Garson’s algorithm, but it uses the real value instead of the absolute value ofthe connection weights and it does not scale the result.

4. Input Perturbation (Scardi and Harding (1999), Gevrey, Dimopoulos, and Lek (2003)).It consists in adding an amount of white noise to each input variable while holding theother inputs at a constant value. The resulting change in a chosen error metric for eachinput perturbation represents the relative importance of each input variable.

5. Profile method for sensitivity analysis (Lek, Delacoste, Baran, Dimopoulos, Lauga, andAulagnier (1996)). Similar to the Input Perturbation algorithm, it changes the value ofone input variable while holding the other variables at a constant value. These constantvalues are different quantiles of each variable, so a plot of model predictions acrossthe range of values of each input can be obtained. A modification of this algorithm isproposed in Beck (2018): to avoid unlikely combinations of input values, a clusteringtechnique is applied to the training dataset and the center values of the clusters areused to hold the unevaluated input variables instead of the quantile values.

6. Partial derivatives method for sensitivity analysis (Dimopoulos, Bourret, and Lek (1995),Dimopoulos, Chronopoulos, Chronopoulou-Sereli, and Lek (1999), MuÃśoz and Czerni-

Page 3: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 3

chow (1998), White and Racine (2001)). Perform sensitivity analysis by computing thepartial derivatives of the ANN outputs with respect to the input neurons evaluated onthe samples of the training dataset (or an analogous dataset).

7. Partial dependence plot (Friedman (2001)). This method consist in evaluating the meanof the output variable in modified training datasets where the input variable of intereststays constant in each of its values. When the input variable has been held constantin all the values of the training dataset, the pairs (xji, F̂ (xji)) can be used to showgraphically how the value of the input variable affects the output variable.

8. Local interpretable model-agnostic explanations (Ribeiro, Singh, and Guestrin (2016)).The complex neural network model is explained by approximating it locally with aninterpretable model, such as a linear or decision tree model.

9. Forward stepwise addition (Gevrey et al. (2003)). Consist in rebuilding the neuralnetwork by sequentially adding an input neuron and its corresponding weights. Thechange in each step in a chosen error metric represents the relative importance of thecorresponding input.

10. Backward stepwise elimination (Gevrey et al. (2003)). Consist in rebuilding the neuralnetwork by sequentially removing an input neuron and its corresponding weights. Thechange in each step in a chosen error metric represents the relative importance of thecorresponding input.

These methods “illuminate the black box” by retrieving information from the neural networkmodel. Increasing the information given by these diagnostic methods will have value forexploratory data-analysis and characterization of relationships among variables.However, these methods have some disadvantages: NID can be difficult to interpret given theamount of connections in most networks, Garson’s and Olden’s algorithms does not take intoaccount the values of the inputs and are based only on the weights of the connections, andLek’s profile method may present unlikely scenarios in the original algorithm or require othermethods like clustering (using the center of the clusters instead of the range quantiles of theinput variables) with its inherent disadvantages (Xu and Tian (2015)). Partial dependenceplots have a similar disadvantage because they might give erroneus information if the valueof the output variable rely not only on the variable of interest but also in compound effects ofunlikely combinations of input variables. Local linearization is useful to interpret the inputvariable importance in specific regions of the dataset, but it does not give a quantitativeimportance measure for the entire dataset. Forward stepwise addition and backward stepwiseelimination performs a more exhaustive analysis, but are computionally expensive and mayproduce different results based on the order in which the inputs are added/removed.The partial derivatives method overcomes these disadvantages by calculating the derivativeof each output variable with respect of each input variable evaluated on each data sample of agiven dataset. The contribution of each input is calculated in both magnitude and sign takeninto account not only the connection weights but also the values of each input. By using allthe samples of the dataset, it calculates the effect of the input variables in the response forthe real values of the data, avoiding the information loss due to perform clustering. However,it also has some disadvantages. The operations required to calculate the partial derivativesare time-consuming compared with other methods like Garson’s and Olden’s. The computing

Page 4: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

4 NeuralSens: Sensitivity Analysis of Neural Networks

time grows rapidly as the size of the neural network or the size of the database used tocalculate the partial derivatives increases. If the size of the dataset makes the algorithm tooslow, a clustering technique can be applied to the data and use the center of the clustersinstead of the whole dataset. In addition, the input variables should be normalized whenusing this method, since in other case the value of the partial derivatives might depend onthe scale of each variable and give misleading results.The main objective of the methods mentioned is to extract information of a given neural net-work. However, unnecessary inputs might lead to a higher complexity of the neural structurethat difficult extracting useful information from the model. Moreover, they might preventfinding the optimal neural structure when training the model and, therefore, reduce the per-formance of the neural network. Several researchers defend the capabilities of the partialderivatives method to determine if an explanatory variable is irrelevant to predict the re-sponse of the neural network (White and Racine (2001), Zurada, Malinowski, and Cloete(1994), Engelbrecht, Cloete, and Zurada (1995)). Pruning the neural network of these irrele-vant inputs improves the capability of the neural network to model the relationship betweenresponse and explanatory variables and, consequently, the quality of information that can beextracted from the model.This article describes the NeuralSens package (Pizarroso, Portela, and MuÃśoz (2019))for R (R Core Team (2017)) that was developed to perform sensitivity analysis of MLPneural networks. Functions provided in the package return information about the partialderivatives of the output respect to the inputs of the model, available in an open-sourceprogramming environment. The package includes functions for model objects from themost popular neural network packages available in R. To the authors’ knowledge, thereis no other R package that calculates the partial derivatives of a neural network. TheNeuralSens package is available from the Comprehensive R Archive Network (CRAN) athttps://CRAN.R-project.org/package=NeuralSens, and the development version is main-tained as a GitHub repository at https://github.com/JaiPizGon/NeuralSens. It shouldbe mentioned that other algorithms to analyze neural networks are already implemented inR: NID, Garson’s, Olden’s and Lek’s profile algorithms are implemented in NeuralNetTools(Beck (2018)) package, partial dependence plots method is implemented in pdp (Greenwell(2017)) package and local linearization is implemented in lime (Lin Pedersen and Benesty(2019)) package.The remainder of this article is structured as follows. Section 2 describes the theory ofthe functions in the NeuralSens package, together with references to general introductionsto neural networks. Section 3 presents the architecture details of the package. Section 4shows applied examples for the usage of the NeuralSens package, comparing the results withpackages yet available in R. Finally, section 5 concludes the article.

2. Theoretical foundation

The NeuralSens package has been designed to calculate the partial derivatives of the outputwith respect to the inputs of a MLP model in R. The remainder of this section explains thetheory of multilayer perceptron models, how to calculate the partial derivatives of the outputof this type of model with respect of its inputs and some sensitivity measures proposed bythe authors.

Page 5: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 5

Figure 1: Scheme of the kth neuron in the lth layer of a MLP model. φlk represent theactivation function of the neuron, bl represent the bias of the lth layer, yl−1

j represent theoutput of the jth neuron in the previous layer and wljk represent the weight of the connectionbetween the neuron and the jth neuron of the previous layer.

2.1. Multilayer perceptron

A MLP consists of several layers of neurons (Rumelhart et al. (1986)), being the first theinput layer and the last the output layer. The layers in between are known as hidden layers.A fully-connected feed-forward MLP has one-way connections from the units of one layer toall neurons of the subsequent layer. Information travels along these connections, propagatingthe variables from the input layer to the output layer. Each time the output of one unittravels along one connection to other unit, it is multiplied by the weight of the connection.This weight defines the strength of information flow between layers. At each unit the inputsare summed and a constant, or bias, is added. These bias are only connected to the hiddenand output layers, and are analogous to intercept terms in a standard regression model. Onceall the input terms of each unit are summed, an activation function is applied to the result.Figure 1 shows the scheme of a neuron in a MLP model and represent graphically the opera-tions in equation (1).For each neuron, the output ylk of the kth neuron in the lth layer can be calculated by:

ylk = φlk

(zlk

)= φlk

nl−1∑j=1

wlkj · yl−1j + wlk0 · bl

(1)

where zlk refers to the weighted sum of the neuron inputs, nl−1 refers to the number of neuronsin the (l− 1)th layer, wlkj refers to the weight of the connection between the jth neuron in the(l − 1)th layer and the kth neuron in the lth layer, φlk refers to the activation function of thekth neuron in lth layer, bl refers to the bias in the lth layer and · refers to the scalar productoperation. For the input layer thus holds l = 1, y1−1

j = xj , w1kj = 1 and b1 = 0.

Figure 2 can be treated as a general MLP model. A MLP can have L layers, and each layerl (1 6 l 6 L) has nl (nl > 1) neurons. n1 stands for the input layer and nL for the outputlayer. For each layer l the input dimension is equal to the output dimension of layer (l − 1).

Page 6: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

6 NeuralSens: Sensitivity Analysis of Neural Networks

Figure 2: General MultiLayer Perceptron structure with L layers. φij represent the activationfunction of the jth neuron in the ith layer, bi represent the bias of the ith layer, xk representthe input variables and yk represent the output variables.

For a neuron i (1 6 i 6 nl) in layer l, its input vector, weight vector and output are ybl−1 =(bl, yl−1

1 , · · · , yl−1nl−1

), wl

i =(wli0, w

li1, · · · , wlinl−1

)Tand yli = φli

(zli

)= φli

(ybl−1 ·wl

i

)re-

spectively, where φli : R → R refers to the neuron activation function and · refers to thematrix multiplication operator. For each layer l, its input vector is ybl−1, its weight matrixis Wl =

[wl

1 · · ·wlnl

]and its output vector is yl =

(yli, · · · , ylnl

)= Φl

(zl)

= Φl(ybl−1 ·Wl

),

where Φl : Rnl → Rnl is a vector-valued function defined as Φl(z) = (φl1(z1), · · · , φlnl(znl)).

Identifying the optimal weights that minimize the prediction error of a dataset is calledtraining the neural network. There are different algorithms to identify these weights, beingthe most used the backpropagation algorithm described in Rumelhart et al. (1986). Thisalgorithm consists in an iterative forward- and backward-propagation process where weightsare gradually changed based on the prediction error. It begins by assigning arbitrary weightsto the connections in the network, following by calculating the output of the neural networkfor the training dataset. Then, the prediction error of the neural network is used to calculatehow much the weights of the output layer should change, propagating backwards this errorand changing the value of the weights in the remaining hidden layers. These forward andbackward steps are repeated until the chosen error metric is minimized or until a number ofrepetitions, commonly known as epochs, is reached (Cheng and Titterington (1994)). Othertraining algorithms are out of the scope of this article.

2.2. Partial derivatives

The sensitivity analysis performed by the NeuralSens package is based on the partial deriva-tives method. This method consists in calculating the derivative of the output with respectto the inputs of the neural network.These partial derivatives are called sensitivity, and are

Page 7: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 7

defined as:sik∣∣xn

= ∂yk∂xi

(xn) (2)

where xn refers to the n sample of the dataset used to perform the sensitivity analysis andsik∣∣xn

refers to the sensitivity of the output of the kth neuron in the output layer with respectto the input of the ith neuron in the input layer evaluated in xn. We calculate these sensitivitiesapplying the chain rule to the partial derivatives of the inner layers (derivatives of expression1 for each neuron in the hidden layers). The partial derivatives of the inner layers are definedfollowing the next expressions:

• Derivative of zlk (input of the kth neuron in the lth layer) with respect to yl−1i (output of

the ith neuron in the (l−1)th layer). This partial derivative corresponds to the weight ofthe connection between the kth neuron in the lth layer and the ith neuron in the (l−1)thlayer:

∂zlk∂yl−1

i

= wlik (3)

• Derivative of ylk (output of the the kth neuron in the lth layer) with respect to zli (inputof the ith neuron in the lth layer):

∂ylk∂zli

∣∣∣∣∣zl

i

= ∂φlk∂zli

(zli

)(4)

where ∂φlk

∂zli

refers to the partial derivative of the activation function of the kth neuron inthe lth layer with respect to the input of the kth neuron in the lth layer evaluated forthe input zli of the ith neuron in the lth layer.

The expressions in (3) and (4) have been implemented in the package in matrix form to reducecomputational time following the next expressions:

∂zl[1×nl]

∂yl−1[1×nl−1]

=

∂zl1

∂yl−11

∂zl2

∂yl−11

· · ·∂zl

nl

∂yl−11

∂zl1

∂yl−12

∂zl2

∂yl−12

· · ·∂zl

nl

∂yl−12...

... . . . ...∂zl

1∂yl−1

nl−1

∂zl2

∂yl−1nl−1

· · ·∂zl

nl

∂yl−1nl−1

=

wl11 wl21 · · · wl

nl1wl12 wl22 · · · wl

nl2...... . . . ...

wl1nl−1 wl2nl−1 · · · wlnlnl−1

= W∗l[nl−1×nl]

(5)

Jll[nl×nl]=∂yl[1×nl]

∂zl[1×nl]=

∂yl1

∂zl1

∂yl2

∂zl1· · ·

∂ylnl

∂zl1

∂yl1

∂zl2

∂yl2

∂zl2· · ·

∂ylnl

∂zl2...

... . . . ...∂yl

1∂zl

nl

∂yl2

∂zlnl

· · ·∂yl

nl

∂zlnl

=

∂φl1

∂zl1

(zl1

)∂φl

2∂zl

1

(zl1

)· · ·

∂φlnl

∂zl1

(zl1

)∂φl

1∂zl

2

(zl2

)∂φl

2∂zl

2

(zl2

)· · ·

∂φlnl

∂zl2

(zl2

)...

... . . . ...∂φl

1∂zl

nl

(zlnl

)∂φl

2∂zl

nl

(zlnl

)· · ·

∂φlnl

∂zlnl

(zlnl

)

(6)

Page 8: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

8 NeuralSens: Sensitivity Analysis of Neural Networks

where W∗l is the reduced weight matrix of the lth layer and Jll is the jacobian matrix of theoutputs in the lth layer with respect to the inputs in the lth layer.Following the chain rule, the jacobian matrix of the outputs in the lth layer with respect tothe inputs in the (l − j)th layer can be calculated by:

Jll−j[nl×nj ]=

l−1∏h=j

(Jhh[nh×nh]·W∗(h+1)

[nh×nh+1]) · Jll[nl×nl]

(7)

where 1 6 k 6 (l− 1) and 2 6 l 6 L. Using this expression with l = L and j = 1, the partialderivatives of the outputs with respect to the inputs of the MLP are obtained.

2.3. Sensitivity measuresOnce the sensitivity has been obtained for each variable and observation, different measurescan be calculated to analyze the results. The authors propose the following sensitivity mea-sures to summarize the information obtained by evaluating the sensitivity of the outputs forall the input samples Xn of the provided dataset:

• Mean sensitivity of the output of the kth neuron in the output layer with respect to theith input variable:

Sik,avg =∑Nj=1 sik

∣∣xj

N(8)

where N is the number of samples in the dataset.

• Sensitivity standard deviation of the output of the kth neuron in the output layer withrespect to the ith input variable:

Sik,sd = σ(sik∣∣xj

); j ∈ 1, ..., N (9)

where N is the number of samples in the dataset and σ refers to the standard deviationfunction.

• Mean squared sensitivity of the output of the kth neuron in the output layer with respectto the ith input variable:

Sik,sq =∑Nj=1

(sik∣∣xj

)2

N(10)

where N is the number of samples in the dataset.

In case there are more than one output neuron, such as in a multi-class classification problem,these measures can be generalized to obtain sensitivity measures of the whole model as follows:

• Mean sensitivity with respect to the ith input variable:

Si,avg =∑nl

k=1 Sik,avgnl

(11)

• Sensitivity standard deviation with respect to the ith input variable:

Si,sd =

√√√√∑nl

k=1

(S2ik,sd + (Sik,avg − Si,avg)2

)nl

(12)

Page 9: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 9

• Mean squared sensitivity with respect to the ith input variable:

Si,sq =

(∑nl

k=1√Sik,sq

)2

nl(13)

Methods in NeuralSens to calculate the sensitivities of a neural network and the proposedsensitivities measures were written for several R packages that can be used to create MLPneural networks: neuralnet (Fritsch, Guenther, and Wright (2019)), nnet (Venables and Rip-ley (2002)), RSNNS (Bergmeir and Benítez (2012)), h2o (LeDell, Gill, Aiello, Fu, Candel,Click, Kraljevic, Nykodym, Aboyoun, Kurka, and Malohlava (2019)), neural (Nagy (2014))and forecast (Hyndman and Khandakar (2008)). The same methods are applied to neuralnetwork objects created with the train() function from the caret package (Kuhn, Wing,Weston, Williams, Keefer, Engelhardt, Cooper, Mayer, Kenkel, the R Core Team, Benesty,Lescarbeau, Ziem, Scrucca, Tang, Candan, and Hunt. (2019)) only if these objects inheritfrom the available packages the “class” attribute. Methods have not been included in Neu-ralSens for other packages that can create MLP neural networks, although further develop-ments of NeuralSens could include additional methods. Methods for the functions are cur-rently available for ‘nn’ (neuralnet), ‘nnet’ (nnet), ‘mlp’ (RSNNS), ‘H2ORegressionModel’and ‘H2OMultinomialModel’ (h2o), ‘list’ (neural), ‘nnetar’ (forecast) and ‘train’ (caret)(only if the object inherits the class attribute from another package). Additional method forclass ‘numeric’ is available to use with the basic information of the model (weights, structureand activation functions of the neurons).

3. Package structureNeuralSens includes four functions to perform the sensitivity analysis described in the previoussection. The functions include:

• SensAnalysisMLP(): S3 method to calculate the partial derivatives of the outputs withrespect to the inputs of the MLP model.

• SensitivityPlots(): represent graphically the sensitivity measures calculated by theSensAnalysisMLP() function.

• SensFeaturePlot(): represent graphically the relation between the sensitivities calcu-lated by SensAnalysisMLP() and the value of the input variables.

• SensTimePlot(): represent graphically the evolution among time of the sensitivitiescalculated by SensAnalysisMLP().

Each of these functions are detailed in the remainder of this section. The output of the threelast functions are plots created with ggplot2 package functions (Wickham (2016)).

3.1. MLP Sensitivity Analysis

SensAnalysisMLP() function calculates the partial derivatives of a MLP model. This functionconsists in a ‘S3’ method to extract the basic information of the model (weights, structureand activation functions) based on the model class attribute and pass this information

Page 10: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

10 NeuralSens: Sensitivity Analysis of Neural Networks

Name Function Derivativesigmoid f(z) = 1

1+exp(−z)∂f∂z (z) = 1

1+e−z ·(1− 1

1+e−z

)tanh f(z) = tanh z ∂f

∂z (z) = 1− tanh z

linear f(z) = z f ′(z) = 1

ReLU f(z) ={

0 when z 6 0z when z > 0

∂f∂z (z) =

{0 when z 6 01 when z > 0

PReLU f(z, a) ={a · z when z 6 0z when z > 0

∂f∂z (z, a) =

{a when z 6 01 when z > 0

ELU f(z, a) ={a · (ez − 1) when z 6 0

z when z > 0∂f∂z (z, a) =

{a · (ez − 1) + a when z 6 0

a when z > 0

step f(z) ={

0 when z 6 01 when z > 0

∂f∂z (z, a) =

{NaN when z = 0

0 when z 6= 0arctan f(z) = arctan z ∂f

∂z (z) = 11+ez

softplus f(z) = ln (1 + ez) ∂f∂z (z) = 1

1+e−z

softmax fi(z) = ezi∑k

(ezk )∂fi∂zj

(z) ={fi(z) · (1− fj(z)) when i = j

−fj(z) · fi(z) when i 6= j

Table 1: Accepted activation functions and their derivatives in SensAnalysisMLP(), where zrefers to the input value of the neuron as described in equation (1) and z refers to the vectorof input values of the neuron layer.

to the default method. This default method calculates the sensitivities of the model asdescribed in section 2, and returns this information in two different formats depending onthe value of the .rawSens argument. If .rawSens = FALSE, it returns a ‘data.frame’ (ora ‘list’ of ‘data.frames’ if there are more than one output neuron) with the sensitivitymeasures proposed in section 2.3 (equations (8), (9) and (10)) for each of the input variables.If .rawSens = TRUE, it returns a ‘3-D array’ with the sensitivities for each input, wherethe first dimension refers to each sample of the dataset to evaluate the partial derivatives,the second dimension refers to each input variable and the third dimension refers to eachoutput variable. SensAnalysisMLP() function performs all operations using matrix calculusto reduce the computational time.To calculate the sensitivities, the function assumes that all the neurons in a defined layerhas the same activation function. In the current version of NeuralSens (version 0.1.0), theaccepted activation functions are shown in table 1.In order to show how SensAnalysisMLP() is used, we use a simulated dataset to train aMLP model of class ‘nn’ (RSNNS). The dataset consists in a ‘data.frame’ with 1500 rows ofobservations and four columns for three input variables (X1, X2, X3) and one output variable(Y). The input variables are random observations of normal distribution with zero mean and

Page 11: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 11

standard deviation equal 1. The output variable is a combination of the input variables plusa random noise:

Y = (X1)2 − 0.5 ·X2 + 0.1 · ε (14)where ε is a random noise generated using a normal distribution with zero mean and standarddeviation equal 1.?NeuralSens::simdata can be executed to obtain more information about the data. Thedataset can be loaded executing the following code:

R> library("NeuralSens")R> data("simdata", package = "NeuralSens")

To test the functionality of the SensAnalysisMLP() function, mlp() function from RSNNSpackage trains a neural network model using the simdata dataset.

R> library("RSNNS")R> set.seed(150)R> mod1 <- mlp(simdata[,c("X1","X2","X3")], simdata[,"Y"],+ maxit = 1000, size = 10, linOut = TRUE)

First, we can use SensAnalysisMLP() to obtain the sensitivity measures for each input vari-able:

R> sens <- SensAnalysisMLP(mod1, trData = simdata, output_name = "Y", plot = FALSE)R> sens

varNames mean std meanSensSQ1 X1 -0.005406908 1.94524276 3.78210663382 X2 -0.485564931 0.06734504 0.24030638913 X3 -0.003200699 0.02971083 0.0008925364

The mean column (Sk,avg) of sens ‘data.frame’ shows the mean effect of the input variable onthe output. They give information about the coefficients of the input variables in expression(14) and, in case its relationship with the output is linear, it corresponds to the coefficient.In this case, the mean and std values shows that for the training dataset:

• X1 has a non-constant, i.e., non-linear effect on the response variable with mean ≈ 0and standard deviation ≈ 2

• X2 has a constant, i.e., linear effect on the response variable with mean ≈ 0.5 andstandard deviation ≈ 0.

• X3 has no effect on the response variable due to mean ≈ 0 and standard deviation ≈ 0

Using .rawSens = TRUE, we can obtain the sensitivities of the output with respect to theinput variables for each observation of simdata:

R> rawsens <- SensAnalysisMLP(mod1, trData = simdata, output_name = "Y",+ .rawSens = TRUE, plot = FALSE)R> rawsens[1:5,,1]

Page 12: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

12 NeuralSens: Sensitivity Analysis of Neural Networks

X1 X2 X3[1,] 2.08642384 -0.4462707 -0.044063158[2,] -0.34976334 -0.3547381 0.014188363[3,] -3.87193752 -0.4988454 0.007160451[4,] -0.04241487 -0.4761000 0.006368317[5,] 1.62839774 -0.4555544 -0.037258524

3.2. Visualizing Neural Network Sensitivity Measures

Although useful to perform quantitative analysis, the sensitivities of the output variables canbe difficult to interpret given the amount of data samples that is commonly used in mostnetworks. In order to extract information of the calculated sensitivities, the authors proposethe following plots:

1. Label plot representing the relationship between Sk,avg (x-axis) and Sk,std (y-axis)(White and Racine (2001)). This plot gives information about the relation of the inputswith the output:

• If Sk,avg and Sk,std are near zero, it indicates that the output is not related to theinput, because for all the training data the sensitivity of the output with respectto that input is approximately zero.

• If Sk,avg is different from zero and Sk,std is near zero, it indicates that the output hasa linear relationship with the input, because for all the training data the sensitivityof the output with respect to that input is approximately constant.

• If Sk,std is different from zero, regardless of the value of Sk,avg, it indicates that theoutput has a non-linear relationship with the input, because the relation betweenthe output and the input vary depending on the value of the input.

2. Bar plot that shows Sk,sq for each input variable. This is an absolute measure of thesensitivity of the output with respect to that input, which can be translated to theimportance of the input variable to predict the output.

3. Density plot that shows the distribution of the sensitivities of the output with respectto each input (MuÃśoz and Czernichow (1998)).

• When a distribution is narrow, i.e., the variance of sensitivity values for a giveninput is low, the relationship between output and input is linear.

• When a distribution is wide, i.e., the variance of sensitivity values for a given inputis high, the relationship between output and input is non-linear.

In case that the input variable has no relationship with the output, the distributionvalues would be around zero with low variance.

These plots can be obtained using the SensitivityPlots() function and the sensitivitiescalculated using SensAnalysisMLP(). If the raw sensitivities are not passed in the derargument, function can not create the density plot and therefore only the other two plots areshown. The following code can be executed to obtain the plots of Figure 3:

Page 13: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 13

Figure 3: Example from the SensitivityPlots() function showing plots specified in section3.2. First plot shows the relation between mean and standard deviation of the sensitivities,second plot shows the square of the sensitivities and third plot shows the distribution of thesensitivities.

R> SensitivityPlots(der = rawsens)

In this case, the first and second plot of figure 3 show that Y has a negative linear relationshipwith X2 (std ≈ 0 and mean < 0), no relationship with X3 (std ≈ 0 and mean ≈ 0) and a non-linear relationship with X1 (std different from 0). The second plot supports this information bygiving null importante to X3, and a greater importance to X1 thant to X2. All this informationcorresponds to the coefficients of the variables in expression (14).

3.3. Visualizing Neural Network Sensitivity over timeA common application of neural network is time series forecasting. Information about howthe sensitivities evolve over time can provide a better understanding of the effect of theexplanatory variables on the output variables.SensTimePlot() returns a sequence plot of the raw sensitivities calculated by the SensAnalysisMLP()function. The x-axis is related to a numeric or Posixct/Posixlt variable containing the timeinformation of each sample. The y-axis is related to the sensitivities of the output with respectto each input.In order to show the utility of this function, the DAILY_DEMAND_TR dataset is used to createa model of class ‘train’ from caret package (Kuhn et al. (2019)). This dataset is similar tothe elecdaily dataset from fpp2 R package (Hyndman (2018)) and is composed by:

• DATE: date of the sample, one per day from July, 2nd 2007 to November, 30th 2012.

• TEMP: mean daily temperature in žC in Madrid, Spain.

Page 14: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

14 NeuralSens: Sensitivity Analysis of Neural Networks

Figure 4: Relation between the output DEM and the input TEMP of DAILY_DEMAND_TRdatabase

• WD: working day, continuous parameter which represents the effect on the daily con-sumption of electricity as a percentage of the expected electricity demand of that daywith respect to the demand of the reference day of the same week Moral-Carcedoand VicÃľns-Otero (2005). In this case, Wednesday is the reference day of the week(WDWed ≈ 1).

• DEM: total daily electricity demand in GWh for Madrid, Spain.

The following code loads the dataset and create the plot of figure 4:

R> data("DAILY_DEMAND_TR")R> library("ggplot2")R> ggplot(DAILY_DEMAND_TR) + geom_line(aes(x = TEMP, y = DEM))

Figure 4 shows a non-linear relationship between the electrical demand and the tempera-ture, where the demand increases for low temperatures (due to heating systems) and hightemperatures (due to air conditioners).The following code scales the data, create a train neural network model and apply theSensTimePlot() function to two years of the data:

R> DAILY_DEMAND_TR[,4] <- DAILY_DEMAND_TR[,4]/10R> DAILY_DEMAND_TR[,2] <- DAILY_DEMAND_TR[,2]/100R> library("caret")R> set.seed(150)R> mod2 <- train(form = DEM~TEMP + WD, data = DAILY_DEMAND_TR,+ method = "nnet", linout = TRUE,+ tuneGrid = data.frame(size = 5, decay = 0.1),+ maxit = 250, preProcess = c("center","scale"),+ trControl = trainControl(), metric = "RMSE")R> SensTimePlot(mod2, DAILY_DEMAND_TR[1:(365*2),],+ date.var = DAILY_DEMAND_TR[1:(365*2),1],+ facet = TRUE, output_name = "DEM")

Page 15: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 15

Figure 5: Example from the SensTimePlot() function, showing how the sensitivities for eachof the input evolve over time.

Figure 5 shows that the temperature variable has a seasonal effect on the response variable. Insummer, the temperature is higher and the refrigeration systems demand more electricity, sothe demand is directly proportional to the temperature. In winter, the temperature is lowerand the heating systems demand more electricity, so the demand is inversely proportional tothe temperature. The sensitivity of the output with respect to WD has also a seasonal effect,with higher variance in winter than in summer and greater sensitivity in weekends.

3.4. Visualizing the Neural Network Sensitivity relation as a function of theinput values

Sometimes it is useful to know how the value of the input variables affect the sensitivity of theresponse variables. SensFeaturePlot() function produce a violin plot to show the probabilitydensity of the output sensitivities with respect to each input. It also plots a jitter strip chartfor each input, where the width of the jitter is controlled by the density distribution of thedata (Pedersen (2019)). The color of the points is proportional to the value of each inputvariable, which show if the relation of the output with the input is relatively constant in arange of input values.The following code produce the plot of figure 6:

R> SensFeaturePlot(mod2, fdata = DAILY_DEMAND_TR[1:(365*2),])

In accordance with the information extracted from figure 5, figure 6 shows that the sensitivityof the output with respect to the temperature is negative when the temperature is low andpositive when the temperature is high. It also shows that the sensitivity of the output withrespect to WD is higher in the weekends (WD < 0.6).

Page 16: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

16 NeuralSens: Sensitivity Analysis of Neural Networks

Figure 6: Example from the SensFeaturePlot() function, showing the relationship of thesensitivities with the value of the inputs.

4. Further examples and comparison with other methodsThis section contains several examples in which the functions of the NeuralSens package arecompared with similar functions from other R packages.

Topics like data pre-processing or network architecture should be considered before modeldevelopment. Discussions about these ideas have been already held (Cannas, Fanni, See, andSias (2006), Amasyali and M. El-Gohari (2017), Maier and Dandy (1999), Lek et al. (1996))and are beyond the scope of this paper.

4.1. Multilayer perceptron for classification

In this example a multilayer perceptron is trained using the well-known iris dataset includedin R. Figure 7 shows that setosa species have a smaller petal size than the other two speciesand a shorter sepal. It also shows that virginica and versicolor species have a similarsepal size, but the latter have a slightly smaller petal size.The function createDataPartition() from caret package divides the data into training andtest, and the train() function from the same package creates a new neural network model.

R> data("iris")R> set.seed(150)R> mod3 <- train(Species ~ ., data = iris_train,+ method = "nnet", linout = TRUE,+ tuneGrid = data.frame(size = 5, decay = 0.1),+ maxit = 150, preProcess = c("center","scale"),

Page 17: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 17

(a) Sepal-related variables (b) Petal-related variables

Figure 7: Plots of the iris dataset. (7a) geom_point plot representing the vari-ables Sepal.Length and Sepal.Width, (7b) geom_point plot representing the variablesPetal.Length and Petal.Width

+ trControl = trainControl(), metric = "Accuracy")

Once the model is trained, the SensAnalysisMLP() function calculates the sensitivities of themodel for each of the levels of the output.

R> sens <- SensAnalysisMLP(mod3)R> sens

$setosavarNames mean std meanSensSQ

1 Sepal.Length 0.02078064 0.01987897 0.0008243742 Sepal.Width 0.03402607 0.03609897 0.0024522213 Petal.Length -0.05359752 0.05811155 0.0062271334 Petal.Width -0.04541914 0.04375004 0.003964204

$versicolorvarNames mean std meanSensSQ

1 Sepal.Length 0.07844830 0.1141276 0.019092402 Sepal.Width 0.06335535 0.1514716 0.026804593 Petal.Length -0.08212536 0.2278483 0.058313324 Petal.Width -0.16249879 0.2454626 0.08625607

$virginicavarNames mean std meanSensSQ

1 Sepal.Length -0.1008144 0.1320715 0.027490152 Sepal.Width -0.1156863 0.1501158 0.035767833 Petal.Length 0.1685571 0.2181727 0.07569352

Page 18: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

18 NeuralSens: Sensitivity Analysis of Neural Networks

4 Petal.Width 0.2137063 0.2797617 0.12341518

The mean of the sensitivities for each of the output gives information about how the neuralnetwork uses the data to predict the output:

• The setosa class has a greater probability when the petal related and Sepal.Lengthvariables are low, but the Sepal.Width variable is high.

• The versicolor class has a greater probability when the petal related variables arelow, but the Sepal.Length variable is high.

• The virginica class has a greater probability when the petal related variables are high.

This information corresponds to the information extracted from figure 7, where setosa classis characterized by a low value of Sepal.Length and petal related variables, and versicolorand virginica classes are differentiated by the value of the petal related variables.However, sometimes is more interesting to obtain global importance measures instead ofmeasures for each output variable. CombineSens() function computes the sensitivity measuresof the whole model (equations 11, 12 and 13).

R> combined_sens <- CombineSens(sens)R> combined_sens

varNames mean std meanSensSQ1 Sepal.Length 0.001447138 0.03545616 0.0012273452 Sepal.Width -0.004311108 0.05770054 0.0031653163 Petal.Length 0.011624836 0.24404886 0.0506373084 Petal.Width 0.011632454 0.23246026 0.045703051

Although the global measures give information about the importance of each input, garson()and olden() method from the NeuralNetTools package (Beck (2018)) gives information re-lated to the first response variable, so the comparison is done using the sensitivity measuresfor the first output class.

R> SensitivityPlots(sens[[1]])R> garson(mod3)R> olden(mod3)

These two methods have their analogous plot in figure 8a. Second plot of this figure cor-responds to the plot of garson() method (figure 8b). The top chart of figure 8a gives thesame information as the olden() method (figure 8c), i.e., Sepal.Width importance sign isthe opposite of the rest of the input variable importances.The lime (Lin Pedersen and Benesty (2019)) package can also be used to obtain informationabout the neural network model. In this case, lime() and explain() functions train adecision tree model to interpret how the neural network predict the class of three differentsamples (one for each level of iris[,"Species"]) using all the features in the training dataset.plot_features() function show graphically the information given by the decision tree.

Page 19: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 19

(a) Model 3, SensitivityPlots()

(b) Model 3, garson()

(c) Model 3, olden()

Figure 8: (8a) Global sensitivity measures plots of mod3 for the iris dataset usingSensitivityPlots(), (8b) variable importance using garson() from NeuralNetTools, (8c)variable importance using olden() from NeuralNetTools.

R> library("lime")R> explanation <- lime(iris, mod3)R> exp <- explain(iris[c(1,51,101),], explanation,+ n_labels = 3, n_features = 4)R> plot_explanations(exp)

Figure 9 confirms the relationships between the inputs and the output variable. However,this method does not provide a quantitative measure for the importance of each input. Theimportance of each feature is given by the absolute value of its weights, because it wouldhave a greater impact on the output. Due to the lack of quantitative measures for inputimportance this method can not be directly compared with the other methods exposed inthis section (figure 8).

4.2. Multilayer perceptron for regression

The Boston dataset from MASS (Ripley, Venables, Bates, Hornik, Gebhardt, Firth, and ca(2019)) package is used to train a nnet (Venables and Ripley (2002)) model. This dataset

Page 20: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

20 NeuralSens: Sensitivity Analysis of Neural Networks

Figure 9: Facetted heatmap-style plots generated by applying the plot_explanations()function to mod3 for three selected samples of iris dataset. Each plot represent the con-tribution (positive or negative) of each feature to the probability of a specific class ofiris[,"Species"]. The case numbers are shown in the horizontal axis and categorizedfeatures are shown in the vertical axis.

contains information collected by the U.S. Census Service concerning housing in the area ofBoston Mass (run ?MASS::Boston to obtain more information about the dataset).The objective of the model is to predict the nitric oxides concentration (parts per 10 million),stored in the nox variable. The input variables of the model are zn, chas, rad and lstat.scale() function calculates the z-scores of the input variables.

R> data("Boston", package = "MASS")R> Boston <- Boston[,c("zn","rad","lstat","nox")]R> Boston <- as.data.frame(scale(Boston))R> set.seed(150)R> mod4 <- nnet::nnet(+ nox ~ .,+ data = Boston,+ size = 15, decay = 0.1, maxit = 150+ )

SensFeaturePlot(), lekprofile() (NeuralNetTools Beck (2018)) and pdp() (pdp Green-

Page 21: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 21

(a) Model 4, lekprofile()

(b) Cluster grouping

Figure 10: (10a) Sensitivity analysis of a neural network using lekprofile() from Neural-NetTools. (10b) Values at which explanatory variables are held constant for each cluster.

well (2017)) functions analyze the relationships of the output with respect to the inputs.

R> p1 <- lekprofile(mod4, group_vals = 6)R> p2 <- lekprofile(mod4, group_vals = 6, group_show = TRUE)R> gridExtra::grid.arrange(grobs = list(p1,p2))R> library("pdp")R> pdps <- list()R> for (i in 1:3) {+ pdps[[i]] <- partial(mod4, pred.var = names(Boston)[i],+ train = Boston, plot = TRUE, pred.fun = pred)+ }R> gridExtra::grid.arrange(grobs = pdps, nrow = 1)R> rawsens4 <- SensAnalysisMLP(mod4, trData = Boston,+ output_name = "nox", .rawSens = TRUE)R> SensFeaturePlot(rawsens4, fdata = Boston)R> pred <- function(object, newdata) {+ pred <- predict(object, newdata)+ mean(pred)+ }

Figure 10 shows the results of the Lek’s profile method from NeuralNetTools package. Each

Page 22: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

22 NeuralSens: Sensitivity Analysis of Neural Networks

Figure 11: Partial dependence plots of mod4 usingpdp() from pdp package. They show themean variation of the output when the input variables change throughout their entire range.

Figure 12: SensFeaturePlot() from NeuralSens applied to mod4. Show the relationshipbetween the sensitivities of the model and the value of the input variables.

plot shows the effect of each input variable by showing model predictions across the rangeof values of that input. The lines of each plot refer to the value of the output across therange of one input when the other two inputs remain constant. In this example, we have usedgroup_vals = 6, performing a k-means clustering of 6 clusters, and choosing the value ofthe input in the center of these clusters to calculate each of the lines of the plot. The secondplot of figure 10 shows the value of the input variables in each of the cluster centers.Figure 11 shows the partial dependence plots of mod4 output. It gives similar results as Lek’sprofile method, although pdp may be more interpretable because it does not consider differentscenarios and return only the mean value of the output. For the same reason this analysis

Page 23: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 23

might give misleading information by avoiding unlikely but representative combination ofinputs.SensFeaturePlot() function performs a similar analysis as Lek’s and pdp by plotting thesensitivity of the output with respect to the input colored proportionally to the value of theinput variable. On the one hand, lekprofile() function indicates that lstat and rad have adirect relationship with the output and zn has an inverse relationship with the output. Figure10 also suggests that all the input variables have a non-linear relationship with the output.On the other hand, figure 12 shows that the sensitivity of the output has an approximatelylinear relationship with zn, and a non-linear relationship with the other two inputs. In thiscase, lekprofile() function give6s more information about the model, but can be difficultto understand how the value of the input variables in each of the groups affect the outputvariable.

5. ConclusionsThe NeuralSens package provides functions to extract information from a fitted feed-forwardMLP neural network in R. These functions can be used to obtain the partial derivativesof the neural network response variables with respect to the input variables, and generateplots to obtain diverse information of the network using these partial derivatives. Methodsare available for the next CRAN packages: ‘nn’ (neuralnet), ‘nnet’ (nnet), ‘mlp’ (RSNNS),‘H2ORegressionModel’ and ‘H2OMultinomialModel’ (h2o), ‘list’ (neural), ‘nnetar’ (fore-cast) and ‘train’ (caret) (only if the object inherits the class attribute from another package).Additional method for class ‘numeric’ is available to use with the basic information of themodel (weights, structure and activation functions of the neurons).The main objective of the package is helping the user to understand how the neural networkuses the input to predict the output. This information could be useful to simplify the neuralnetwork structure by erasing the inputs which are not used to predict the output. It couldalso give a deeper understanding on the problem and the relationship among variables. Neu-ralSens is another tool from among other several methods for exploratory data analysis andmodel evaluation, and it could be used with other packages (Beck (2018)) to obtain optimalinformation about the neural network model.Future versions of the package may include additional functionalities as:

• Parallelizing the sensitivity calculations when workers registered to work in parallel aredetected.

• Calculate the sensitivities of the output variables with respect to the output of hiddenneurons, in order to obtain the importance of each hidden neuron and help with theselection of the optimal network architecture.

• Calculate the sensitivities of other neural network models such as Probabilistic RadialBasis Function Network (PRBFN) or Recurrent Neural Network (RNN)

References

Page 24: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

24 NeuralSens: Sensitivity Analysis of Neural Networks

Amasyali K, M El-Gohari N (2017). “A review of data-driven building energy consumptionprediction studies.” doi:https://doi.org/10.1016/j.rser.2017.04.095.

Beck MW (2018). “NeuralNetTools : Visualization and Analysis Tools for Neural Net-works.” Journal of Statistical Software, 85(11). ISSN 1548-7660. doi:10.18637/jss.v085.i11. URL http://www.jstatsoft.org/v85/i11/.

Bergmeir C, Benítez JM (2012). “Neural Networks in R Using the Stuttgart Neural NetworkSimulator: RSNNS.” Journal of Statistical Software, 46(7), 1–26. URL http://www.jstatsoft.org/v46/i07/.

Butler KT, Davies DW, Cartwright H, Isayev O, Walsh A (2018). “Machine learning formolecular and materials science.” Nature, 559(7715), 547–555. ISSN 0028-0836, 1476-4687.doi:10.1038/s41586-018-0337-2.

Cannas B, Fanni A, See L, Sias G (2006). “Data preprocessing for river flow forecasting usingneural networks: Wavelet transforms and data partitioning.” Elsevier, 31, 1164–1171.doi:https://doi.org/10.1016/j.pce.2006.03.020.

Cheng B, Titterington DM (1994). “Neural Networks: A Review from a Statistical Perspec-tive.” Statist. Sci., 9(1), 2–30. doi:10.1214/ss/1177010638.

Cybenko G (1989). “Approximation by superpositions of a sigmoidal function.” Mathe-matics of Control, Signals and Systems, 2(4), 303–314. ISSN 1435-568X. doi:10.1007/BF02551274.

Dimopoulos I, Chronopoulos J, Chronopoulou-Sereli A, Lek S (1999). “Neural network modelsto study relationships between lead concentration in grasses and permanent urban descrip-tors in Athens city (Greece).” Ecological Modelling, 120(2-3), 157–165. ISSN 03043800.doi:10.1016/S0304-3800(99)00099-X.

Dimopoulos Y, Bourret P, Lek S (1995). “Use of some sensitivity criteria for choosingnetworks with good generalization ability.” Neural Processing Letters, 2, 1–4. doi:10.1007/BF02309007.

Engelbrecht AP, Cloete I, Zurada JM (1995). “Determining the significance of input pa-rameters using sensitivity analysis.” In G Goos, J Hartmanis, J Leeuwen, J Mira,F Sandoval (eds.), From Natural to Artificial Neural Computation, volume 930, pp.382–388. Springer Berlin Heidelberg, Berlin, Heidelberg. ISBN 978-3-540-59497-0 978-3-540-49288-7. doi:10.1007/3-540-59497-3_199. URL http://link.springer.com/10.1007/3-540-59497-3_199.

Friedman JH (2001). “Greedy function approximation: a gradient boosting machine.” 29(05),1189–1232.

Fritsch S, Guenther F, Wright MN (2019). “neuralnet: Training of Neural Networks.” Rpackage version 1.44.2, URL https://CRAN.R-project.org/package=neuralnet.

Garson GD (1991). “Interpreting Neural-network Connection Weights.” AI Expert, 6(4),46–51. ISSN 0888-3785. URL http://dl.acm.org/citation.cfm?id=129449.129452.

Page 25: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 25

Gevrey M, Dimopoulos Y, Lek S (2003). “Review and comparison of methods to study thecontribution of variables in Artificial Neural Networks models.” Ecological Modelling, 160,249–264. doi:10.1016/S0304-3800(02)00257-0.

Greenwell BM (2017). “pdp: An R Package for Constructing Partial Dependence Plots.”The R Journal, 9(1), 421–436. URL https://journal.r-project.org/archive/2017/RJ-2017-016/index.html.

Hastie T, Tibshirani R, Friedman J (2001). The Elements of Statistical Learning. SpringerSeries in Statistics. Springer New York Inc., New York, NY, USA.

Hornik K (1991). “Approximation capabilities of multilayer feedforward networks.” NeuralNetworks, 4(2), 251–257. ISSN 0893-6080. doi:10.1016/0893-6080(91)90009-T.

Hornik K, Stinchcombe M, White H (1989). “Multilayer feedforward networks are univer-sal approximators.” Neural Networks, 2(5), 359–366. ISSN 08936080. doi:10.1016/0893-6080(89)90020-8.

Hyndman R (2018). fpp2: Data for "Forecasting: Principles and Practice" (2nd Edition). Rpackage version 2.3, URL https://CRAN.R-project.org/package=fpp2.

Hyndman RJ, Khandakar Y (2008). “Automatic time series forecasting: the forecast packagefor R.” Journal of Statistical Software, 26(3), 1–22. URL http://www.jstatsoft.org/article/view/v027i03.

Kelling S, Hochachka WM, Fink D, Riedewald M, Caruana R, Ballard G, Hooker G (2009).“Data-intensive Science: A New Paradigm for Biodiversity Studies.” BioScience, 59(7),613–620. ISSN 0006-3568, 1525-3244. doi:10.1525/bio.2009.59.7.12.

Kuhn M, Wing J, Weston S, Williams A, Keefer C, Engelhardt A, Cooper T, Mayer Z, KenkelB, the R Core Team, Benesty M, Lescarbeau R, Ziem A, Scrucca L, Tang Y, Candan C,Hunt T (2019). “caret: Classification and Regression Training.” R package version 6.0-84,URL https://CRAN.R-project.org/package=caret.

LeDell E, Gill N, Aiello S, Fu A, Candel A, Click C, Kraljevic T, Nykodym T, Aboyoun P,Kurka M, Malohlava M (2019). “h2o: R Interface for ’H2O’.” R package version 3.26.0.2,URL https://CRAN.R-project.org/package=h2o.

Lek S, Delacoste M, Baran P, Dimopoulos Y, Lauga J, Aulagnier S (1996). “Application ofneural networks to modeling nonlinear relationships in ecology.” Ecological Modelling, 90,39–52. doi:10.1016/0304-3800(95)00142-5.

Lin Pedersen T, Benesty M (2019). “Understanding lime.” URL https://cran.r-project.org/web/packages/lime/vignettes/Understanding_lime.html.

Maier HR, Dandy GC (1999). “Neural networks for the prediction and forecasting of waterresources variables: a review of modelling issues and applications.” doi:https://doi.org/10.1016/S1364-8152(99)00007-9.

Mcculloch WS, Pitts W (1943). “A logical calculus of the ideas immanent in nervous activity.”pp. 115–133.

Page 26: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

26 NeuralSens: Sensitivity Analysis of Neural Networks

Michener WK, Jones MB (2012). “Ecoinformatics: supporting ecology as a data-intensivescience.” Trends in Ecology & Evolution, 27(2), 85–93. ISSN 01695347. doi:10.1016/j.tree.2011.11.016.

Moral-Carcedo J, VicÃľns-Otero J (2005). “Modelling the non-linear response of Spanishelectricity demand to temperature variations.” Energy Economics, 27(3), 477–494. ISSN01409883. doi:10.1016/j.eneco.2005.01.003.

Mosavi A, Salimi M, Faizollahzadeh Ardabili S, Rabczuk T, Shamshirband S, Varkonyi-KoczyA (2019). “State of the Art of Machine Learning Models in Energy Systems, a SystematicReview.” Energies, 12(7), 1301. ISSN 1996-1073. doi:10.3390/en12071301.

MuÃśoz A, Czernichow T (1998). “Variable selection using feedforward and recurrent neuralnetworks.” Engineering Intelligent Systems for Electrical Engineering and Communications,6(2), 102.

Nagy A (2014). “neural: Neural Networks.” R package version 1.4.2.2, URL https://CRAN.R-project.org/package=neural.

Olden JD, Joy MK, Death RG (2004). “An accurate comparison of methods for quanti-fying variable importance in artificial neural networks using simulated data.” EcologicalModelling, 178(3-4), 389–397. ISSN 03043800. doi:10.1016/j.ecolmodel.2004.03.013.

Pedersen TL (2019). ggforce: Accelerating ’ggplot2’. R package version 0.3.1, URL https://CRAN.R-project.org/package=ggforce.

Pizarroso J, Portela J, MuÃśoz A (2019). “NeuralSens: Sensitivity analysis of neu-ral networks.” R package version 0.0.9, URL https://CRAN.R-project.org/package=NeuralSens.

R Core Team (2017). “R: A Language and Environment for Statistical Computing.” URLhttps://www.R-project.org/.

Ribeiro MT, Singh S, Guestrin C (2016). “"Why Should I Trust You?": Explaining thePredictions of Any Classifier.” In Proceedings of the 22Nd ACM SIGKDD InternationalConference on Knowledge Discovery and Data Mining, KDD ’16, pp. 1135–1144. ACM,New York, NY, USA. ISBN 978-1-4503-4232-2. doi:10.1145/2939672.2939778. Event-place: San Francisco, California, USA.

Ripley B, Venables B, Bates DM, Hornik K, Gebhardt A, Firth D, ca (2019). “MASS:Support Functions and Datasets for Venables and Ripley’s MASS.” URL https://CRAN.R-project.org/package=MASS.

Rojas R (1996). “Fast Learning Algorithms.” In Neural Networks, pp. 183–225. SpringerBerlin Heidelberg, Berlin, Heidelberg. ISBN 978-3-540-60505-8 978-3-642-61068-4. doi:10.1007/978-3-642-61068-4_8.

Rumelhart DE, Hintont GE, Williams RJ (1986). “Learning representations by back-propagating errors.” p. 4.

Page 27: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

Jaime Pizarroso, José Portela, Antonio Muñoz 27

Scardi M, Harding LW (1999). “Developing an empirical model of phytoplankton primaryproduction: a neural network case study.” Ecological Modelling, 120(2-3), 213–223. ISSN03043800. doi:10.1016/S0304-3800(99)00103-9.

Smalley E (2017). “AI-powered drug discovery captures pharma interest.” Nature Biotech-nology, 35(7), 604–605. ISSN 1087-0156, 1546-1696. doi:10.1038/nbt0717-604.

Sun Z, Sun L, Strang K (2018). “Big Data Analytics Services for Enhancing Business In-telligence.” Journal of Computer Information Systems, 58(2), 162–169. ISSN 0887-4417,2380-2057. doi:10.1080/08874417.2016.1220239.

Venables WN, Ripley BD (2002). Modern Applied Statistics with S. Fourth edition. Springer,New York. ISBN 0-387-95457-0, URL http://www.stats.ox.ac.uk/pub/MASS4.

Vu MAT, AdalÄś T, Ba D, BuzsÃąki G, Carlson D, Heller K, Liston C, Rudin C, SohalVS, Widge AS, Mayberg HS, Sapiro G, Dzirasa K (2018). “A Shared Vision for MachineLearning in Neuroscience.” The Journal of Neuroscience, 38(7), 1601–1607. ISSN 0270-6474, 1529-2401. doi:10.1523/JNEUROSCI.0508-17.2018.

White H, Racine J (2001). “Statistical inference, the bootstrap, and neural-network modelingwith application to foreign exchange rates.” IEEE Transactions on Neural Networks, 12(4),657–673. ISSN 10459227. doi:10.1109/72.935080. URL http://ieeexplore.ieee.org/document/935080/.

Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York.ISBN 978-3-319-24277-4. URL https://ggplot2.tidyverse.org.

Xu D, Tian Y (2015). “A Comprehensive Survey of Clustering Algorithms.” Annals of DataScience, 2. doi:10.1007/s40745-015-0040-1.

ÃŰzesmi SL, ÃŰzesmi U (1999). “An artificial neural network approach to spatial habi-tat modelling with interspecific interaction.” Ecological Modelling, 116(1), 15–31. ISSN03043800. doi:10.1016/S0304-3800(98)00149-5.

Zhang Z, Beck MW, Winkler DA, Huang B, Sibanda W, Goyal H (2018). “Opening theblack box of neural networks: methods for interpreting neural network models in clinicalapplications.” Annals of Translational Medicine, 6(11). ISSN 2305-5839. doi:10.21037/atm.2018.05.32.

Zurada J, Malinowski A, Cloete I (1994). “Sensitivity analysis for minimization of in-put data dimension for feedforward neural network.” In Proceedings of IEEE Interna-tional Symposium on Circuits and Systems - ISCAS ’94, volume 6, pp. 447–450. IEEE,London, UK. ISBN 978-0-7803-1915-8. doi:10.1109/ISCAS.1994.409622. URL http://ieeexplore.ieee.org/document/409622/.

Affiliation:Jaime Pizarroso, José Portela, Antonio MuñozInstituto de InvestigaciÃşn TecnolÃşgica (IIT)

Page 28: NeuralSens: Sensitivity Analysis of Neural Networks · for R (R Core Team(2017)) that was developed to perform sensitivity analysis of MLP neural networks. Functions provided in the

28 NeuralSens: Sensitivity Analysis of Neural Networks

Escuela TÃľcnica Superior de IngenierÃŋa ICAIUniversidad Pontificia ComillasCalle de Sta. Cruz de Marcenado, 2628015 Madrid, SpainE-mail: [email protected],[email protected],[email protected]