Beispiel #1
0
    def __init__(self,
                 name: str,
                 predictor_host: str,
                 method: ExplainerMethod,
                 config: Mapping,
                 explainer: object = None,
                 protocol: Protocol = Protocol.seldon_grpc,
                 tf_data_type: str = None,
                 keras_model: keras.Model = None):
        super().__init__(name)
        self.predictor_host = predictor_host
        logging.info("Predict URL set to %s", self.predictor_host)
        self.method = method
        self.protocol = protocol
        self.tf_data_type = tf_data_type
        logging.info("Protocol is %s", str(self.protocol))

        # Add type for first value to help pass mypy type checks
        if self.method is ExplainerMethod.anchor_tabular:
            self.wrapper: ExplainerWrapper = AnchorTabular(
                self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_images:
            self.wrapper = AnchorImages(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_text:
            self.wrapper = AnchorText(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.kernel_shap:
            self.wrapper = KernelShap(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.integrated_gradients:
            self.wrapper = IntegratedGradients(keras_model, **config)
        elif self.method is ExplainerMethod.tree_shap:
            self.wrapper = TreeShap(explainer, **config)
        else:
            raise NotImplementedError
def test_integrated_gradients():

    with tempfile.TemporaryDirectory() as model_dir:
        download_from_gs(IMDB_KERAS_MODEL_URI, model_dir)
        keras_model_path = os.path.join(model_dir, KERAS_MODEL_FILENAME)
        keras_model = keras.models.load_model(keras_model_path)
    integrated_gradients = IntegratedGradients(keras_model, layer=1)
    max_features = 10000
    maxlen = 100
    (x_train, y_train), (x_test,
                         y_test) = imdb.load_data(num_words=max_features)

    x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
    x_test = sequence.pad_sequences(x_test, maxlen=maxlen)

    explanation = integrated_gradients.explain(x_test[0:1].tolist())
    attrs = explanation["attributions"]
    assert len(attrs) > 0
Beispiel #3
0
def test_integrated_gradients():

    keras_model_path = os.path.join(
        kfserving.Storage.download(IMDB_KERAS_MODEL_URI), KERAS_MODEL_FILENAME)
    keras_model = keras.models.load_model(keras_model_path)
    integrated_gradients = IntegratedGradients(keras_model, layer=1)
    max_features = 10000
    maxlen = 100
    (x_train, y_train), (x_test,
                         y_test) = imdb.load_data(num_words=max_features)
    print(len(x_train), 'train sequences')
    print(len(x_test), 'test sequences')

    print('Pad sequences (samples x time)')
    x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
    x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
    print('x_train shape:', x_train.shape)
    print('x_test shape:', x_test.shape)

    explanation = integrated_gradients.explain(x_test[0:1].tolist())
    attrs = explanation["attributions"]
    assert len(attrs) > 0
    print(explanation)