【发布时间】:2021-12-17 11:51:34
【问题描述】:
从概念上讲,我想做一些简单的事情:保存一个我以后可以从另一个(不同的)程序访问的 python 对象。
但问题是它周围有一个包装器(下面的 f(x)),在新环境中没有被引用。
花了整整 12 个小时后,我感到比开始时更加困惑。我认为“泡菜”或“莳萝”等……是我应该做的。但我遇到了酸洗问题。但是在线阅读让我无处可去。 (顺便说一句,我尝试了 shap.save,但它遇到了同样的问题,并且无论如何都使用 pickle)。
import shap, pickle
model = ... (some tensorflow function)
def f(X):
...
return model.predict(...).flatten()
explainer = shap.KernelExplainer(f, X.iloc[:50, :])
with open(f"/tmp/{file}.pkl", 'wb') as fil:
# explainer.save(fil)
pickle.dump(explainer, fil)
这不起作用,因为它“找不到 属性 'f'"。这些看起来是我能找到的最有前途的文章,但我无法为我的场景实现。
http://gael-varoquaux.info/programming/decoration-in-python-done-right-decorating-and-pickling.html
Unable to load files using pickle and multiple modules
https://github.com/slundberg/shap/issues/295
Python: Can't pickle type X, attribute lookup failed
*** 请为我提供有关 cmets 术语的建议,以改进我的提问方式,因为我不知道如何表达我的提示。
【问题讨论】: