-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for white-box explainers to alibi-explain runtime (#1279)
- Loading branch information
1 parent
6864a2d
commit 7838eff
Showing
10 changed files
with
587 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
runtimes/alibi-explain/mlserver_alibi_explain/explainers/sklearn_api_runtime.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Any | ||
|
||
import joblib | ||
from xgboost.core import XGBoostError | ||
|
||
from mlserver_xgboost.xgboost import _load_sklearn_interface as load_xgb_model | ||
from mlserver.errors import InvalidModelURI | ||
from mlserver_alibi_explain.explainers.white_box_runtime import ( | ||
AlibiExplainWhiteBoxRuntime, | ||
) | ||
|
||
|
||
class SKLearnRuntime(AlibiExplainWhiteBoxRuntime): | ||
""" | ||
Runtime for white-box explainers that require access to a tree-based model matching | ||
the SKLearn API, such as a sklearn, XGBoost, or LightGBM model. Example explainers | ||
include TreeShap and TreePartialDependence. | ||
""" | ||
|
||
async def _get_inference_model(self) -> Any: | ||
inference_model_path = self.alibi_explain_settings.infer_uri | ||
# Attempt to load model. | ||
try: | ||
# Try to load as joblib model first | ||
model = joblib.load(inference_model_path) | ||
except (IndexError, KeyError, IOError): | ||
try: | ||
# Try to load as XGBoost model | ||
model = load_xgb_model(inference_model_path) | ||
except XGBoostError: | ||
raise InvalidModelURI(self.name, inference_model_path) | ||
return model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.