我已经被这个错误困扰了很长时间,有没有办法在不降级我的 tensorflow 版本的情况下解决它?到目前为止,我发现的所有解决方案都推荐使用 TF<2.0,我不想这样做。当前 TF 版本 = 2.4.1,Keras 版本 = 2.4.3,使用 google colab
我正在尝试将 SHAP GradientExplainer 与 VGG 16 模型一起使用,以查看特定层如何影响预测。
代码是:
e = shap.GradientExplainer((model.layers[7].input, model.layers[-1].output), map2layer(preprocess_input(X.copy()), 7))
shap_values, indexes = e.shap_values(map2layer(to_predict, 7), ranked_outputs=2)
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)
index_names
错误是:
TypeError Traceback (most recent call last)
<ipython-input-13-b3a265bc3cde> in <module>()
----> 1 e = shap.GradientExplainer((model.layers[7].input, model.layers[-1].output), map2layer(preprocess_input(X.copy()), 7))
2 shap_values, indexes = e.shap_values(map2layer(to_predict, 7), ranked_outputs=2)
3 index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)
4 index_names
<ipython-input-11-f110beabf449> in map2layer(x, layer)
1 def map2layer(x, layer):
----> 2 feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
3 return K.get_session().run(model.layers[layer].input, feed_dict)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/keras_tensor.py in __hash__(self)
259 def __hash__(self):
260 raise TypeError('Tensors are unhashable. (%s)'
--> 261 'Instead, use tensor.ref() as the key.' % self)
262
263 # Note: This enables the KerasTensor's overloaded "right" binary
TypeError: Tensors are unhashable. (KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"))Instead, use tensor.ref() as the key.