Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix kernel explainer save / load method #2604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion shap/explainers/_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import itertools
import warnings
import gc
import pickle
from sklearn.linear_model import LassoLarsIC, Lasso, lars_path
from tqdm.auto import tqdm
from ._explainer import Explainer
from .._serializable import Serializer, Deserializer, Serializable

log = logging.getLogger('shap')



class Kernel(Explainer):
"""Uses the Kernel SHAP method to explain the output of any function.

Expand Down Expand Up @@ -610,3 +611,22 @@ def solve(self, fraction_evaluated, dim):
phi[i] = 0

return phi, np.ones(len(phi))

@classmethod
def load(cls, in_file, model_loader=None, masker_loader=None, instantiate=True):
if instantiate:
return cls._instantiated_load(in_file, model_loader=None, masker_loader=None)

kwargs = Serializable.load(in_file, instantiate=False)
with Deserializer(in_file, "shap.Explainer", min_version=0, max_version=0) as s:
kwargs["model"] = s.load("model")
kwargs["link"] = s.load("link")
kwargs["data"] = s.load("data")
return kwargs

def save(self, out_file, model_saver=None, masker_saver=None):
pickle.dump(type(self), out_file)
with Serializer(out_file, "shap.Explainer", version=0) as s:
s.save("model", self.model)
s.save("link", self.link)
s.save("data", self.data)