marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.65k stars 1.81k forks source link

Difference between paper and code in calculating distance function #403

Closed tomohikoabe-gvatech closed 4 years ago

tomohikoabe-gvatech commented 5 years ago

I'm currently working on text classification tasks with LSTM and would like to use LIME to help to explain the results.

I have two questions.

(1) I think there's difference between paper and code in calculating distance function D(x,z) (Eq.2 in the paper). Specifically, in the paper, the distance function is calculated over original representations x and z in R^d, on the other hand, in the code (lime/lime/lime_text.py), it is calculated over interpretable (or binary) representations x' and z' in {0,1}^d' as follows:

class LimeTextExplainer(object):
    ...
    def __data_labels_distances(self,
                                indexed_string,
                                classifier_fn,
                                num_samples,
                                distance_metric='cosine'):
        ...
        data = np.ones((num_samples, doc_size))
        ...
        distances = distance_fn(sp.sparse.csr_matrix(data))
        ...

(2) If the calculation method in the paper is correct, i couldn't figure out how i could calculate the distance function D(x,z) as an input of a sequence of token vectors {x1, ..., xn}.

I would really appreciate it if you could respond to the questions.

marcotcr commented 4 years ago

You are correct, strictly speaking we should compute distance on the original representation (text). However, to compute distance between two strings you have to represent them somehow. To make things simple, we just used the same binary representation used for explanation. We could have used count vectors, which would be a little more meaningful I guess.

If you want to implement a different distance function, you can use inverse_data, which has the perturbed data in string form with the original string in position [0]