【问题标题】:shap.DeepExplainer gives an error related to GlobalMaxPooling1D layer of CNNshap.DeepExplainer 给出与 CNN 的 GlobalMaxPooling1D 层相关的错误
【发布时间】:2021-11-28 19:58:31
【问题描述】:

每当我想用 SHAP 解释 CNN 分类器进行情感分析时,我都会收到如下错误:

InvalidArgumentError: Incompatible shapes: [100,128] vs. [100,46,128]
     [[{{node gradients/global_max_pooling1d/Max_grad/truediv_1}}]]

这是我的 CNN 代码:

inputs = Input(shape=(max_length,), dtype=tf.int32)
embedded_sequences = Embedding(max_features, embedding_dims)(inputs)

out = Conv1D(filters,
             kernel_size,
             padding='valid',
             activation='relu',
             strides=1)(embedded_sequences)

out = Dropout(0.4)(out)
out = GlobalMaxPooling1D()(out)
out = Dense(hidden_dims, activation='relu')(out)
out = Dropout(0.4)(out)
outputs = Dense(1, activation='sigmoid')(out)

model = Model(inputs=inputs, outputs=outputs)

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

这是我的 SHAP 代码:

shap.explainers._deep.deep_tf.op_handlers["AddV2"] = shap.explainers._deep.deep_tf.passthrough
explainer = shap.DeepExplainer(model_lstm, train_padded[:100])

# error appears at this line:
shap_values = explainer.shap_values(test_padded[:20]) 

【问题讨论】:

    标签: python tensorflow conv-neural-network shap


    【解决方案1】:

    您需要确保批次大小相等且可被 4 整除。

    【讨论】:

    • 请问您到底是什么意思?我使用的是 batch_size=256。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2020-12-18
    • 2015-08-08
    相关资源
    最近更新 更多