'SmoothGrad' was introduced by D. Smilkov et al. (2017) and is an extension to the classical Vanilla Gradient method. It takes the mean of the gradients for n perturbations of each data point, i.e. with $$\epsilon \sim N(0,\sigma)$$ $$1/n \sum_n d f(x+ \epsilon)_j / d x_j.$$

## References

D. Smilkov et al. (2017) SmoothGrad: removing noise by adding noise. CoRR, abs/1706.03825

## Super classes

innsight::InterpretingMethod -> innsight::GradientBased -> SmoothGrad

## Public fields

n

Number of perturbations of the input data (default: $$50$$).

noise_level

The standard deviation of the Gaussian perturbation, i.e. $$\sigma = (max(x) - min(x)) *$$ noise_level.

## Methods

### Public methods

Inherited methods

### Method new()

Create a new instance of the SmoothGrad method.

#### Arguments

deep

Whether to make a deep clone.

## Examples

# ------------------------- Example 1: Torch -------------------------------
library(torch)

# Create nn_sequential model and data
model <- nn_sequential(
nn_linear(5, 10),
nn_relu(),
nn_linear(10, 2),
nn_sigmoid()
)
data <- torch_randn(25, 5)

# Create Converter
converter <- Converter$new(model, input_dim = c(5)) # Calculate the smoothed Gradients smoothgrad <- SmoothGrad$new(converter, data)
#>
|
|                                                                      |   0%
|
|===================================                                   |  50%
|
|======================================================================| 100%

# Print the result as a data.frame for first 5 rows
smoothgrad$get_result("data.frame")[1:5, ] #> data feature class value #> 1 data_1 X1 Y1 -0.1037277 #> 2 data_2 X1 Y1 -0.1112626 #> 3 data_3 X1 Y1 -0.1047125 #> 4 data_4 X1 Y1 -0.1354966 #> 5 data_5 X1 Y1 -0.1952363 # Plot the result for both classes plot(smoothgrad, output_idx = 1:2) # Plot the boxplot of all datapoints boxplot(smoothgrad, output_idx = 1:2) # ------------------------- Example 2: Neuralnet --------------------------- library(neuralnet) data(iris) # Train a neural network nn <- neuralnet(Species ~ ., iris, linear.output = FALSE, hidden = c(10, 5), act.fct = "logistic", rep = 1 ) # Convert the trained model converter <- Converter$new(nn)

smoothgrad <- SmoothGrad$new(converter, iris[, -5], times_input = FALSE) #> Backward pass 'SmoothGrad': #> | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100% # Plot the result for the first and 60th data point and all classes plot(smoothgrad, data_idx = c(1, 60), output_idx = 1:3) # Calculate SmoothGrad x Input and do not ignore the last activation smoothgrad <- SmoothGrad$new(converter, iris[, -5], ignore_last_act = FALSE)
#>
|
|                                                                      |   0%
|
|=======================                                               |  33%
|
|===============================================                       |  67%
|
|======================================================================| 100%

# Plot the result again
plot(smoothgrad, data_idx = c(1, 60), output_idx = 1:3)

# ------------------------- Example 3: Keras -------------------------------
library(keras)

if (is_keras_available()) {
data <- array(rnorm(64 * 60 * 3), dim = c(64, 60, 3))

model <- keras_model_sequential()
model %>%
layer_conv_1d(
input_shape = c(60, 3), kernel_size = 8, filters = 8,
activation = "softplus", padding = "valid"
) %>%
layer_conv_1d(
kernel_size = 8, filters = 4, activation = "tanh",
) %>%
layer_conv_1d(
kernel_size = 4, filters = 2, activation = "relu",
) %>%
layer_flatten() %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 3, activation = "softmax")

# Convert the model
converter <- Converter$new(model) # Apply the SmoothGrad method smoothgrad <- SmoothGrad$new(converter, data, channels_first = FALSE)

# Plot the result for the first datapoint and all classes

# Plot the result as boxplots for first two classes

# You can also create an interactive plot with plotly.
# This is a suggested package, so make sure that it is installed
library(plotly)

# Result as boxplots

