R/AgnosticWrapper.R
AgnosticWrapper.Rd
This is a super class for all implemented model-agnostic
interpretability methods and inherits from the InterpretingMethod
class. Instead of just an object of the Converter
class, any model
can now be passed. In contrast to the other model-specific methods in this
package, only the prediction function of the model is required, and not
the internal details of the model. The following model-agnostic methods
are available (all are wrapped by other packages):
Shapley values (SHAP
) based on fastshap::explain
Local interpretable model-agnostic explanations (LIME
) based on
lime::lime
innsight::InterpretingMethod
-> AgnosticWrapper
data_orig
The individual instances to be explained by the method (unprocessed!).
new()
Create a new instance of the AgnosticWrapper
R6 class.
AgnosticWrapper$new(
model,
data,
data_ref,
output_type = NULL,
pred_fun = NULL,
output_idx = NULL,
output_label = NULL,
channels_first = TRUE,
input_dim = NULL,
input_names = NULL,
output_names = NULL
)
model
(any prediction model)
A fitted model for a classification or regression task that
is intended to be interpreted. A Converter
object can also be
passed. In order for the package to know how to make predictions
with the given model, a prediction function must also be passed with
the argument pred_fun
. However, for models created by
nn_sequential
, keras_model
,
neuralnet
or Converter
,
these have already been pre-implemented and do not need to be
specified.
data
(array
, data.frame
or torch_tensor
)
The individual instances to be explained by the method.
These must have the same format as the input data of the passed model
and has to be either matrix
, an array
, a data.frame
or a
torch_tensor
. If no value is specified, all instances in the
dataset data
will be explained.
Note: For the model-agnostic methods, only models with a single
input and output layer is allowed!
data_ref
(array
, data.frame
or torch_tensor
)
The dataset to which the method is to be applied. These must
have the same format as the input data of the passed model and has to
be either matrix
, an array
, a data.frame
or a
torch_tensor
.
Note: For the model-agnostic methods, only models with a single
input and output layer is allowed!
output_type
(character(1)
)
Type of the model output, i.e., either
"classification"
or "regression"
.
pred_fun
(function
)
Prediction function for the model. This argument is only
needed if model
is not a model created by
nn_sequential
, keras_model
,
neuralnet
or Converter
. The first argument of
pred_fun
has to be newdata
, e.g.,
function(newdata, ...) model(newdata)
output_idx
(integer
, list
or NULL
)
These indices specify the output nodes for which
the method is to be applied. In order to allow models with multiple
output layers, there are the following possibilities to select
the indices of the output nodes in the individual output layers:
An integer
vector of indices: If the model has only one output
layer, the values correspond to the indices of the output nodes, e.g.,
c(1,3,4)
for the first, third and fourth output node. If there are
multiple output layers, the indices of the output nodes from the first
output layer are considered.
A list
of integer
vectors of indices: If the method is to be
applied to output nodes from different layers, a list can be passed
that specifies the desired indices of the output nodes for each
output layer. Unwanted output layers have the entry NULL
instead of
a vector of indices, e.g., list(NULL, c(1,3))
for the first and
third output node in the second output layer.
NULL
(default): The method is applied to all output nodes in
the first output layer but is limited to the first ten as the
calculations become more computationally expensive for more output
nodes.
output_label
(character
, factor
, list
or NULL
)
These values specify the output nodes for which
the method is to be applied. Only values that were previously passed with
the argument output_names
in the converter
can be used. In order to
allow models with multiple
output layers, there are the following possibilities to select
the names of the output nodes in the individual output layers:
A character
vector or factor
of labels: If the model has only one output
layer, the values correspond to the labels of the output nodes named in the
passed Converter
object, e.g.,
c("a", "c", "d")
for the first, third and fourth output node if the
output names are c("a", "b", "c", "d")
. If there are
multiple output layers, the names of the output nodes from the first
output layer are considered.
A list
of charactor
/factor
vectors of labels: If the method is to be
applied to output nodes from different layers, a list can be passed
that specifies the desired labels of the output nodes for each
output layer. Unwanted output layers have the entry NULL
instead of
a vector of labels, e.g., list(NULL, c("a", "c"))
for the first and
third output node in the second output layer.
NULL
(default): The method is applied to all output nodes in
the first output layer but is limited to the first ten as the
calculations become more computationally expensive for more output
nodes.
channels_first
(logical(1)
)
The channel position of the given data (argument
data
). If TRUE
, the channel axis is placed at the second position
between the batch size and the rest of the input axes, e.g.,
c(10,3,32,32)
for a batch of ten images with three channels and a
height and width of 32 pixels. Otherwise (FALSE
), the channel axis
is at the last position, i.e., c(10,32,32,3)
. If the data
has no channel axis, use the default value TRUE
.
input_dim
(integer
)
The model input dimension excluding the batch
dimension. It can be specified as vector of integers, but has to be in
the format "channels first".
input_names
(character
, factor
or list
)
The input names of the model excluding the batch dimension. For a model
with a single input layer and input axis (e.g., for tabular data), the
input names can be specified as a character vector or factor, e.g.,
for a dense layer with 3 input features use c("X1", "X2", "X3")
. If
the model input consists of multiple axes (e.g., for signal and
image data), use a list of character vectors or factors for each axis
in the format "channels first", e.g., use
list(c("C1", "C2"), c("L1","L2","L3","L4","L5"))
for a 1D
convolutional input layer with signal length 4 and 2 channels.
Note: This argument is optional and otherwise the names are
generated automatically. But if this argument is set, all found
input names in the passed model will be disregarded.
output_names
(character
, factor
)
A character vector with the names for the output dimensions
excluding the batch dimension, e.g., for a model with 3 output nodes use
c("Y1", "Y2", "Y3")
. Instead of a character
vector you can also use a factor to set an order for the plots.
Note: This argument is optional and otherwise the names are
generated automatically. But if this argument is set, all found
output names in the passed model will be disregarded.