Closed andgan closed 6 years ago
I have also encountered the same problem. Did you solve the problem?
Gradient information cannot propagate through Embedding layers. For NLP models (or any model using lookups), please read https://github.com/marcoancona/DeepExplain#nlp--embedding-lookups The input layer or the "explanation" model should be the first layer after the lookup. In the case of NLP, this means that you compute explanations on the vector representation of each word, instead of the word itself. You also check this discussion.
Hey :)
Thanks for the help. How would I have to modify the source code if I have several inputs/outputs in the embedding layer?
For example, in such a network structure:
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 10) 0
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 10) 0
__________________________________________________________________________________________________
input_3 (InputLayer) (None, 10) 0
__________________________________________________________________________________________________
ModellEmbeddings (Embedding) (None, 10, 32) 172096 input_1[0][0]
input_2[0][0]
input_3[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 10, 96) 0 ModellEmbeddings[0][0]
ModellEmbeddings[1][0]
ModellEmbeddings[2][0]
__________________________________________________________________________________________________
conv1d_1 (Conv1D) (None, 10, 100) 19300 concatenate_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D) (None, 5, 100) 0 conv1d_1[0][0]
__________________________________________________________________________________________________
lstm_1 (LSTM) (None, 100) 80400 max_pooling1d_1[0][0]
__________________________________________________________________________________________________
main_output (Dense) (None, 5383) 543683 lstm_1[0][0]
===================================================================
The approach described in this does not work for me. I implemented it like:
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)
# Evaluate the embedding tensor on the model input (in other words, perform the lookup)
embedding_tensor = model.layers[3].output
input_tensor = model.inputs[0]
embedding_out = current_session.run(embedding_tensor, {input_tensor: X_test})
xs = X_test
ys = predictions
# Run DeepExplain with the embedding as input
attributions = de.explain('elrp', model.layers[-1].output * ys, model.layers[1].input, embedding_out)
print("attributions shape --- {}".format(attributions.shape))
This is a different problem. In this case you have a shared embedding layers used 3 times. Don't you get an error when you try to do model.layers[3].output
? Since layer 3 (embedding) is a shared layer, you should be using model.layers[3].get_output_at(index)
according to https://keras.io/layers/about-keras-layers/
Another problem with your code is that de.explain
is called with input tensor model.layers[1].input
instead of the tensor after the embedding lookup.
Since you are then concatenating the results of the embedding lookup, I would suggest to call de.explain
on the input to the Concatenate layer, like the following (not tested):
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)
# Evaluate the embedding tensor on the model input (in other words, perform the lookup)
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 embedding as input
attributions = de.explain('elrp', model.layers[-1].output * ys, concat_tensor, concat_out)
print("attributions shape --- {}".format(attributions.shape))
Close as the original issue is known and covered in the README. Please open a new issue if the second problem persists.
I 'm running the following model:
and then I try DeepExplain
and get this error
and this is mt layer configuration