marcotcr / anchor

Code for "High-Precision Model-Agnostic Explanations" paper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Anchor - multi-labels prediction model

limesun opened this issue · comments

commented

Hi,

I am trying to apply anchor in my text classification models, but I am wondering if it is applicable to multi-labeled target case because your sample is handling a binary case. In the example, I should set up the "alternative" parameter for the other target.

Please advised me.

Thanks,

You can apply it to the multi-label case, but you have to be careful about what the explanation means. You can either have an anchor for the presence of a certain label in the prediction, or an anchor for the whole set of labels. Let's assume your model takes in a 2d numpy array, and returns prediction probabilities for each label such that anything above a certain threshold is predicted. If you want to get an anchor for the presence of a specific label in the prediction, you have to wrap the prediction function as follows:

def get_wrapped_prediction_function(predict_fn, label, threshold=0.5):
    def wrapped_predict_fn(data):
        preds = predict_fn(data)
        return (preds[:, label] > threshold).astype(int)
    return wrapped_predict_fn

If you want to explain the whole set (i.e. the anchor is telling you what is sufficient to get ALL of the predicted labels), you could do something like:

def get_wrapped_predict_fn(predict_fn, instance, threshold=0.5):
    labels = predict_fn(instance.reshape(1, -1)) > threshold)
    def wrapped_predict_fn(data):
        preds = predict_fn(data)
        return np.all((preds[:, labels] > threshold), axis=1).astype(int)
    return wrapped_predict_fn

In either case, what I've done is binarize the original prediction function to output 1 if the condition you want an anchor for is true, and 0 otherwise. This wrapped function is what you should use with the explainer.

commented

Thanks, Marco!