marcoancona / DeepExplain

A unified framework of perturbation and gradient-based attribution methods for Deep Neural Networks interpretability. DeepExplain also includes support for Shapley Values sampling. (ICLR 2018)
https://arxiv.org/abs/1711.06104
MIT License
725 stars 133 forks source link

DeepExplain with the concat-layer as input #18

Open JoshPrim opened 6 years ago

JoshPrim commented 6 years ago

Hey :) Unfortunately, I haven't been able to solve my problem yet.

I tried to implement it as described:

current_session = K.get_session()

with DeepExplain(session=current_session) as de:

    # load the model 
    model = load_model('model.h5')
    predictions = model.predict([input_0, input_1, input_2], verbose=2) 

    # predict on test data
    X_test = [input_0, input_1, input_2]
    y_pred = model.predict(X_test)

    concat_tensor = model.layers[4].input
    input_tensor = model.inputs[0]
    concat_out = current_session.run(concat_tensor, {input_tensor: X_test})

    xs = X_test
    ys = predictions

    # Run DeepExplain with the concat-layer as input
    attributions = de.explain('elrp', model.layers[-1].output * ys, concat_tensor, concat_out);
    print("attributions shape --- {}".format(attributions.shape));

I get in the line:

concat_out = current_session.run(concat_tensor, {input_tensor: X_test})

the following error message:

ValueError: Cannot feed value of shape (3, 1, 10) for Tensor 'input_1_1:0', which has shape '(?, 10)'

Do I have to change my normal input to put it into the concat layer?

Thanks again for the quick help :)

marcoancona commented 6 years ago

No, the input shape should be the same. Just to double check, are you sure model.layers[4].input is the input of the concat layer? Can you try targeting model.layers[4].output instead? We have never tested Concat layers, but I can try myself if you can provide a minimal working example.

JoshPrim commented 6 years ago

Hey :)

Thanks a lot for your support concerning my problem.

I think I've found a solution. I implemented it as follows:

current_session = K.get_session()

with DeepExplain(session=current_session) as de:

    # load the model 
    model = load_model('model.h5')
    predictions = model.predict([input_0, input_1, input_2], verbose=2) 

    # predict on test data
    X_test = [input_0, input_1, input_2]
    y_pred = model.predict(X_test)

    concat_tensor = model.layers[4].input
    input_tensor = model.inputs 
    concat_out = current_session.run(concat_tensor, {input_tensor[0]: X_test[0], input_tensor[1]: X_test[1], input_tensor[2]: X_test[2]})

    xs = X_test
    ys = predictions

    # Run DeepExplain with the concat-layer as input
    attributions = de.explain('elrp', model.layers[-1].output * ys, concat_tensor, concat_out)

    print("attributions --- {}".format(attributions))

Once again, thank you very much for your support.