From 33c24a7a22df32d8fa1dc258b5899325b259f6a2 Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 16:57:32 +0100 Subject: [PATCH 1/7] Fix default noise level for NoiseAugmentation Breaking: rename kwarg to `std`. --- src/input_augmentation.jl | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/input_augmentation.jl b/src/input_augmentation.jl index a99edbe..e3c459b 100644 --- a/src/input_augmentation.jl +++ b/src/input_augmentation.jl @@ -83,11 +83,20 @@ function augment_indices(inds::Vector{CartesianIndex{N}}, n) where {N} end """ - NoiseAugmentation(analyzer, n, [std=1, rng=GLOBAL_RNG]) - NoiseAugmentation(analyzer, n, distribution, [rng=GLOBAL_RNG]) + NoiseAugmentation(analyzer, n) + NoiseAugmentation(analyzer, n, std::Real) + NoiseAugmentation(analyzer, n, distribution::Sampleable) -A wrapper around analyzers that augments the input with `n` samples of additive noise sampled from `distribution`. +A wrapper around analyzers that augments the input with `n` samples of additive noise sampled from a scalar `distribution`. This input augmentation is then averaged to return an `Explanation`. + +Defaults to the normal distribution `Normal(0, std^2)` with `std=1`. +For optimal results, $REF_SMILKOV_SMOOTHGRAD recommends setting `std` between 10% and 20% of the input range of every sample, +e.g. `std = 0.1 * (maximum(input) - minimum(input))`. + +## Keyword arguments +- `rng::AbstractRNG`: Specify the random number generator that is used to sample noise from the `distribution`. + Defaults to `GLOBAL_RNG`. """ struct NoiseAugmentation{A<:AbstractXAIMethod,D<:Sampleable,R<:AbstractRNG} <: AbstractXAIMethod @@ -96,11 +105,11 @@ struct NoiseAugmentation{A<:AbstractXAIMethod,D<:Sampleable,R<:AbstractRNG} <: distribution::D rng::R end -function NoiseAugmentation(analyzer, n, distr::Sampleable, rng=GLOBAL_RNG) - return NoiseAugmentation(analyzer, n, distr::Sampleable, rng) +function NoiseAugmentation(analyzer, n, distribution::Sampleable, rng=GLOBAL_RNG) + return NoiseAugmentation(analyzer, n, distribution::Sampleable, rng) end -function NoiseAugmentation(analyzer, n, σ::Real=0.1f0, args...) - return NoiseAugmentation(analyzer, n, Normal(0.0f0, Float32(σ)^2), args...) +function NoiseAugmentation(analyzer, n, std::T=1.0f0, rng=GLOBAL_RNG) where {T<:Real} + return NoiseAugmentation(analyzer, n, Normal(zero(T), std^2), rng) end function call_analyzer(input, aug::NoiseAugmentation, ns::AbstractOutputSelector; kwargs...) From 6e7161dbd68b9aeb8b97025f6e586691ff6bf26c Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 16:57:40 +0100 Subject: [PATCH 2/7] Fix typo --- Project.toml | 2 +- src/input_augmentation.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index ea22585..b416712 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ExplainableAI" uuid = "4f1bc3e1-d60d-4ed0-9367-9bdff9846d3b" authors = ["Adrian Hill "] -version = "0.9.0" +version = "0.10.0-DEV" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" diff --git a/src/input_augmentation.jl b/src/input_augmentation.jl index e3c459b..1535fa1 100644 --- a/src/input_augmentation.jl +++ b/src/input_augmentation.jl @@ -56,7 +56,7 @@ end """ augment_indices(indices, n) -Strip batch indices and return inidices for batch augmented by n samples. +Strip batch indices and return indices for batch augmented by n samples. ## Example ```julia-repl From 4352c9da886cccd68da92bb3b409fdfac101003b Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 17:02:22 +0100 Subject: [PATCH 3/7] Revert breaking version renamed arg was optional, not a kwargs --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b416712..2812014 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ExplainableAI" uuid = "4f1bc3e1-d60d-4ed0-9367-9bdff9846d3b" authors = ["Adrian Hill "] -version = "0.10.0-DEV" +version = "0.9.1-DEV" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 77ff6a72da53cea26ceafaf87e0a1101d4db3a64 Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 17:09:00 +0100 Subject: [PATCH 4/7] Improve docstring --- src/input_augmentation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_augmentation.jl b/src/input_augmentation.jl index 1535fa1..77f8bac 100644 --- a/src/input_augmentation.jl +++ b/src/input_augmentation.jl @@ -91,7 +91,7 @@ A wrapper around analyzers that augments the input with `n` samples of additive This input augmentation is then averaged to return an `Explanation`. Defaults to the normal distribution `Normal(0, std^2)` with `std=1`. -For optimal results, $REF_SMILKOV_SMOOTHGRAD recommends setting `std` between 10% and 20% of the input range of every sample, +For optimal results, $REF_SMILKOV_SMOOTHGRAD recommends setting `std` between 10% and 20% of the input range of each sample, e.g. `std = 0.1 * (maximum(input) - minimum(input))`. ## Keyword arguments From 3173ae184221e894cd2fa2118fdec6a342e898b7 Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 17:10:24 +0100 Subject: [PATCH 5/7] Fix SmoothGrad docstring --- src/gradient.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gradient.jl b/src/gradient.jl index 58577f0..8e6d4b5 100644 --- a/src/gradient.jl +++ b/src/gradient.jl @@ -75,8 +75,8 @@ function call_analyzer( end """ - SmoothGrad(analyzer, [n=50, std=0.1, rng=GLOBAL_RNG]) - SmoothGrad(analyzer, [n=50, distribution=Normal(0, σ²=0.01), rng=GLOBAL_RNG]) + SmoothGrad(analyzer, [n=50, std=1.0, rng=GLOBAL_RNG]) + SmoothGrad(analyzer, [n=50, distribution=Normal(0, 1), rng=GLOBAL_RNG]) Analyze model by calculating a smoothed sensitivity map. This is done by averaging sensitivity maps of a `Gradient` analyzer over random samples From da08fbd9d08ab2b5c75e8c4d4691ba2b1c67d00d Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 17:10:54 +0100 Subject: [PATCH 6/7] Revert to breaking release --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2812014..b416712 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ExplainableAI" uuid = "4f1bc3e1-d60d-4ed0-9367-9bdff9846d3b" authors = ["Adrian Hill "] -version = "0.9.1-DEV" +version = "0.10.0-DEV" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" From 1cbcca2133dbced32bbdee2e3dc32191bdd5e076 Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Dec 2024 17:13:20 +0100 Subject: [PATCH 7/7] Indicate default `Float32` eltype in docstrings --- src/gradient.jl | 4 ++-- src/input_augmentation.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gradient.jl b/src/gradient.jl index 8e6d4b5..fd90726 100644 --- a/src/gradient.jl +++ b/src/gradient.jl @@ -75,8 +75,8 @@ function call_analyzer( end """ - SmoothGrad(analyzer, [n=50, std=1.0, rng=GLOBAL_RNG]) - SmoothGrad(analyzer, [n=50, distribution=Normal(0, 1), rng=GLOBAL_RNG]) + SmoothGrad(analyzer, [n=50, std=1.0f0, rng=GLOBAL_RNG]) + SmoothGrad(analyzer, [n=50, distribution=Normal(0.0f0, 1.0f0), rng=GLOBAL_RNG]) Analyze model by calculating a smoothed sensitivity map. This is done by averaging sensitivity maps of a `Gradient` analyzer over random samples diff --git a/src/input_augmentation.jl b/src/input_augmentation.jl index 77f8bac..02a6675 100644 --- a/src/input_augmentation.jl +++ b/src/input_augmentation.jl @@ -90,7 +90,7 @@ end A wrapper around analyzers that augments the input with `n` samples of additive noise sampled from a scalar `distribution`. This input augmentation is then averaged to return an `Explanation`. -Defaults to the normal distribution `Normal(0, std^2)` with `std=1`. +Defaults to the normal distribution `Normal(0, std^2)` with `std=1.0f0`. For optimal results, $REF_SMILKOV_SMOOTHGRAD recommends setting `std` between 10% and 20% of the input range of each sample, e.g. `std = 0.1 * (maximum(input) - minimum(input))`.