Closed thomasdulacatos closed 11 months ago
Hi there!
Just to be sure do you want to pickle the explainer object or the explanations ?
So after investigating with @dv-ai we found out the reason why the dump isn't working (and that is true for Lime
too: it is because similarity_kernel
and pertub_func
attributes are @tf.function
thus a graph is built and it cannot be serialized. So there are two solutions we can suggest:
Solution 1: Do not serialize KernelShap
explainer but only your simple model and the explanations as building KernelShap once the model is trained is fast:
def main():
model = SimpleModel()
model.fit(pd.DataFrame(np.random.randint(1,100,size=(100, 100))))
dump1 = dill.dumps(model)
explainer = KernelShap(model = model)
explanations = explainer.explain(np.random.randint(1,100,size=(100, 100)),
np.random.randint(1,100,size=(100, 100)))
dump2 = dill.dumps(explanations)
Solution 2: You can run tf eagerly, however, that may induce a performance decrease (but that depends on your data and your model):
import tensorflow as tf
def main():
tf.config.run_functions_eagerly(True)
model = SimpleModel()
model.fit(pd.DataFrame(np.random.randint(1,100,size=(100, 100))))
explainer = KernelShap(model = model)
explanations = explainer.explain(np.random.randint(1,100,size=(100, 100)),
np.random.randint(1,100,size=(100, 100)))
model = SimpleModel()
model.fit(pd.DataFrame(np.random.randint(1,100,size=(100, 100))))
explainer = KernelShap(model = model)
dump1 = dill.dumps(explainer)
For performance issues, we do not think to remove the @tf.function
decorator. Consequently, can you tell us if you are satisfied with the solutions we gave here?
Thanks for your solutions. I'll use the first one in the futur, It seems to be working perfectly well. Maybe you could add something in the doc for futur case as mine :) Have a nice day Thomas
Module
Attributions Methods
Current Behavior
Pickling of KernelShap doesn't work, using dill, pickle or cloudpickle. Error : TypeError: cannot pickle '_thread._local' object It's a duplicate of another ticket I created 2 weeks ago, but I wasn't clear enough / precise enough.
Expected Behavior
Pickling should work.
Version
1.1.0
Environment
Relevant log output
To Reproduce
Python file :
` import numpy as np import pandas as pd import dill
from sklearn.preprocessing import MinMaxScaler from xplique.attributions import KernelShap
class SimpleModel:
def main():
main() `