-
Notifications
You must be signed in to change notification settings - Fork 6
/
growing_spheres.jl
260 lines (207 loc) · 9.47 KB
/
growing_spheres.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using LinearAlgebra: LinearAlgebra
using Random: Random
"Growing Spheres counterfactual generator class."
mutable struct GrowingSpheresGenerator <: AbstractNonGradientBasedGenerator
n::Union{Nothing,Integer}
η::Union{Nothing,AbstractFloat}
latent_space::Bool
dim_reduction::Bool
end
"""
GrowingSpheresGenerator(; n::Int=100, η::Float64=0.1, kwargs...)
Constructs a new Growing Spheres Generator object.
"""
function GrowingSpheresGenerator(;
n::Union{Nothing,Integer}=100, η::Union{Nothing,AbstractFloat}=0.1
)
return GrowingSpheresGenerator(n, η, false, false)
end
"""
growing_spheres_generation(ce::AbstractCounterfactualExplanation)
Generate counterfactual candidates using the growing spheres generation algorithm.
# Arguments
- `ce::AbstractCounterfactualExplanation`: An instance of the `AbstractCounterfactualExplanation` type representing the counterfactual explanation.
# Returns
- `nothing`
This function applies the growing spheres generation algorithm to generate counterfactual candidates. It starts by generating random points uniformly on a sphere, gradually reducing the search space until no counterfactuals are found. Then it expands the search space until at least one counterfactual is found or the maximum number of iterations is reached.
The algorithm iteratively generates counterfactual candidates and predicts their labels using the model stored in `ce.M`. It checks if any of the predicted labels are different from the factual class. The process of reducing the search space involves halving the search radius, while the process of expanding the search space involves increasing the search radius.
"""
function growing_spheres_generation!(ce::AbstractCounterfactualExplanation)
generator = ce.generator
model = ce.M
factual = ce.x
counterfactual_data = ce.data
target = [ce.target]
max_iter = 1000
# Copy hyperparameters
n = generator.n
η = convert(eltype(factual), generator.η)
# Generate random points uniformly on a sphere
counterfactual_candidates = hyper_sphere_coordinates(n, factual, 0.0, η)
if (factual == target)
ce.s′ = factual
return nothing
end
# Predict labels for each candidate counterfactual
counterfactual = find_counterfactual(
model, target, counterfactual_data, counterfactual_candidates
)
# Repeat until there's no counterfactual points (process of removing all counterfactuals by reducing the search space)
while (!isnothing(counterfactual))
η /= 2
a₀ = convert(eltype(factual), 0.0)
counterfactual_candidates = hyper_sphere_coordinates(n, factual, a₀, η)
counterfactual = find_counterfactual(
model, target, counterfactual_data, counterfactual_candidates
)
max_iter -= 1
if max_iter == 0
break
end
end
# Update path
ce.search[:iteration_count] += n
for i in eachindex(counterfactual_candidates[1, :])
push!(ce.search[:path], reshape(counterfactual_candidates[:, i], :, 1))
end
# Initialize boundaries of the sphere's radius
a₀, a₁ = η, 2η
# Repeat until there's at least one counterfactual (process of expanding the search space)
while (isnothing(counterfactual))
a₀ = a₁
a₁ += η
counterfactual_candidates = hyper_sphere_coordinates(n, factual, a₀, a₁)
counterfactual = find_counterfactual(
model, target, counterfactual_data, counterfactual_candidates
)
max_iter -= 1
if max_iter == 0
break
end
end
# Update path
ce.search[:iteration_count] += n
for i in eachindex(counterfactual_candidates[1, :])
push!(ce.search[:path], reshape(counterfactual_candidates[:, i], :, 1))
end
ce.s′ = counterfactual_candidates[:, counterfactual]
return nothing
end
"""
feature_selection!(ce::AbstractCounterfactualExplanation)
Perform feature selection to find the dimension with the closest (but not equal) values between the `ce.x` (factual) and `ce.s′` (counterfactual) arrays.
# Arguments
- `ce::AbstractCounterfactualExplanation`: An instance of the `AbstractCounterfactualExplanation` type representing the counterfactual explanation.
# Returns
- `nothing`
The function iteratively modifies the `ce.s′` counterfactual array by updating its elements to match the corresponding elements in the `ce.x` factual array, one dimension at a time, until the predicted label of the modified `ce.s′` matches the predicted label of the `ce.x` array.
"""
function feature_selection!(ce::AbstractCounterfactualExplanation)
model = ce.M
counterfactual_data = ce.data
factual = ce.x
target = [ce.target]
# Assign the initial counterfactual to both counterfactual′ and counterfactual″
counterfactual′ = ce.s′
counterfactual″ = ce.s′
factual_class = CounterfactualExplanations.Models.predict_label(
model, counterfactual_data, factual
)[1]
while (
factual_class != CounterfactualExplanations.Models.predict_label(
model, counterfactual_data, counterfactual′
) &&
target == CounterfactualExplanations.Models.predict_label(
model, counterfactual_data, counterfactual′
)
)
counterfactual″ = counterfactual′
i = find_closest_dimension(factual, counterfactual′)
counterfactual′[i] = factual[i]
ce.search[:iteration_count] += 1
push!(ce.search[:path], reshape(counterfactual″, :, 1))
end
ce.s′ = counterfactual″
return nothing
end
"""
hyper_sphere_coordinates(n_search_samples::Int, instance::Vector{Float64}, low::Int, high::Int; p_norm::Int=2)
Generates candidate counterfactuals using the growing spheres method based on hyper-sphere coordinates.
The implementation follows the Random Point Picking over a sphere algorithm described in the paper:
"Learning Counterfactual Explanations for Tabular Data" by Pawelczyk, Broelemann & Kascneci (2020),
presented at The Web Conference 2020 (WWW). It ensures that points are sampled uniformly at random
using insights from: http://mathworld.wolfram.com/HyperspherePointPicking.html
The growing spheres method is originally proposed in the paper:
"Comparison-based Inverse Classification for Interpretability in Machine Learning" by Thibaut Laugel et al (2018),
presented at the International Conference on Information Processing and Management of Uncertainty in Knowledge-Based Systems (2018).
# Arguments
- `n_search_samples::Int`: The number of search samples (int > 0).
- `instance::AbstractArray`: The input point array.
- `low::AbstractFloat`: The lower bound (float >= 0, l < h).
- `high::AbstractFloat`: The upper bound (float >= 0, h > l).
- `p_norm::Integer`: The norm parameter (int >= 1).
# Returns
- `candidate_counterfactuals::Array`: An array of candidate counterfactuals.
"""
function hyper_sphere_coordinates(
n_search_samples::Integer,
instance::AbstractArray,
low::AbstractFloat,
high::AbstractFloat;
p_norm::Integer=2,
)
delta_instance = Random.randn(n_search_samples, length(instance))
delta_instance = convert.(eltype(instance), delta_instance)
# length range [l, h)
dist = Random.rand(n_search_samples) .* (high - low) .+ low
norm_p = LinearAlgebra.norm(delta_instance, p_norm)
# rescale/normalize factor
d_norm = dist ./ norm_p
delta_instance .= delta_instance .* d_norm
instance_matrix = repeat(reshape(instance, 1, length(instance)), n_search_samples)
candidate_counterfactuals = instance_matrix + delta_instance
return transpose(candidate_counterfactuals)
end
"""
find_counterfactual(model, factual_class, counterfactual_data, counterfactual_candidates)
Find the first counterfactual index by predicting labels.
# Arguments
- `model`: The fitted model used for prediction.
- `target_class`: Expected target class.
- `counterfactual_data`: Data required for counterfactual generation.
- `counterfactual_candidates`: The array of counterfactual candidates.
# Returns
- `counterfactual`: The index of the first counterfactual found.
"""
function find_counterfactual(
model, target_class, counterfactual_data, counterfactual_candidates
)
predicted_labels = map(
e -> CounterfactualExplanations.Models.predict_label(model, counterfactual_data, e),
eachcol(counterfactual_candidates),
)
counterfactual = findfirst(predicted_labels .== target_class)
return counterfactual
end
"""
find_closest_dimension(factual, counterfactual)
Find the dimension with the closest (but not equal) values between the `factual` and `counterfactual` arrays.
# Arguments
- `factual`: The factual array.
- `counterfactual`: The counterfactual array.
# Returns
- `closest_dimension`: The index of the dimension with the closest values.
The function iterates over the indices of the `factual` array and calculates the absolute difference between the corresponding elements in the `factual` and `counterfactual` arrays. It returns the index of the dimension with the smallest difference, excluding dimensions where the values in `factual` and `counterfactual` are equal.
"""
function find_closest_dimension(factual, counterfactual)
min_diff = typemax(eltype(factual))
closest_dimension = -1
for i in eachindex(factual)
diff = abs(factual[i] - counterfactual[i])
if diff < min_diff && factual[i] != counterfactual[i]
min_diff = diff
closest_dimension = i
end
end
return closest_dimension
end