-
Notifications
You must be signed in to change notification settings - Fork 11
/
inductive_classification.jl
executable file
·227 lines (192 loc) · 8.65 KB
/
inductive_classification.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
"""
score(conf_model::ConformalProbabilisticSet, fitresult, X, y=nothing)
Generic score method for the [`ConformalProbabilisticSet`](@ref). It computes nonconformity scores using the heuristic function `h` and the softmax probabilities of the true class. Method is dispatched for different Conformal Probabilistic Sets and atomic models.
"""
function score(conf_model::ConformalProbabilisticSet, fitresult, X, y=nothing)
return score(conf_model, conf_model.model, fitresult, X, y)
end
"""
split_data(conf_model::ConformalProbabilisticSet, indices::Base.OneTo{Int})
Splits the data into a proper training and calibration set.
"""
function split_data(conf_model::ConformalProbabilisticSet, X, y)
train, calibration = partition(eachindex(y), conf_model.train_ratio)
Xtrain = selectrows(X, train)
ytrain = y[train]
Xcal = selectrows(X, calibration)
ycal = y[calibration]
return Xtrain, ytrain, Xcal, ycal
end
# Simple
"The `SimpleInductiveClassifier` is the simplest approach to Inductive Conformal Classification. Contrary to the [`NaiveClassifier`](@ref) it computes nonconformity scores using a designated calibration dataset."
mutable struct SimpleInductiveClassifier{Model<:Supervised} <: ConformalProbabilisticSet
model::Model
coverage::AbstractFloat
scores::Union{Nothing,Dict{Any,Any}}
heuristic::Function
train_ratio::AbstractFloat
end
function SimpleInductiveClassifier(
model::Supervised;
coverage::AbstractFloat=0.95,
heuristic::Function=minus_softmax,
train_ratio::AbstractFloat=0.5,
)
return SimpleInductiveClassifier(model, coverage, nothing, heuristic, train_ratio)
end
"""
score(conf_model::SimpleInductiveClassifier, ::Type{<:Supervised}, fitresult, X, y::Union{Nothing,AbstractArray}=nothing)
Score method for the [`SimpleInductiveClassifier`](@ref) dispatched for any `<:Supervised` model.
"""
function score(
conf_model::SimpleInductiveClassifier, atomic::Supervised, fitresult, X, y=nothing
)
p̂ = reformat_mlj_prediction(MMI.predict(atomic, fitresult, MMI.reformat(atomic, X)...))
L = p̂.decoder.classes
probas = pdf(p̂, L)
scores = @.(conf_model.heuristic(y, probas))
if isnothing(y)
return scores
else
cal_scores = getindex.(Ref(scores), 1:size(scores, 1), levelcode.(y))
return cal_scores, scores
end
end
@doc raw"""
MMI.fit(conf_model::SimpleInductiveClassifier, verbosity, X, y)
For the [`SimpleInductiveClassifier`](@ref) nonconformity scores are computed as follows:
``
S_i^{\text{CAL}} = s(X_i, Y_i) = h(\hat\mu(X_i), Y_i), \ i \in \mathcal{D}_{\text{calibration}}
``
A typical choice for the heuristic function is ``h(\hat\mu(X_i), Y_i)=1-\hat\mu(X_i)_{Y_i}`` where ``\hat\mu(X_i)_{Y_i}`` denotes the softmax output of the true class and ``\hat\mu`` denotes the model fitted on training data ``\mathcal{D}_{\text{train}}``. The simple approach only takes the softmax probability of the true label into account.
"""
function MMI.fit(conf_model::SimpleInductiveClassifier, verbosity, X, y)
# Data Splitting:
Xtrain, ytrain, Xcal, ycal = split_data(conf_model, X, y)
# Training:
fitresult, cache, report = MMI.fit(
conf_model.model, verbosity, MMI.reformat(conf_model.model, Xtrain, ytrain)...
)
# Nonconformity Scores:
cal_scores, scores = score(conf_model, fitresult, Xcal, ycal)
conf_model.scores = Dict(:calibration => cal_scores, :all => scores)
return (fitresult, cache, report)
end
@doc raw"""
MMI.predict(conf_model::SimpleInductiveClassifier, fitresult, Xnew)
For the [`SimpleInductiveClassifier`](@ref) prediction sets are computed as follows,
``
\hat{C}_{n,\alpha}(X_{n+1}) = \left\{y: s(X_{n+1},y) \le \hat{q}_{n, \alpha}^{+} \{S_i^{\text{CAL}}\} \right\}, \ i \in \mathcal{D}_{\text{calibration}}
``
where ``\mathcal{D}_{\text{calibration}}`` denotes the designated calibration data.
"""
function MMI.predict(conf_model::SimpleInductiveClassifier, fitresult, Xnew)
p̂ = reformat_mlj_prediction(
MMI.predict(conf_model.model, fitresult, MMI.reformat(conf_model.model, Xnew)...)
)
v = conf_model.scores[:calibration]
q̂ = qplus(v, conf_model.coverage)
p̂ = map(p̂) do pp
L = p̂.decoder.classes
probas = pdf.(pp, L)
is_in_set = 1.0 .- probas .<= q̂
if !all(is_in_set .== false)
pp = UnivariateFinite(L[is_in_set], probas[is_in_set])
else
pp = missing
end
return pp
end
return p̂
end
# Adaptive
"The `AdaptiveInductiveClassifier` is an improvement to the [`SimpleInductiveClassifier`](@ref) and the [`NaiveClassifier`](@ref). Contrary to the [`NaiveClassifier`](@ref) it computes nonconformity scores using a designated calibration dataset like the [`SimpleInductiveClassifier`](@ref). Contrary to the [`SimpleInductiveClassifier`](@ref) it utilizes the softmax output of all classes."
mutable struct AdaptiveInductiveClassifier{Model<:Supervised} <: ConformalProbabilisticSet
model::Model
coverage::AbstractFloat
scores::Union{Nothing,Dict{Any,Any}}
heuristic::Function
train_ratio::AbstractFloat
end
function AdaptiveInductiveClassifier(
model::Supervised;
coverage::AbstractFloat=0.95,
heuristic::Function=minus_softmax,
train_ratio::AbstractFloat=0.5,
)
return AdaptiveInductiveClassifier(model, coverage, nothing, heuristic, train_ratio)
end
@doc raw"""
MMI.fit(conf_model::AdaptiveInductiveClassifier, verbosity, X, y)
For the [`AdaptiveInductiveClassifier`](@ref) nonconformity scores are computed by cumulatively summing the ranked scores of each label in descending order until reaching the true label ``Y_i``:
``
S_i^{\text{CAL}} = s(X_i,Y_i) = \sum_{j=1}^k \hat\mu(X_i)_{\pi_j} \ \text{where } \ Y_i=\pi_k, i \in \mathcal{D}_{\text{calibration}}
``
"""
function MMI.fit(conf_model::AdaptiveInductiveClassifier, verbosity, X, y)
# Data Splitting:
Xtrain, ytrain, Xcal, ycal = split_data(conf_model, X, y)
# Training:
fitresult, cache, report = MMI.fit(
conf_model.model, verbosity, MMI.reformat(conf_model.model, Xtrain, ytrain)...
)
# Nonconformity Scores:
cal_scores, scores = score(conf_model, fitresult, Xcal, ycal)
conf_model.scores = Dict(:calibration => cal_scores, :all => scores)
return (fitresult, cache, report)
end
"""
score(conf_model::AdaptiveInductiveClassifier, ::Type{<:Supervised}, fitresult, X, y::Union{Nothing,AbstractArray}=nothing)
Score method for the [`AdaptiveInductiveClassifier`](@ref) dispatched for any `<:Supervised` model.
"""
function score(
conf_model::AdaptiveInductiveClassifier, atomic::Supervised, fitresult, X, y=nothing
)
p̂ = reformat_mlj_prediction(MMI.predict(atomic, fitresult, MMI.reformat(atomic, X)...))
L = p̂.decoder.classes
probas = pdf(p̂, L) # compute probabilities for all classes
scores = map(Base.Iterators.product(eachrow(probas), L)) do Z
probasᵢ, yₖ = Z
Π = sortperm(.-probasᵢ) # rank in descending order
πₖ = findall(L[Π] .== yₖ)[1] # index of true y in sorted array
scoresᵢ = last(cumsum(probasᵢ[Π][1:πₖ])) # sum up until true y is reached
return scoresᵢ
end
if isnothing(y)
return scores
else
cal_scores = getindex.(Ref(scores), 1:size(scores, 1), levelcode.(y))
return cal_scores, scores
end
end
@doc raw"""
MMI.predict(conf_model::AdaptiveInductiveClassifier, fitresult, Xnew)
For the [`AdaptiveInductiveClassifier`](@ref) prediction sets are computed as follows,
``
\hat{C}_{n,\alpha}(X_{n+1}) = \left\{y: s(X_{n+1},y) \le \hat{q}_{n, \alpha}^{+} \{S_i^{\text{CAL}}\} \right\}, i \in \mathcal{D}_{\text{calibration}}
``
where ``\mathcal{D}_{\text{calibration}}`` denotes the designated calibration data.
"""
function MMI.predict(conf_model::AdaptiveInductiveClassifier, fitresult, Xnew)
p̂ = reformat_mlj_prediction(
MMI.predict(conf_model.model, fitresult, MMI.reformat(conf_model.model, Xnew)...)
)
v = conf_model.scores[:calibration]
q̂ = qplus(v, conf_model.coverage)
p̂ = map(p̂) do pp
L = p̂.decoder.classes
probas = pdf.(pp, L)
Π = sortperm(.-probas) # rank in descending order
in_set = findall(cumsum(probas[Π]) .> q̂)
if length(in_set) > 0
k = in_set[1] # index of first class with probability > q̂ (supremum)
else
k = 0
end
k += 1
final_idx = minimum([k, length(Π)])
pp = UnivariateFinite(L[Π][1:final_idx], probas[Π][1:final_idx])
return pp
end
return p̂
end