Search in sources :

Example 71 with PredictionOutput

use of org.kie.kogito.explainability.model.PredictionOutput 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 72 with PredictionOutput

use of org.kie.kogito.explainability.model.PredictionOutput 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 73 with PredictionOutput

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

the class ShapKernelExplainerTest method testBatched.

@Test
void testBatched() throws ExecutionException, InterruptedException {
    RealVector modelWeights = MatrixUtils.createRealMatrix(generateN(1, 10, "5021")).getRowVector(0);
    PredictionProvider model = TestUtils.getLinearModel(modelWeights.toArray());
    RealMatrix data = MatrixUtils.createRealMatrix(generateN(101, 10, "8629"));
    List<PredictionInput> toExplain = createPIFromMatrix(data.getRowMatrix(100).getData());
    List<PredictionOutput> predictionOutputs = model.predictAsync(toExplain).get();
    RealVector predictionOutputVector = MatrixUtilsExtensions.vectorFromPredictionOutput(predictionOutputs.get(0));
    Prediction p = new SimplePrediction(toExplain.get(0), predictionOutputs.get(0));
    List<PredictionInput> bg = createPIFromMatrix(new double[100][10]);
    ShapConfig skNB = testConfig.copy().withBackground(bg).withBatchSize(1).build();
    ShapConfig skB = testConfig.copy().withBackground(bg).withBatchSize(20).build();
    ShapKernelExplainer skeNB = new ShapKernelExplainer(skNB);
    ShapResults shapResultsNB = skeNB.explainAsync(p, model).get();
    ShapKernelExplainer skeB = new ShapKernelExplainer(skB);
    ShapResults shapResultsB = skeB.explainAsync(p, model).get();
    assertEquals(shapResultsNB, shapResultsB);
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) RealVector(org.apache.commons.math3.linear.RealVector) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Prediction(org.kie.kogito.explainability.model.Prediction) SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 74 with PredictionOutput

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

the class ShapKernelExplainerTest method testTooLargeBackground.

// Test cases with size errors ========================================================
@Test
void testTooLargeBackground() throws InterruptedException, TimeoutException, ExecutionException {
    // establish background data and desired data to explain
    double[][] tooLargeBackground = new double[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            tooLargeBackground[i][j] = i / 10. + j;
        }
    }
    double[][] toExplainTooSmall = { { 0, 1., 2., 3., 4. } };
    List<PredictionInput> background = createPIFromMatrix(tooLargeBackground);
    List<PredictionInput> toExplain = createPIFromMatrix(toExplainTooSmall);
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    ShapConfig skConfig = testConfig.withBackground(background).build();
    // initialize explainer
    List<PredictionOutput> predictionOutputs = model.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 because our background is bigger than the point to be explained
    Prediction p = predictions.get(0);
    ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
    assertThrows(IllegalArgumentException.class, () -> ske.explainAsync(p, model));
}
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 75 with PredictionOutput

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

the class ShapKernelExplainerTest method testErrorBounds.

// given a noisy model, expect the n% confidence window to include true value roughly n% of the time
@ParameterizedTest
@ValueSource(doubles = { .001, .1, .25, .5 })
void testErrorBounds(double noise) throws InterruptedException, ExecutionException {
    for (double interval : new double[] { .95, .975, .99 }) {
        int[] testResults = new int[600];
        for (int test = 0; test < 100; test++) {
            PredictionProvider model = TestUtils.getNoisySumModel(pc.getRandom(), noise);
            ShapConfig skConfig = testConfig.withBackground(createPIFromMatrix(backgroundAllZeros)).withConfidence(interval).build();
            List<PredictionInput> toExplain = createPIFromMatrix(toExplainAllOnes);
            ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
            List<PredictionOutput> predictionOutputs = model.predictAsync(toExplain).get();
            Prediction p = new SimplePrediction(toExplain.get(0), predictionOutputs.get(0));
            Saliency[] saliencies = ske.explainAsync(p, model).get().getSaliencies();
            RealMatrix[] explanationsAndConfs = saliencyToMatrix(saliencies);
            RealMatrix explanations = explanationsAndConfs[0];
            RealMatrix confidence = explanationsAndConfs[1];
            for (int i = 0; i < explanations.getRowDimension(); i++) {
                for (int j = 0; j < explanations.getColumnDimension(); j++) {
                    double conf = confidence.getEntry(i, j);
                    double exp = explanations.getEntry(i, j);
                    // see if true value falls into confidence interval
                    testResults[test * 6 + j] = (exp + conf) > 1.0 & 1.0 > (exp - conf) ? 1 : 0;
                }
            }
        }
        // roughly interval% of the tests should be true
        double score = Arrays.stream(testResults).sum() / 600.;
        assertEquals(interval, score, .05);
    }
}
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) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)155 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)137 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)124 Prediction (org.kie.kogito.explainability.model.Prediction)122 Random (java.util.Random)90 Test (org.junit.jupiter.api.Test)90 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)89 Feature (org.kie.kogito.explainability.model.Feature)80 ArrayList (java.util.ArrayList)74 Output (org.kie.kogito.explainability.model.Output)65 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)65 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)55 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)52 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)50 Saliency (org.kie.kogito.explainability.model.Saliency)48 Value (org.kie.kogito.explainability.model.Value)47 LinkedList (java.util.LinkedList)37 List (java.util.List)36 LimeConfigOptimizer (org.kie.kogito.explainability.local.lime.optim.LimeConfigOptimizer)33 ValueSource (org.junit.jupiter.params.provider.ValueSource)32