-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_nlp_occlusion.py
More file actions
47 lines (36 loc) · 1.49 KB
/
test_nlp_occlusion.py
File metadata and controls
47 lines (36 loc) · 1.49 KB
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
"""
Test object detection BoundingBoxesExplainer
"""
import numpy as np
from xplique.attributions import NlpOcclusion
def test_masks():
"""Test the masks creation"""
sentence = "aaa bbb ccc"
words = sentence.split(" ")
masks = NlpOcclusion._get_masks(words)
assert masks.shape == (len(words), len(words))
expected_mask = np.array([[False, True, True],
[True, False, True],
[True, True, False]])
assert np.array_equal(masks, expected_mask)
def test_apply_masks():
"""Test if the application of a mask generate valid results"""
sentence = "aaa bbb ccc"
words = sentence.split(" ")
masks = NlpOcclusion._get_masks(words)
occluded_inputs = NlpOcclusion._apply_masks(words, masks)
expected_occludec_inputs = [['bbb', 'ccc'], ['aaa', 'ccc'], ['aaa', 'bbb']]
assert np.array_equal(occluded_inputs, expected_occludec_inputs)
def test_output_shape():
"""Test the output shape for several input sentences"""
nb_concepts = 10
def transform(inputs):
# simulate the transorm method used in Craft/Cockatiel
return np.ones((len(inputs), nb_concepts))
input_sentence = ["aaa bbb ccc ddd eee fff", "ggg hhh iii jjj"]
for sentence in input_sentence:
words = sentence.split(" ")
separator = " "
method = NlpOcclusion(model=transform)
sensitivity = method.explain(sentence, words, separator)
assert sensitivity.shape == (nb_concepts, len(words))