Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve issues #150 and #151 on fidelity metrics #152

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.3.2
current_version = 1.3.3
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="Xplique",
version="1.3.2",
version="1.3.3",
description="Explanations toolbox for Tensorflow 2",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion tests/metrics/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_data_types_shapes():
explanation_metrics = {
Deletion: {"steps": 3},
Insertion: {"steps": 3},
MuFidelity: {"nb_samples": 3},
MuFidelity: {"nb_samples": 3, "grid_size": None, "subset_percent": 0.9},
}

for data_type, input_shape in data_types_input_shapes.items():
Expand Down
4 changes: 2 additions & 2 deletions tests/metrics/test_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def test_mu_fidelity():

def test_causal_metrics():
# ensure we can compute insertion/deletion metric with consistent arguments
input_shape, nb_labels, nb_samples = ((32, 32, 3), 10, 20)
input_shape, nb_labels, nb_samples = ((10, 10, 3), 10, 20)
x, y = generate_data(input_shape, nb_labels, nb_samples)
model = generate_model(input_shape, nb_labels)
explanations = np.random.uniform(0, 1, x.shape[:-1] + (1,)).astype(np.float32)

for step in [5, 10]:
for step in [-1, 5, 10]:
for baseline_mode in [0.0, lambda x: x-0.5]:
score_insertion = Insertion(model, x, y,
baseline_mode=baseline_mode,
Expand Down
2 changes: 1 addition & 1 deletion xplique/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
techniques
"""

__version__ = '1.3.2'
__version__ = '1.3.3'

from . import attributions
from . import concepts
Expand Down
10 changes: 4 additions & 6 deletions xplique/metrics/fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ def __init__(self,

# if unspecified use the original equation (pixel-wise modification)
self.grid_size = grid_size or self.inputs.shape[1]
# cardinal of subset (|S| in the equation)
self.subset_size = int(self.grid_size ** 2 * self.subset_percent)

self.base_predictions = self.batch_inference_function(self.model, self.inputs,
self.targets, self.batch_size)
Expand Down Expand Up @@ -198,14 +196,14 @@ def _perturb_samples(self,
if len(inputs.shape) == 2: # tabular data, grid size is ignored
# prepare the random masks
subset_masks = tf.random.uniform((nb_perturbations, inputs.shape[1]), 0, 1, tf.float32)
subset_masks = tf.argsort(subset_masks, axis=-1) > self.subset_size
subset_masks = subset_masks > self.subset_percent
subset_masks = tf.cast(subset_masks, tf.float32)

elif len(inputs.shape) == 3: # time series
# prepare the random masks
subset_masks = tf.random.uniform((nb_perturbations, self.grid_size * inputs.shape[2]),
minval=0, maxval=1, dtype=tf.float32)
subset_masks = tf.argsort(subset_masks, axis=-1) > self.subset_size
subset_masks = subset_masks > self.subset_percent

# and interpolate them if needed
subset_masks = tf.reshape(tf.cast(subset_masks, tf.float32),
Expand All @@ -217,7 +215,7 @@ def _perturb_samples(self,
# prepare the random masks
subset_masks = tf.random.uniform(shape=(nb_perturbations, self.grid_size ** 2),
minval=0, maxval=1, dtype=tf.float32)
subset_masks = tf.argsort(subset_masks, axis=-1) > self.subset_size
subset_masks = subset_masks > self.subset_percent

# and interpolate them if needed
subset_masks = tf.reshape(tf.cast(subset_masks, tf.float32),
Expand Down Expand Up @@ -303,7 +301,7 @@ def __init__(self,

assert 0.0 < max_percentage_perturbed <= 1.0, \
"`max_percentage_perturbed` must be in ]0, 1]."
self.max_nb_perturbed = tf.math.floor(self.nb_features * max_percentage_perturbed)
self.max_nb_perturbed = int(np.floor(self.nb_features * max_percentage_perturbed))

if steps == -1:
steps = self.max_nb_perturbed
Expand Down