# Result of the second data point
plot(smoothgrad, data_idx = 2, as_plotly = TRUE)
}
#>
|
|                                                                      |   0%
|
|=======================                                               |  33%
|
|===============================================                       |  67%
|
|======================================================================| 100%

# If you want to create an interactive plot of your results with custom
# changes, you can take use of the method plotly::ggplotly
library(ggplot2)
library(plotly)
library(neuralnet)
data(iris)

nn <- neuralnet(Species ~ .,
iris,
linear.output = FALSE,
hidden = c(10, 8), act.fct = "tanh", rep = 1, threshold = 0.5
)
# create an converter for this model
converter <- Converter$new(nn) # create new instance of 'SmoothGrad' smoothgrad <- SmoothGrad$new(converter, iris[, -5])
#>
|
|                                                                      |   0%
|
|=======================                                               |  33%
|
|===============================================                       |  67%
|
|======================================================================| 100%

library(plotly)

{"x":{"data":[{"x":[2.7,2.7,3.3,3.3,2.7],"y":[0,-0.485071629285812,-0.485071629285812,0,0],"text":"<b><\/br> Gradient : -0.4851 <\/b><br /> <\/br> Datapoint:  data_1 <\/br> Output:       setosa <\/br> Feature:      Petal.Length","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(0,255,0,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.7,0.7,1.3,1.3,0.7],"y":[0,0.00733345979824662,0.00733345979824662,0,0],"text":"<b><\/br> Gradient : 0.007333 <\/b><br /> <\/br> Datapoint:  data_1 <\/br> Output:       setosa <\/br> Feature:      Sepal.Length","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(3,1,6,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[1.7,1.7,2.3,2.3,1.7],"y":[0,0.374823272228241,0.374823272228241,0,0],"text":"<b><\/br> Gradient : 0.3748 <\/b><br /> <\/br> Datapoint:  data_1 <\/br> Output:       setosa <\/br> Feature:      Sepal.Width","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(34,13,191,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[3.7,3.7,4.3,4.3,3.7],"y":[0,0.382453113794327,0.382453113794327,0,0],"text":"<b><\/br> Gradient : 0.3825 <\/b><br /> <\/br> Datapoint:  data_1 <\/br> Output:       setosa <\/br> Feature:      Petal.Width","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(33,13,195,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[2.7,2.7,3.3,3.3,2.7],"y":[0,-0.473034411668777,-0.473034411668777,0,0],"text":"<b><\/br> Gradient : -0.473 <\/b><br /> <\/br> Datapoint:  data_2 <\/br> Output:       setosa <\/br> Feature:      Petal.Length","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(0,255,0,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.7,0.7,1.3,1.3,0.7],"y":[0,0.0491398572921753,0.0491398572921753,0,0],"text":"<b><\/br> Gradient : 0.04914 <\/b><br /> <\/br> Datapoint:  data_2 <\/br> Output:       setosa <\/br> Feature:      Sepal.Length","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(18,7,29,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[3.7,3.7,4.3,4.3,3.7],"y":[0,0.207739561796188,0.207739561796188,0,0],"text":"<b><\/br> Gradient : 0.2077 <\/b><br /> <\/br> Datapoint:  data_2 <\/br> Output:       setosa <\/br> Feature:      Petal.Width","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(35,17,104,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[1.7,1.7,2.3,2.3,1.7],"y":[0,0.365683317184448,0.365683317184448,0,0],"text":"<b><\/br> Gradient : 0.3657 <\/b><br /> <\/br> Datapoint:  data_2 <\/br> Output:       setosa <\/br> Feature:      Sepal.Width","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(34,13,191,1)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y2","hoverinfo":"text","frame":null},{"x":[0.4,4.6],"y":[0,0],"text":"","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"rgba(0,0,0,1)","dash":"solid"},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0.4,4.6],"y":[0,0],"text":"","type":"scatter","mode":"lines","line":{"width":1.88976377952756,"color":"rgba(0,0,0,1)","dash":"solid"},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y2","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":34.9954337899543,"r":18.9954337899543,"b":37.2602739726027,"l":54.7945205479452},"plot_bgcolor":"rgba(255,255,255,1)","paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":14.6118721461187},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[0.4,4.6],"tickmode":"array","ticktext":["Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"],"tickvals":[1,2,3,4],"categoryorder":"array","categoryarray":["Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"],"nticks":null,"ticks":"outside","tickcolor":"rgba(51,51,51,1)","ticklen":3.65296803652968,"tickwidth":0.66417600664176,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.689497716895},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176,"zeroline":false,"anchor":"y2","title":"","hoverformat":".2f"},"annotations":[{"text":"Feature","x":0.5,"y":0,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":14.6118721461187},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"top","annotationType":"axis","yshift":-21.9178082191781},{"text":"Gradient","x":0,"y":0.5,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":14.6118721461187},"xref":"paper","yref":"paper","textangle":-90,"xanchor":"right","yanchor":"center","annotationType":"axis","xshift":-39.4520547945205},{"text":"setosa","x":0.5,"y":1,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":11.689497716895},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"bottom"},{"text":"data_1","x":1,"y":0.755707762557078,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":11.689497716895},"xref":"paper","yref":"paper","textangle":90,"xanchor":"left","yanchor":"middle"},{"text":"data_2","x":1,"y":0.244292237442922,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgba(26,26,26,1)","family":"","size":11.689497716895},"xref":"paper","yref":"paper","textangle":90,"xanchor":"left","yanchor":"middle"}],"yaxis":{"domain":[0.511415525114155,1],"automargin":true,"type":"linear","autorange":false,"range":[-0.528447866439819,0.425829350948334],"tickmode":"array","ticktext":["-0.50","-0.25","0.00","0.25"],"tickvals":[-0.5,-0.25,0,0.25],"categoryorder":"array","categoryarray":["-0.50","-0.25","0.00","0.25"],"nticks":null,"ticks":"outside","tickcolor":"rgba(51,51,51,1)","ticklen":3.65296803652968,"tickwidth":0.66417600664176,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.689497716895},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176,"zeroline":false,"anchor":"x","title":"","hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":"transparent","line":{"color":"rgba(51,51,51,1)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0.511415525114155,"y1":1},{"type":"rect","fillcolor":"rgba(217,217,217,1)","line":{"color":"rgba(51,51,51,1)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":23.37899543379,"yanchor":1,"ysizemode":"pixel"},{"type":"rect","fillcolor":"rgba(217,217,217,1)","line":{"color":"rgba(51,51,51,1)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","y0":0.511415525114155,"y1":1,"x0":0,"x1":23.37899543379,"xanchor":1,"xsizemode":"pixel"},{"type":"rect","fillcolor":"transparent","line":{"color":"rgba(51,51,51,1)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":0.488584474885845},{"type":"rect","fillcolor":"rgba(217,217,217,1)","line":{"color":"rgba(51,51,51,1)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","y0":0,"y1":0.488584474885845,"x0":0,"x1":23.37899543379,"xanchor":1,"xsizemode":"pixel"}],"yaxis2":{"type":"linear","autorange":false,"range":[-0.514970298111439,0.40761920362711],"tickmode":"array","ticktext":["-0.50","-0.25","0.00","0.25"],"tickvals":[-0.5,-0.25,0,0.25],"categoryorder":"array","categoryarray":["-0.50","-0.25","0.00","0.25"],"nticks":null,"ticks":"outside","tickcolor":"rgba(51,51,51,1)","ticklen":3.65296803652968,"tickwidth":0.66417600664176,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.689497716895},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0,0.488584474885845],"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176,"zeroline":false,"anchor":"x","title":"","hoverformat":".2f"},"showlegend":false,"legend":{"bgcolor":"rgba(255,255,255,1)","bordercolor":"transparent","borderwidth":1.88976377952756,"font":{"color":"rgba(0,0,0,1)","family":"","size":11.689497716895},"title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":14.6118721461187}}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"37c31871b52c":{"xmin":{},"xmax":{},"ymin":{},"ymax":{},"fill":{},"text":{},"type":"scatter"},"37c36f652f99":{"yintercept":{}}},"cur_data":"37c31871b52c","visdat":{"37c31871b52c":["function (y) ","x"],"37c36f652f99":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}