我正在尝试应用 shap.deepexplainer 来解释模型输出。
我的模型类如下:
class MyModel(tf.keras.Model):
def __init__(self,
input_dim,
emb_dim=128,
alpha_hidden_dim_size=128,
beta_hidden_dim_size=128,
keep_prob_emb=0.25,
keep_prob_context=0.25,
num_class=1):
super(MyModel, self).__init__()
self.embedding = layers.Dense(emb_dim,
use_bias=False,
input_shape=(input_dim, ))
self.emb_drp = layers.Dropout(keep_prob_emb)
self.enroll = layers.Dense(emb_dim, activation='tanh')
self.gru_alpha = layers.Bidirectional(
layers.LSTM(alpha_hidden_dim_size, return_sequences=True))
self.gru_beta = layers.Bidirectional(
layers.LSTM(beta_hidden_dim_size, return_sequences=True))
self.alpha = layers.Dense(1)
self.beta = layers.Dense(emb_dim, activation='tanh')
self.context_drp = layers.Dropout(keep_prob_context)
self.out = layers.Dense(num_class)
def call(self, visits, enroll, lengths, **kwargs):
max_len = lengths[tf.argmax(lengths)]
visits = visits[:, :max_len]
emb = self.embedding(visits)
emb = self.emb_drp(emb, training=kwargs.get('training', False))
enroll = self.enroll(enroll)
mask = tf.sequence_mask(lengths)
h_a = self.gru_alpha(emb, mask=mask)
h_b = self.gru_beta(emb, mask=mask)
preAlpha = self.alpha(h_a)
preAlpha = tf.keras.backend.squeeze(preAlpha, axis=2)
mask_norm = (1 - tf.cast(mask, tf.float32)) * NEG_INF
alpha = tf.nn.softmax(preAlpha + mask_norm, axis=1)
beta = self.beta(h_b)
c_t = tf.math.reduce_sum(alpha[:, :, None] * beta * emb, axis=1)
c_t = layers.add([c_t, enroll])
c_t = self.context_drp(c_t, training=kwargs.get('training', False))
preY = self.out(c_t)
return preY, alpha, beta
当我将模型应用为:
model = MyModel(**flags)
并且模型加载成功:
print(model)
<__main__.MyModel object at 0x7f51db414400>
然后我尝试使用
background = X.loc[10:20]
e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)
但后来它给了我错误:
AttributeError: 层密集未连接,没有输入返回。Traceback(最近一次调用最后一次):文件“/home/ANANT/codes/test/env/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py”,第 1808 行,在输入中没有连接,没有输入返回。')
而且model.layers[-1].output也不能给出正确的输出:
AttributeError:Layer dense_4 没有入站节点。回溯(最后一次调用):文件“/home/ANANT/test/env/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py”,第 1827 行,在输出中引发 AttributeError( '图层' + self.name + '没有入站节点。')
我的包版本是:keras==2.3.1,tensorflow==1.15.3,shap==0.35.0
我在这个问题上停留了几天,也尝试了 shap.KernelExplainer,它给了我一个不同的错误:
shap.KernelExplainer(model, df_fis, link="logit")
错误如下:
类型错误:调用()缺少 2 个必需的位置参数:“注册”和“长度”回溯(最近一次调用):文件“/home/ANANT/test/env/lib/python3.6/site-packages/shap/explainers /kernel.py”,第 97 行,在 __init__model_null = match_model_to_data(self.model, self.data) 文件“/home/ANANT/test/env/lib/python3.6/site-packages/shap/common.py”中,第 89 行,在 match_model_to_dataout_val = model.f(data.data) 文件“/home/ANANT/test/env/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py”中,第 968 行, 在 __call__outputs = self.call(cast_inputs, *args, **kwargs)
请帮忙,提前谢谢!