1

''' 我正在使用 SHAP 进行模型分析,在调用 'DeepExplainer' 时,我收到“AttributeError: 'Sequential' object has no attribute 'eval'”

我正在使用带有 keras 而不是张量流的 theano,因为 SHAP 存在一些版本不匹配问题,我在其他问题中也发布过。所以现在我正在尝试相同的东西,但是这一次,_Backend 我正在使用 PyTorch 并且模型构建很好,但是在使用 SHAP DeepExplainer 时它会抛出属性错误,我是 Model Explainer 域的这种类型错误的新手'''

输入:

print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print('Train...')
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=1,
          validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test,
                            batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

输出:没有错误

之后: 输入:

import shap

# we use the first 100 training examples as our background dataset to integrate over
explainer = shap.DeepExplainer(model, x_train[:100])

错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-12-9cca779d01d2> in <module>
      1 # we use the first 100 training examples as our background dataset to integrate over
----> 2 explainer = shap.DeepExplainer(model,1)

c:\users\shubh\.conda\envs\pytorch_cpu\lib\site-packages\shap\explainers\deep\__init__.py in __init__(self, model, data, session, learning_phase_flags)
     79             self.explainer = TFDeepExplainer(model, data, session, learning_phase_flags)
     80         elif framework == 'pytorch':
---> 81             self.explainer = PyTorchDeepExplainer(model, data)
     82 
     83         self.expected_value = self.explainer.expected_value

c:\users\shubh\.conda\envs\pytorch_cpu\lib\site-packages\shap\explainers\deep\deep_pytorch.py in __init__(self, model, data)
     47             self.target_handle.remove()
     48             del self.layer.target_input
---> 49         self.model = model.eval()
     50 
     51         self.multi_output = False

AttributeError: 'Sequential' object has no attribute 'eval'

解决此错误的任何帮助或方向?

4

0 回答 0