Computes integrated gradients scores for model and an input sequence. This can be used to visualize what part of the input is import for the models decision. Code is R implementation of python code from here. Tensorflow implementation is based on this paper.
Usage
integrated_gradients(
m_steps = 50,
baseline_type = "zero",
input_seq,
target_class_idx,
model,
pred_stepwise = FALSE,
num_baseline_repeats = 1
)
Arguments
- m_steps
Number of steps between baseline and original input.
- baseline_type
Baseline sequence, either
"zero"
for all zeros or"shuffle"
for random permutation ofinput_seq
.- input_seq
Input tensor.
- target_class_idx
Index of class to compute gradient for
- model
Model to compute gradient for.
- pred_stepwise
Whether to do predictions with batch size 1 rather than all at once. Can be used if input is too big to handle at once. Only supported for single input layer.
- num_baseline_repeats
Number of different baseline estimations if baseline_type is
"shuffle"
(estimate integrated gradient repeatedly for different shuffles). Final result is average ofnum_baseline
single calculations.
Examples
if (FALSE) { # reticulate::py_module_available("tensorflow")
library(reticulate)
model <- create_model_lstm_cnn(layer_lstm = 8, layer_dense = 3, maxlen = 20, verbose = FALSE)
random_seq <- sample(0:3, 20, replace = TRUE)
input_seq <- array(keras::to_categorical(random_seq), dim = c(1, 20, 4))
integrated_gradients(
input_seq = input_seq,
target_class_idx = 3,
model = model)
}