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

Re-setting numpy random seed to zero on every explain request #4076

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion components/alibi-explain-server/alibiexplainer/anchor_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import numpy as np
from alibi.api.interfaces import Explanation

from alibiexplainer.constants import SELDON_LOGLEVEL
from alibiexplainer.constants import (
EXPLAIN_RANDOM_SEED,
EXPLAIN_RANDOM_SEED_VALUE,
SELDON_LOGLEVEL,
)
from alibiexplainer.explainer_wrapper import ExplainerWrapper

logging.basicConfig(level=SELDON_LOGLEVEL)
Expand All @@ -38,9 +42,13 @@ def __init__(
if explainer is None:
raise Exception("Anchor images requires a built explainer")
self.anchors_image = explainer
if EXPLAIN_RANDOM_SEED == "True" and EXPLAIN_RANDOM_SEED_VALUE.isdigit():
self.seed = int(EXPLAIN_RANDOM_SEED_VALUE)
self.kwargs = kwargs

def explain(self, inputs: List) -> Explanation:
if self.seed:
SachinVarghese marked this conversation as resolved.
Show resolved Hide resolved
np.random.seed(self.seed)
arr = np.array(inputs)
logging.info("Calling explain on image of shape %s", (arr.shape,))
logging.info("anchor image call with %s", self.kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import numpy as np
from alibi.api.interfaces import Explanation

from alibiexplainer.constants import SELDON_LOGLEVEL
from alibiexplainer.constants import (
EXPLAIN_RANDOM_SEED,
EXPLAIN_RANDOM_SEED_VALUE,
SELDON_LOGLEVEL,
)
from alibiexplainer.explainer_wrapper import ExplainerWrapper

logging.basicConfig(level=SELDON_LOGLEVEL)
Expand All @@ -37,9 +41,13 @@ def __init__(self, explainer=Optional[alibi.explainers.AnchorTabular], **kwargs)
raise Exception("Anchor images requires a built explainer")
self.anchors_tabular: alibi.explainers.AnchorTabular = explainer
self.anchors_tabular = explainer
if EXPLAIN_RANDOM_SEED == "True" and EXPLAIN_RANDOM_SEED_VALUE.isdigit():
self.seed = int(EXPLAIN_RANDOM_SEED_VALUE)
self.kwargs = kwargs

def explain(self, inputs: List) -> Explanation:
if self.seed:
np.random.seed(self.seed)
arr = np.array(inputs)
# We assume the input has batch dimension
# but Alibi explainers presently assume no batch
Expand Down
11 changes: 10 additions & 1 deletion components/alibi-explain-server/alibiexplainer/anchor_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
from typing import Callable, List, Optional

import alibi
import numpy as np
import spacy
from alibi.api.interfaces import Explanation
from alibi.utils.download import spacy_model

from alibiexplainer.constants import SELDON_LOGLEVEL
from alibiexplainer.constants import (
EXPLAIN_RANDOM_SEED,
EXPLAIN_RANDOM_SEED_VALUE,
SELDON_LOGLEVEL,
)
from alibiexplainer.explainer_wrapper import ExplainerWrapper

logging.basicConfig(level=SELDON_LOGLEVEL)
Expand All @@ -41,6 +46,8 @@ def __init__(
**kwargs
):
self.predict_fn = predict_fn
if EXPLAIN_RANDOM_SEED == "True" and EXPLAIN_RANDOM_SEED_VALUE.isdigit():
self.seed = int(EXPLAIN_RANDOM_SEED_VALUE)
self.kwargs = kwargs
logging.info("Anchor Text args %s", self.kwargs)
if explainer is None:
Expand All @@ -55,5 +62,7 @@ def __init__(
self.anchors_text = explainer

def explain(self, inputs: List) -> Explanation:
if self.seed:
np.random.seed(self.seed)
anchor_exp = self.anchors_text.explain(inputs[0], **self.kwargs)
return anchor_exp
2 changes: 2 additions & 0 deletions components/alibi-explain-server/alibiexplainer/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

SELDON_LOGLEVEL = os.environ.get("SELDON_LOGLEVEL", "INFO").upper()
EXPLAIN_RANDOM_SEED = os.environ.get("EXPLAIN_RANDOM_SEED", "True")
EXPLAIN_RANDOM_SEED_VALUE = os.environ.get("EXPLAIN_RANDOM_SEED_VALUE", 0)