# All functions and types

## Exported functions

CounterfactualExplanations.generate_counterfactualMethod
generate_counterfactual(
x::Union{AbstractArray,Int}, target::Union{AbstractFloat,Int}, data::CounterfactualData, M::Models.AbstractFittedModel, generator::AbstractGenerator;
γ::AbstractFloat=0.75, T=1000
)

The core function that is used to run counterfactual search for a given factual x, target, counterfactual data, model and generator. Keywords can be used to specify the desired threshold for the predicted target class probability and the maximum number of iterations.

Examples

Generic generator

using CounterfactualExplanations

# Data:
using CounterfactualExplanations.Data
using Random
Random.seed!(1234)
xs, ys = Data.toy_data_linear()
X = hcat(xs...)
counterfactual_data = CounterfactualData(X,ys')

# Model
using CounterfactualExplanations.Models: LogisticModel, probs
# Logit model:
w = [1.0 1.0] # true coefficients
b = 0
M = LogisticModel(w, [b])

# Randomly selected factual:
x = select_factual(counterfactual_data,rand(1:size(X)[2]))
y = round(probs(M, x)[1])
target = round(probs(M, x)[1])==0 ? 1 : 0

# Counterfactual search:
generator = GenericGenerator()
counterfactual = generate_counterfactual(x, target, counterfactual_data, M, generator)
source
CounterfactualExplanations.DataPreprocessing.CounterfactualDataMethod
CounterfactualData(
X::AbstractMatrix, y::AbstractMatrix;
mutability::Union{Vector{Symbol},Nothing}=nothing,
domain::Union{Any,Nothing}=nothing,
categorical::Union{Vector{Int},Nothing}=nothing,
continuous::Union{Vector{Int},Nothing}=nothing,
standardize::Bool=false
)

This outer constructor method prepares features X and labels y to be used with the package. Mutability and domain constraints can be added for the features. The function also accepts arguments that specify which features are categorical and which are continues. These arguments are currently not used.

Examples

using CounterfactualExplanations.Data
x, y = toy_data_linear()
X = hcat(x...)
counterfactual_data = CounterfactualData(X,y')
source
CounterfactualExplanations.Counterfactuals.target_probsFunction
target_probs(counterfactual_explanation::CounterfactualExplanation, x::Union{AbstractArray, Nothing}=nothing)

Returns the predicted probability of the target class for x. If x is nothing, the predicted probability corresponding to the counterfactual value is returned.

source
CounterfactualExplanations.Counterfactuals.update!Method
update!(counterfactual_explanation::CounterfactualExplanation)

An important subroutine that updates the counterfactual explanation. It takes a snapshot of the current counterfactual search state and passes it to the generator. Based on the current state the generator generates perturbations. Various constraints are then applied to the proposed vector of feature perturbations. Finally, the counterfactual search state is updated.

source
CounterfactualExplanations.Models.BayesianLogisticModelType
BayesianLogisticModel(μ::Matrix,Σ::Matrix)

Constructs a Bayesian logistic classifier based on maximum a posteriori (MAP) estimates μ (coefficients including constant term(s)) and Σ (covariance matrix).

Examples

using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = CounterfactualExplanations.Models.BayesianLogisticModel(μ, Σ);

source
CounterfactualExplanations.Models.LogisticModelType
LogisticModel(W::Matrix,b::AbstractArray)

Constructs a logistic classifier based on arrays containing coefficients w and constant terms b.

Examples

w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = CounterfactualExplanations.Models.LogisticModel(w, b);

source
CounterfactualExplanations.Models.logitsMethod
logits(M::BayesianLogisticModel, X::AbstractArray)

Computes logits as μ[1ᵀ Xᵀ]ᵀ.

Examples

using CounterfactualExplanations.Models
using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = BayesianLogisticModel(μ, Σ);
x = [1,1]
logits(M, x)
source
CounterfactualExplanations.Models.logitsMethod
logits(M::LogisticModel, X::AbstractArray)

Computes logits as WX+b.

Examples

using CounterfactualExplanations.Models
w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = LogisticModel(w, b);
x = [1,1]
logits(M, x)
source
CounterfactualExplanations.Models.probsMethod
probs(M::BayesianLogisticModel, X::AbstractArray)

Computes predictive probabilities using a Probit approximation.

Examples

using CounterfactualExplanations.Models
using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = BayesianLogisticModel(μ, Σ);
x = [1,1]
probs(M, x)
source
CounterfactualExplanations.Models.probsMethod
probs(M::LogisticModel, X::AbstractArray)

Computes predictive probabilities from logits as σ(WX+b) where 'σ' is the sigmoid function.

Examples

using CounterfactualExplanations.Models
w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = LogisticModel(w, b);
x = [1,1]
probs(M, x)
source
CounterfactualExplanations.Generators.CLUEGeneratorMethod
CLUEGenerator(
;
loss::Symbol=:logitbinarycrossentropy,
complexity::Function=norm,
λ::AbstractFloat=0.1,
opt::Any=Flux.Optimise.Descent(),
τ::AbstractFloat=1e-5
)

An outer constructor method that instantiates a CLUE generator.

Examples

generator = CLUEGenerator()
source
CounterfactualExplanations.Generators.DiCEGeneratorMethod
DiCEGenerator(
;
loss::Symbol=:logitbinarycrossentropy,
complexity::Function=norm,
λ::AbstractFloat=0.1,
opt::Any=Flux.Optimise.Descent(),
τ::AbstractFloat=1e-5
)

An outer constructor method that instantiates a generic generator.

Examples

generator = DiCEGenerator()
source
CounterfactualExplanations.Generators.GenericGeneratorMethod
GenericGenerator(
;
loss::Symbol=:logitbinarycrossentropy,
complexity::Function=norm,
λ::AbstractFloat=0.1,
opt::Any=Flux.Optimise.Descent(),
τ::AbstractFloat=1e-5
)

An outer constructor method that instantiates a generic generator.

Examples

generator = GenericGenerator()
source
CounterfactualExplanations.Generators.GreedyGeneratorMethod
GreedyGenerator(
;
loss::Symbol=:logitbinarycrossentropy,
ϵ::Union{AbstractFloat,Nothing}=nothing,
n::Union{Int,Nothing}=nothing
)

An outer constructor method that instantiates a greedy generator.

Examples

generator = GreedyGenerator()
source
CounterfactualExplanations.Generators.REVISEGeneratorMethod
REVISEGenerator(
;
loss::Symbol=:logitbinarycrossentropy,
complexity::Function=norm,
λ::AbstractFloat=0.1,
opt::Any=Flux.Optimise.Descent(),
τ::AbstractFloat=1e-5
)

An outer constructor method that instantiates a REVISE generator.

Examples

generator = REVISEGenerator()
source
CounterfactualExplanations.Generators.mutability_constraintsMethod
mutability_constraints(generator::AbstractGradientBasedGenerator, counterfactual_state::CounterfactualState.State)

The default method to return mutability constraints that are dependent on the current counterfactual search state. For generic gradient-based generators, no state-dependent constraints are added.

source
CounterfactualExplanations.Generators.mutability_constraintsMethod
mutability_constraints(generator::GreedyGenerator, counterfactual_state::CounterfactualState.State)

The default method to return search state dependent mutability constraints for a greedy generator. Features that have been perturbed n times already can no longer be perturbed.

source
Flux.Losses.binary_focal_lossMethod
binary_focal_loss(ŷ, y; agg=mean, γ=2, ϵ=eps(ŷ))

Return the binaryfocalloss The input, 'ŷ', is expected to be normalized (i.e. softmax output).

For γ == 0, the loss is mathematically equivalent to Losses.binarycrossentropy.

See also: Losses.focal_loss for multi-class setting

Example

julia> y = [0  1  0
1  0  1]
2×3 Matrix{Int64}:
0  1  0
1  0  1

julia> ŷ = [0.268941  0.5  0.268941
0.731059  0.5  0.731059]
2×3 Matrix{Float64}:
0.268941  0.5  0.268941
0.731059  0.5  0.731059

julia> Flux.binary_focal_loss(ŷ, y) ≈ 0.0728675615927385
true
Flux.Losses.binarycrossentropyMethod
binarycrossentropy(ŷ, y; agg = mean, ϵ = eps(ŷ))

Return the binary cross-entropy loss, computed as

agg(@.(-y * log(ŷ + ϵ) - (1 - y) * log(1 - ŷ + ϵ)))

Where typically, the prediction ŷ is given by the output of a sigmoid activation. The ϵ term is included to avoid infinity. Using logitbinarycrossentropy is recomended over binarycrossentropy for numerical stability.

Use label_smoothing to smooth the y value as preprocessing before computing the loss.

Examples

julia> y_bin = Bool[1,0,1]
3-element Vector{Bool}:
1
0
1

julia> y_prob = softmax(reshape(vcat(1:3, 3:5), 2, 3) .* 1f0)
2×3 Matrix{Float32}:
0.268941  0.5  0.268941
0.731059  0.5  0.731059

julia> Flux.binarycrossentropy(y_prob[2,:], y_bin)
0.43989f0

julia> all(p -> 0 < p < 1, y_prob[2,:])  # else DomainError
true

julia> y_hot = Flux.onehotbatch(y_bin, 0:1)
2×3 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
⋅  1  ⋅
1  ⋅  1

julia> Flux.crossentropy(y_prob, y_hot)
0.43989f0
Flux.Losses.crossentropyMethod
crossentropy(ŷ, y; dims = 1, ϵ = eps(ŷ), agg = mean)

Return the cross entropy between the given probability distributions; calculated as

agg(-sum(y .* log.(ŷ .+ ϵ); dims))

Cross entropy is typically used as a loss in multi-class classification, in which case the labels y are given in a one-hot format. dims specifies the dimension (or the dimensions) containing the class probabilities. The prediction ŷ is supposed to sum to one across dims, as would be the case with the output of a softmax operation.

For numerical stability, it is recommended to use logitcrossentropy rather than softmax followed by crossentropy .

Use label_smoothing to smooth the true labels as preprocessing before computing the loss.

Example

julia> y_label = Flux.onehotbatch([0, 1, 2, 1, 0], 0:2)
3×5 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
1  ⋅  ⋅  ⋅  1
⋅  1  ⋅  1  ⋅
⋅  ⋅  1  ⋅  ⋅

julia> y_model = softmax(reshape(-7:7, 3, 5) .* 1f0)
3×5 Matrix{Float32}:
0.0900306  0.0900306  0.0900306  0.0900306  0.0900306
0.244728   0.244728   0.244728   0.244728   0.244728
0.665241   0.665241   0.665241   0.665241   0.665241

julia> sum(y_model; dims=1)
1×5 Matrix{Float32}:
1.0  1.0  1.0  1.0  1.0

julia> Flux.crossentropy(y_model, y_label)
1.6076053f0

julia> 5 * ans ≈ Flux.crossentropy(y_model, y_label; agg=sum)
true

julia> y_smooth = Flux.label_smoothing(y_label, 0.15f0)
3×5 Matrix{Float32}:
0.9   0.05  0.05  0.05  0.9
0.05  0.9   0.05  0.9   0.05
0.05  0.05  0.9   0.05  0.05

julia> Flux.crossentropy(y_model, y_smooth)
1.5776052f0
Flux.Losses.dice_coeff_lossMethod
dice_coeff_loss(ŷ, y; smooth = 1)

Return a loss based on the dice coefficient. Used in the V-Net image segmentation architecture. The dice coefficient is similar to the F1_score. Loss calculated as:

1 - 2*sum(|ŷ .* y| + smooth) / (sum(ŷ.^2) + sum(y.^2) + smooth)

Example

julia> y_pred = [1.1, 2.1, 3.1];

julia> Flux.dice_coeff_loss(y_pred, 1:3)
0.000992391663909964

julia> 1 - Flux.dice_coeff_loss(y_pred, 1:3)  # ~ F1 score for image segmentation
0.99900760833609
Flux.Losses.focal_lossMethod
focal_loss(ŷ, y; dims=1, agg=mean, γ=2, ϵ=eps(ŷ))

Return the focal_loss which can be used in classification tasks with highly imbalanced classes. It down-weights well-classified examples and focuses on hard examples. The input, 'ŷ', is expected to be normalized (i.e. softmax output).

The modulating factor, γ, controls the down-weighting strength. For γ == 0, the loss is mathematically equivalent to Losses.crossentropy.

Example

julia> y = [1  0  0  0  1
0  1  0  1  0
0  0  1  0  0]
3×5 Matrix{Int64}:
1  0  0  0  1
0  1  0  1  0
0  0  1  0  0

julia> ŷ = softmax(reshape(-7:7, 3, 5) .* 1f0)
3×5 Matrix{Float32}:
0.0900306  0.0900306  0.0900306  0.0900306  0.0900306
0.244728   0.244728   0.244728   0.244728   0.244728
0.665241   0.665241   0.665241   0.665241   0.665241

julia> Flux.focal_loss(ŷ, y) ≈ 1.1277571935622628
true

See also: Losses.binary_focal_loss for binary (not one-hot) labels

Flux.Losses.hinge_lossMethod
hinge_loss(ŷ, y; agg = mean)

Return the hinge_loss given the prediction ŷ and true labels y (containing 1 or -1); calculated as

sum(max.(0, 1 .- ŷ .* y)) / size(y, 2)

Usually used with classifiers like Support Vector Machines. See also: squared_hinge_loss

Example

julia> y_true = [1, -1, 1, 1];

julia> y_pred = [0.1, 0.3, 1, 1.5];

julia> Flux.hinge_loss(y_pred, y_true)
0.55

julia> Flux.hinge_loss(y_pred[1], y_true[1]) != 0  # same sign but |ŷ| < 1
true

julia> Flux.hinge_loss(y_pred[end], y_true[end]) == 0  # same sign but |ŷ| >= 1
true

julia> Flux.hinge_loss(y_pred[2], y_true[2]) != 0 # opposite signs
true
Flux.Losses.huber_lossMethod
huber_loss(ŷ, y; δ = 1, agg = mean)

Return the mean of the Huber loss given the prediction ŷ and true values y.

             | 0.5 * |ŷ - y|^2,            for |ŷ - y| <= δ
Huber loss = |
|  δ * (|ŷ - y| - 0.5 * δ), otherwise

Example

julia> ŷ = [1.1, 2.1, 3.1];

julia> Flux.huber_loss(ŷ, 1:3)  # default δ = 1 > |ŷ - y|
0.005000000000000009

julia> Flux.huber_loss(ŷ, 1:3, δ=0.05)  # changes behaviour as |ŷ - y| > δ
0.003750000000000005
Flux.Losses.kldivergenceMethod
kldivergence(ŷ, y; agg = mean, ϵ = eps(ŷ))

Return the Kullback-Leibler divergence between the given probability distributions.

The KL divergence is a measure of how much one probability distribution is different from the other. It is always non-negative, and zero only when both the distributions are equal.

Example

julia> p1 = [1 0; 0 1]
2×2 Matrix{Int64}:
1  0
0  1

julia> p2 = fill(0.5, 2, 2)
2×2 Matrix{Float64}:
0.5  0.5
0.5  0.5

julia> Flux.kldivergence(p2, p1) ≈ log(2)
true

julia> Flux.kldivergence(p2, p1; agg = sum) ≈ 2log(2)
true

julia> Flux.kldivergence(p2, p2; ϵ = 0)  # about -2e-16 with the regulator
0.0

julia> Flux.kldivergence(p1, p2; ϵ = 0)  # about 17.3 with the regulator
Inf
Flux.Losses.label_smoothingMethod
label_smoothing(y::Union{Number, AbstractArray}, α; dims::Int=1)

Returns smoothed labels, meaning the confidence on label values are relaxed.

When y is given as one-hot vector or batch of one-hot, its calculated as

y .* (1 - α) .+ α / size(y, dims)

when y is given as a number or batch of numbers for binary classification, its calculated as

y .* (1 - α) .+ α / 2

in which case the labels are squeezed towards 0.5.

α is a number in interval (0, 1) called the smoothing factor. Higher the value of α larger the smoothing of y.

dims denotes the one-hot dimension, unless dims=0 which denotes the application of label smoothing to binary distributions encoded in a single number.

Example

julia> y = Flux.onehotbatch([1, 1, 1, 0, 1, 0], 0:1)
2×6 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
⋅  ⋅  ⋅  1  ⋅  1
1  1  1  ⋅  1  ⋅

julia> y_smoothed = Flux.label_smoothing(y, 0.2f0)
2×6 Matrix{Float32}:
0.1  0.1  0.1  0.9  0.1  0.9
0.9  0.9  0.9  0.1  0.9  0.1

julia> y_sim = softmax(y .* log(2f0))
2×6 Matrix{Float32}:
0.333333  0.333333  0.333333  0.666667  0.333333  0.666667
0.666667  0.666667  0.666667  0.333333  0.666667  0.333333

julia> y_dis = vcat(y_sim[2,:]', y_sim[1,:]')
2×6 Matrix{Float32}:
0.666667  0.666667  0.666667  0.333333  0.666667  0.333333
0.333333  0.333333  0.333333  0.666667  0.333333  0.666667

julia> Flux.crossentropy(y_sim, y) < Flux.crossentropy(y_sim, y_smoothed)
true

julia> Flux.crossentropy(y_dis, y) > Flux.crossentropy(y_dis, y_smoothed)
true
Flux.Losses.logitbinarycrossentropyMethod
logitbinarycrossentropy(ŷ, y; agg = mean)

Mathematically equivalent to binarycrossentropy(σ(ŷ), y) but is more numerically stable.

Examples

julia> y_bin = Bool[1,0,1];

julia> y_model = Float32[2, -1, pi]
3-element Vector{Float32}:
2.0
-1.0
3.1415927

julia> Flux.logitbinarycrossentropy(y_model, y_bin)
0.160832f0

julia> Flux.binarycrossentropy(sigmoid.(y_model), y_bin)
0.16083185f0
Flux.Losses.logitcrossentropyMethod
logitcrossentropy(ŷ, y; dims = 1, agg = mean)

Return the cross entropy calculated by

agg(-sum(y .* logsoftmax(ŷ; dims); dims))

This is mathematically equivalent to crossentropy(softmax(ŷ), y), but is more numerically stable than using functions crossentropy and softmax separately.

Example

julia> y_label = Flux.onehotbatch(collect("abcabaa"), 'a':'c')
3×7 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
1  ⋅  ⋅  1  ⋅  1  1
⋅  1  ⋅  ⋅  1  ⋅  ⋅
⋅  ⋅  1  ⋅  ⋅  ⋅  ⋅

julia> y_model = reshape(vcat(-9:0, 0:9, 7.5f0), 3, 7)
3×7 Matrix{Float32}:
-9.0  -6.0  -3.0  0.0  2.0  5.0  8.0
-8.0  -5.0  -2.0  0.0  3.0  6.0  9.0
-7.0  -4.0  -1.0  1.0  4.0  7.0  7.5

julia> Flux.logitcrossentropy(y_model, y_label)
1.5791205f0

julia> Flux.crossentropy(softmax(y_model), y_label)
1.5791197f0
Flux.Losses.maeMethod
mae(ŷ, y; agg = mean)

Return the loss corresponding to mean absolute error:

agg(abs.(ŷ .- y))

Example

julia> y_model = [1.1, 1.9, 3.1];

julia> Flux.mae(y_model, 1:3)
0.10000000000000009
Flux.Losses.mseMethod
mse(ŷ, y; agg = mean)

Return the loss corresponding to mean square error:

agg((ŷ .- y) .^ 2)

Example

julia> y_model = [1.1, 1.9, 3.1];

julia> y_true = 1:3;

julia> Flux.mse(y_model, y_true)
0.010000000000000018
Flux.Losses.msleMethod
msle(ŷ, y; agg = mean, ϵ = eps(ŷ))

The loss corresponding to mean squared logarithmic errors, calculated as

agg((log.(ŷ .+ ϵ) .- log.(y .+ ϵ)) .^ 2)

The ϵ term provides numerical stability. Penalizes an under-estimation more than an over-estimatation.

Example

julia> Flux.msle(Float32[1.1, 2.2, 3.3], 1:3)
0.009084041f0

julia> Flux.msle(Float32[0.9, 1.8, 2.7], 1:3)
0.011100831f0
Flux.Losses.poisson_lossMethod
poisson_loss(ŷ, y; agg = mean)

Return how much the predicted distribution ŷ diverges from the expected Poisson distribution y; calculated as -

sum(ŷ .- y .* log.(ŷ)) / size(y, 2)

Example

julia> y_model = [1, 3, 3];  # data should only take integral values

julia> Flux.poisson_loss(y_model, 1:3)
0.5023128522198171
Flux.Losses.siamese_contrastive_lossMethod
siamese_contrastive_loss(ŷ, y; margin = 1, agg = mean)

Return the contrastive loss which can be useful for training Siamese Networks. It is given by

agg(@. (1 - y) * ŷ^2 + y * max(0, margin - ŷ)^2)

Specify margin to set the baseline for distance at which pairs are dissimilar.

Example

julia> ŷ = [0.5, 1.5, 2.5];

julia> Flux.siamese_contrastive_loss(ŷ, 1:3)
-4.833333333333333

julia> Flux.siamese_contrastive_loss(ŷ, 1:3, margin = 2)
-4.0
Flux.Losses.squared_hinge_lossMethod
squared_hinge_loss(ŷ, y)

Return the squared hinge_loss loss given the prediction ŷ and true labels y (containing 1 or -1); calculated as

sum((max.(0, 1 .- ŷ .* y)).^2) / size(y, 2)

Usually used with classifiers like Support Vector Machines. See also: hinge_loss

Example

julia> y_true = [1, -1, 1, 1];

julia> y_pred = [0.1, 0.3, 1, 1.5];

julia> Flux.squared_hinge_loss(y_pred, y_true)
0.625

julia> Flux.squared_hinge_loss(y_pred[1], y_true[1]) != 0
true

julia> Flux.squared_hinge_loss(y_pred[end], y_true[end]) == 0
true

julia> Flux.squared_hinge_loss(y_pred[2], y_true[2]) != 0
true
Flux.Losses.tversky_lossMethod
tversky_loss(ŷ, y; β = 0.7)

Return the Tversky loss. Used with imbalanced data to give more weight to false negatives. Larger β weigh recall more than precision (by placing more emphasis on false negatives). Calculated as:

1 - sum(|y .* ŷ| + 1) / (sum(y .* ŷ + (1 - β)*(1 .- y) .* ŷ + β*y .* (1 .- ŷ)) + 1)

## Internal functions

CounterfactualExplanations.Generators.∂hMethod
∂h(generator::AbstractGradientBasedGenerator, counterfactual_state::CounterfactualState.State)

The default method to compute the gradient of the complexity penalty at the current counterfactual state for gradient-based generators. It assumes that Zygote.jl has gradient access.

source
CounterfactualExplanations.Generators.∂ℓMethod
∂ℓ(generator::AbstractGradientBasedGenerator, M::Union{Models.LogisticModel, Models.BayesianLogisticModel}, counterfactual_state::CounterfactualState.State)

The default method to compute the gradient of the loss function at the current counterfactual state for gradient-based generators. It assumes that Zygote.jl has gradient access.

source
CounterfactualExplanations.Generators.∂ℓMethod
∂ℓ(generator::AbstractGradientBasedGenerator, M::Models.RTorchModel, counterfactual_state::CounterfactualState.State)

The default method to compute the gradient of the loss function at the current counterfactual state for gradient-based generators. It assumes that Zygote.jl has gradient access.

source
CounterfactualExplanations.Generators.∂ℓMethod
∂ℓ(generator::AbstractGradientBasedGenerator, M::Models.RTorchModel, counterfactual_state::CounterfactualState.State)

The default method to compute the gradient of the loss function at the current counterfactual state for gradient-based generators. It assumes that Zygote.jl has gradient access.

source
CounterfactualExplanations.Generators.∇Method
∇(generator::AbstractGradientBasedGenerator, M::Models.AbstractDifferentiableModel, counterfactual_state::CounterfactualState.State)

The default method to compute the gradient of the counterfactual search objective for gradient-based generators. It simply computes the weighted sum over partial derivates. It assumes that Zygote.jl has gradient access.

source
CounterfactualExplanations.Generators.∇Method
∇(generator::GreedyGenerator, counterfactual_state::CounterfactualState.State)

he default method to compute the gradient of the counterfactual search objective for a greedy generator. Since no complexity penalty is needed, this gradients just correponds to the partial derivative with respect to the loss function.

source
Flux.Losses.xlogxMethod
xlogx(x)

Return x * log(x) for x ≥ 0, handling x == 0 by taking the limit from above, to get zero.

Flux.Losses.xlogyMethod
xlogy(x, y)

Return x * log(y) for y > 0, and zero when x == 0.