Search in sources :

Example 61 with PredictionProvider

use of org.kie.kogito.explainability.model.PredictionProvider in project kogito-apps by kiegroup.

the class ShapKernelExplainerTest method testPredictionWrongSize.

// Test cases with prediction size mismatches ========================================================
@Test
void testPredictionWrongSize() throws InterruptedException, TimeoutException, ExecutionException {
    // establish background data and desired data to explain
    double[][] backgroundMat = new double[5][5];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            backgroundMat[i][j] = i / 5. + j;
        }
    }
    double[][] toExplainTooSmall = { { 0, 1., 2., 3., 4. } };
    List<PredictionInput> background = createPIFromMatrix(backgroundMat);
    List<PredictionInput> toExplain = createPIFromMatrix(toExplainTooSmall);
    PredictionProvider modelForPredictions = TestUtils.getSumSkipTwoOutputModel(1);
    PredictionProvider modelForShap = TestUtils.getSumSkipModel(1);
    ShapConfig skConfig = testConfig.withBackground(background).build();
    // initialize explainer
    List<PredictionOutput> predictionOutputs = modelForPredictions.predictAsync(toExplain).get(5, TimeUnit.SECONDS);
    List<Prediction> predictions = new ArrayList<>();
    for (int i = 0; i < predictionOutputs.size(); i++) {
        predictions.add(new SimplePrediction(toExplain.get(i), predictionOutputs.get(i)));
    }
    // make sure we get an illegal argument exception; our prediction to explain has a different shape t
    // than the background predictions will
    Prediction p = predictions.get(0);
    ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
    assertThrows(ExecutionException.class, () -> ske.explainAsync(p, modelForShap).get());
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) ArrayList(java.util.ArrayList) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with PredictionProvider

use of org.kie.kogito.explainability.model.PredictionProvider in project kogito-apps by kiegroup.

the class ShapKernelExplainerTest method testLargeBackground2.

@Test
void testLargeBackground2() throws InterruptedException, TimeoutException, ExecutionException {
    // establish background data and desired data to explain
    double[][] largeBackground = new double[100][10];
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 10; j++) {
            largeBackground[i][j] = i / 100. + j;
        }
    }
    double[][] toExplainLargeBackground = { { 0, 1., -2., 3.5, -4.1, 5.5, -12., .8, .11, 15. } };
    double[][][] expected = { { { -0.495, 0., -4.495, 0.005, -8.595, 0.005, -18.495, -6.695, -8.385, 5.505 } } };
    List<PredictionInput> background = createPIFromMatrix(largeBackground);
    List<PredictionInput> toExplain = createPIFromMatrix(toExplainLargeBackground);
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    ShapConfig skConfig = testConfig.withBackground(background).withNSamples(1000).build();
    // initialize explainer
    List<PredictionOutput> predictionOutputs = model.predictAsync(toExplain).get();
    List<Prediction> predictions = new ArrayList<>();
    for (int i = 0; i < predictionOutputs.size(); i++) {
        predictions.add(new SimplePrediction(toExplain.get(i), predictionOutputs.get(i)));
    }
    // evaluate if the explanations match the expected value
    ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
    for (int i = 0; i < toExplain.size(); i++) {
        Saliency[] explanationSaliencies = ske.explainAsync(predictions.get(i), model).get(5, TimeUnit.SECONDS).getSaliencies();
        RealMatrix[] explanationsAndConfs = saliencyToMatrix(explanationSaliencies);
        RealMatrix explanations = explanationsAndConfs[0];
        for (int j = 0; j < explanations.getRowDimension(); j++) {
            assertArrayEquals(expected[i][j], explanations.getRow(j), 1e-2);
        }
    }
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) ArrayList(java.util.ArrayList) Saliency(org.kie.kogito.explainability.model.Saliency) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 63 with PredictionProvider

use of org.kie.kogito.explainability.model.PredictionProvider in project kogito-apps by kiegroup.

the class ShapKernelExplainerTest method testRegularizations.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3 })
void testRegularizations(int config) throws InterruptedException, ExecutionException {
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    List<PredictionInput> toExplain = createPIFromMatrix(toExplainRegTests);
    RealMatrix toExplainMatrix = MatrixUtils.createRealMatrix(toExplainRegTests);
    List<PredictionOutput> predictionOutputs = model.predictAsync(toExplain).get();
    RealVector predictionOutputVector = MatrixUtilsExtensions.vectorFromPredictionOutput(predictionOutputs.get(0));
    Prediction p = new SimplePrediction(toExplain.get(0), predictionOutputs.get(0));
    ShapConfig skConfig = sks.get(config);
    ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
    ShapResults shapResults = ske.explainAsync(p, model).get();
    Saliency[] saliencies = shapResults.getSaliencies();
    RealMatrix[] explanationsAndConfs = saliencyToMatrix(saliencies);
    RealMatrix explanations = explanationsAndConfs[0];
    double actualOut = predictionOutputVector.getEntry(0);
    double predOut = MatrixUtilsExtensions.sum(explanations.getRowVector(0)) + shapResults.getFnull().getEntry(0);
    assertTrue(Math.abs(predOut - actualOut) < 1e-6);
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) Saliency(org.kie.kogito.explainability.model.Saliency) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) RealVector(org.apache.commons.math3.linear.RealVector) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 64 with PredictionProvider

use of org.kie.kogito.explainability.model.PredictionProvider in project kogito-apps by kiegroup.

the class ShapKernelExplainerTest method testNoVarianceOneOutput.

// Single output models ============================================================================================
// test a single output model with no varying features
@Test
void testNoVarianceOneOutput() throws InterruptedException, TimeoutException, ExecutionException {
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    List<PredictionInput> background = createPIFromMatrix(backgroundNoVariance);
    ShapConfig skConfig = testConfig.withBackground(background).withNSamples(100).build();
    shapTestCase(model, skConfig, toExplainZeroVariance, zeroVarianceOneOutputSHAP);
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 65 with PredictionProvider

use of org.kie.kogito.explainability.model.PredictionProvider in project kogito-apps by kiegroup.

the class ShapKernelExplainerTest method testOneVarianceOneOutput.

// test a single output model with one varying feature
@Test
void testOneVarianceOneOutput() throws InterruptedException, TimeoutException, ExecutionException {
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    List<PredictionInput> background = createPIFromMatrix(backgroundNoVariance);
    ShapConfig skConfig = testConfig.withBackground(background).withNSamples(100).build();
    shapTestCase(model, skConfig, toExplainOneVariance, oneVarianceOneOutputSHAP);
}
Also used : PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)158 Prediction (org.kie.kogito.explainability.model.Prediction)134 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)134 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)126 Test (org.junit.jupiter.api.Test)109 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)99 Random (java.util.Random)91 Feature (org.kie.kogito.explainability.model.Feature)76 ArrayList (java.util.ArrayList)73 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)69 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)64 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)59 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)54 Output (org.kie.kogito.explainability.model.Output)45 Saliency (org.kie.kogito.explainability.model.Saliency)45 LinkedList (java.util.LinkedList)41 Value (org.kie.kogito.explainability.model.Value)41 List (java.util.List)37 LimeConfigOptimizer (org.kie.kogito.explainability.local.lime.optim.LimeConfigOptimizer)33 ValueSource (org.junit.jupiter.params.provider.ValueSource)32