plot: logical whether or not to plot the analysis. By default is TRUE.
.rawSens: DEPRECATED
sens_origin_layer: numeric specifies the layer of neurons with respect to which the derivative must be calculated. The input layer is specified by 1 (default).
sens_end_layer: numeric specifies the layer of neurons of which the derivative is calculated. It may also be 'last' to specify the output layer (default).
sens_origin_input: logical specifies if the derivative must be calculated with respect to the inputs (TRUE) or output (FALSE) of the sens_origin_layer layer of the model. By default is TRUE.
sens_end_input: logical specifies if the derivative calculated is of the output (FALSE) or from the input (TRUE) of the sens_end_layer layer of the model. By default is FALSE.
...: additional arguments passed to or from other methods
trData: data.frame containing the data to evaluate the sensitivity of the model
actfunc: character vector indicating the activation function of each neurons layer.
deractfunc: character vector indicating the derivative of the activation function of each neurons layer.
der2actfunc: character vector indicating the second derivative of the activation function of each neurons layer.
preProc: preProcess structure applied to the training data. See also preProcess
terms: function applied to the training data to create factors. See also train
output_name: character name of the output variable in order to avoid changing the name of the output variable in trData to '.outcome'
Returns
SensMLP object with the sensitivity metrics and sensitivities of the MLP model passed to the function.
Details
In case of using an input of class factor and a package which need to enter the input data as matrix, the dummies must be created before training the neural network.
After that, the training data must be given to the function using the trData argument.
Plots
Plot 1: colorful plot with the classification of the classes in a 2D map
Plot 2: b/w plot with probability of the chosen class in a 2D map
Plot 3: plot with the stats::predictions of the data provided
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR
## Parameters of the NNET ------------------------------------------------------hidden_neurons <-5iters <-100decay <-0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3]<- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1]<- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse =" + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep =" ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output =TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPNeuralSens::HessianMLP(nnetmod, trData = nntrData, plot =FALSE)# Try HessianMLP to calculate sensitivities with respect to output of hidden neuronesNeuralSens::HessianMLP(nnetmod, trData = nntrData, sens_origin_layer =2, sens_end_layer ="last", sens_origin_input =FALSE, sens_end_input =FALSE)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method ="boot", savePredictions =FALSE, summaryFunction = caret::defaultSummary)set.seed(150)#For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.tr, method ="nnet", linout =TRUE, tuneGrid = data.frame(size =3, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric ="RMSE")# Try HessianMLPNeuralSens::HessianMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create a cluster with 4 available coresh2o::h2o.init(ip ="localhost", nthreads =4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.tr, destination_frame ="fdata_h2o")set.seed(150)h2omod <-h2o:: h2o.deeplearning(x = names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], y = names(fdata.Reg.tr)[1], distribution ="AUTO", training_frame = fdata_h2o, standardize =TRUE, activation ="Tanh", hidden = c(hidden_neurons), stopping_rounds =0, epochs = iters, seed =150, model_id ="nnet_h2o", adaptive_rate =FALSE, rate_decay = decay, export_weights_and_biases =TRUE)# Try HessianMLPNeuralSens::HessianMLP(h2omod)# Turn off the clusterh2o::h2o.shutdown(prompt =FALSE)rm(fdata_h2o)## Train RSNNS NNET ------------------------------------------------------------# Normalize data using RSNNS algorithmstrData <- as.data.frame(RSNNS::normalizeData(fdata.Reg.tr))names(trData)<- names(fdata.Reg.tr)set.seed(150)RSNNSmod <-RSNNS::mlp(x = trData[,2:ncol(trData)], y = trData[,1], size = hidden_neurons, linOut =TRUE, learnFuncParams=c(decay), maxit=iters)# Try HessianMLPNeuralSens::HessianMLP(RSNNSmod, trData = trData, output_name ="DEM")## USE DEFAULT METHOD ----------------------------------------------------------NeuralSens::HessianMLP(caretmod$finalModel$wts, trData = fdata.Reg.tr, mlpstr = caretmod$finalModel$n, coefnames = caretmod$coefnames, actfun = c("linear","sigmoid","linear"), output_name ="DEM")######################################################################################################### CLASSIFICATION NNET ################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.cl <- fdata[,2:ncol(fdata)]fdata.Reg.cl[,2:3]<- fdata.Reg.cl[,2:3]/10fdata.Reg.cl[,1]<- fdata.Reg.cl[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)# Factorize the outputfdata.Reg.cl$DEM <- factor(round(fdata.Reg.cl$DEM, digits =1))# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method ="boot", savePredictions =FALSE, summaryFunction = caret::defaultSummary)set.seed(150)#For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.cl, method ="nnet", linout =FALSE, tuneGrid = data.frame(size = hidden_neurons, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric ="Accuracy")# Try HessianMLPNeuralSens::HessianMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create local cluster with 4 available coresh2o::h2o.init(ip ="localhost", nthreads =4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.cl, destination_frame ="fdata_h2o")set.seed(150)h2omod <- h2o::h2o.deeplearning(x = names(fdata.Reg.cl)[2:ncol(fdata.Reg.cl)], y = names(fdata.Reg.cl)[1], distribution ="AUTO", training_frame = fdata_h2o, standardize =TRUE, activation ="Tanh", hidden = c(hidden_neurons), stopping_rounds =0, epochs = iters, seed =150, model_id ="nnet_h2o", adaptive_rate =FALSE, rate_decay = decay, export_weights_and_biases =TRUE)# Try HessianMLPNeuralSens::HessianMLP(h2omod)# Apaga el clusterh2o::h2o.shutdown(prompt =FALSE)rm(fdata_h2o)## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse =" + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep =" ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output =TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPNeuralSens::HessianMLP(nnetmod, trData = nntrData)