Exemplo n.º 1
0
 def explain_instance(self,
                      data_row,
                      classifier_fn,
                      threshold=0.95,
                      delta=0.1,
                      tau=0.15,
                      batch_size=100,
                      max_anchor_size=None,
                      desired_label=None,
                      beam_size=4,
                      sample_whole_instances=True,
                      **kwargs):
     # It's possible to pass in max_anchor_size
     sample_fn, mapping = self.get_sample_fn(data_row,
                                             classifier_fn,
                                             sample_whole_instances,
                                             desired_label=desired_label)
     exp = anchor_base.AnchorBaseBeam.anchor_beam(
         sample_fn,
         delta=delta,
         epsilon=tau,
         batch_size=batch_size,
         desired_confidence=threshold,
         max_anchor_size=max_anchor_size,
         **kwargs)
     self.add_names_to_exp(data_row, exp, mapping)
     exp['instance'] = data_row
     exp['prediction'] = classifier_fn(
         self.encoder.transform(data_row.reshape(1, -1)))[0]
     explanation = anchor_explanation.AnchorExplanation(
         'tabular', exp, self.as_html)
     return explanation
Exemplo n.º 2
0
 def explainInstance(self,
                     text,
                     classifierFn,
                     threshold=0.95,
                     delta=0.1,
                     tau=0.15,
                     batchSize=100,
                     useProba=False,
                     beamSize=4,
                     **kwargs):
     words, positions, trueLabel, sampleFn = self.getSampleFn(
         text, classifierFn, useProba=useProba)
     exp = anchor_base.AnchorBaseBeam.anchor_beam(
         sampleFn,
         delta=delta,
         epsilon=tau,
         batch_size=batchSize,
         desired_confidence=threshold,
         stop_on_first=True,
         **kwargs)
     exp['names'] = [words[x] for x in exp['feature']]
     exp['positions'] = [positions[x] for x in exp['feature']]
     exp['instance'] = text
     exp['prediction'] = trueLabel
     explanation = anchor_explanation.AnchorExplanation(
         'text', exp, self.as_html)
     return explanation
Exemplo n.º 3
0
 def explain_instance(self,
                      text,
                      classifier_fn,
                      threshold=0.95,
                      delta=0.1,
                      tau=0.15,
                      batch_size=100,
                      use_proba=False,
                      beam_size=4,
                      pertinents_negatifs=False,
                      pertinents_negatifs_replace=False,
                      **kwargs):
     # words corresponds to the different words from the target sentence, positions to their positions in the sentence
     # true_labels to the label predicted by the black box and sample_fn is the function generating the matrix of perturbed sentence
     words, positions, true_label, sample_fn = self.get_sample_fn(
         text,
         classifier_fn,
         use_proba=use_proba,
         pertinents_negatifs=pertinents_negatifs,
         pertinents_negatifs_replace=pertinents_negatifs_replace)
     # Main function that find an anchor based on the matrix of perturbed sentence
     exp = anchor_base.AnchorBaseBeam.anchor_beam(
         sample_fn,
         delta=delta,
         epsilon=tau,
         batch_size=batch_size,
         desired_confidence=threshold,
         stop_on_first=True,
         pertinents_negatifs=pertinents_negatifs,
         pertinents_negatifs_replace=pertinents_negatifs_replace,
         **kwargs)
     if pertinents_negatifs:
         exp['names'] = ['not ' + words[x] for x in exp['feature']]
     elif pertinents_negatifs_replace:
         exp['names'] = ['not ' + words[x] for x in exp['feature']]
     else:
         exp['names'] = [words[x] for x in exp['feature']]
     exp['positions'] = [positions[x] for x in exp['feature']]
     exp['instance'] = text
     exp['prediction'] = true_label
     explanation = anchor_explanation.AnchorExplanation(
         'text', exp, self.as_html)
     return explanation