Search in sources :

Example 76 with PredictionInput

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

the class RecordingLimeExplainerTest method testExplainNonOptimized.

@Test
void testExplainNonOptimized() throws ExecutionException, InterruptedException, TimeoutException {
    RecordingLimeExplainer limeExplainer = new RecordingLimeExplainer(10);
    List<Feature> features = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        features.add(TestUtils.getMockedNumericFeature(i));
    }
    PredictionInput input = new PredictionInput(features);
    PredictionProvider model = TestUtils.getSumSkipModel(0);
    PredictionOutput output = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit()).get(0);
    Prediction prediction = new SimplePrediction(input, output);
    Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
    assertNotNull(saliencyMap);
}
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) Feature(org.kie.kogito.explainability.model.Feature) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 77 with PredictionInput

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

the class RecordingLimeExplainerTest method testAutomaticConfigOptimization.

@ParameterizedTest
@ValueSource(longs = { 0 })
void testAutomaticConfigOptimization(long seed) throws Exception {
    PredictionProvider model = TestUtils.getSumThresholdModel(10, 10);
    PerturbationContext pc = new PerturbationContext(seed, new Random(), 1);
    LimeConfig config = new LimeConfig().withPerturbationContext(pc);
    RecordingLimeExplainer limeExplainer = new RecordingLimeExplainer(2);
    for (int i = 0; i < 50; i++) {
        List<Feature> features = new LinkedList<>();
        features.add(TestUtils.getMockedNumericFeature(Type.NUMBER.randomValue(pc).asNumber()));
        features.add(TestUtils.getMockedNumericFeature(Type.NUMBER.randomValue(pc).asNumber()));
        features.add(TestUtils.getMockedNumericFeature(Type.NUMBER.randomValue(pc).asNumber()));
        PredictionInput input = new PredictionInput(features);
        List<PredictionOutput> outputs = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
        Prediction prediction = new SimplePrediction(input, outputs.get(0));
        Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
        for (Saliency saliency : saliencyMap.values()) {
            assertNotNull(saliency);
        }
    }
    LimeConfig optimizedConfig = limeExplainer.getExecutionConfig();
    assertThat(optimizedConfig).isNotEqualTo(config);
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) 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) Feature(org.kie.kogito.explainability.model.Feature) LimeConfig(org.kie.kogito.explainability.local.lime.LimeConfig) LinkedList(java.util.LinkedList) Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 78 with PredictionInput

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

the class ShapKernelExplainerTest method testMultiVarianceOneOutputLogit.

// test a single output model with many varying features and logit link
@Test
void testMultiVarianceOneOutputLogit() throws InterruptedException, TimeoutException, ExecutionException {
    PredictionProvider model = TestUtils.getSumSkipModel(1);
    List<PredictionInput> background = createPIFromMatrix(backgroundLogit);
    ShapConfig skConfig = ShapConfig.builder().withBackground(background).withLink(ShapConfig.LinkType.LOGIT).withNSamples(100).withPC(pc).build();
    shapTestCase(model, skConfig, toExplainLogit, logitSHAP);
}
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 79 with PredictionInput

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

the class ShapKernelExplainerTest method shapTestCase.

/*
     * given a specific model, config, background, explanations, ske, and expected shap values,
     * test that the computed shape values match expected shap values
     */
private void shapTestCase(PredictionProvider model, ShapKernelExplainer ske, double[][] toExplainRaw, double[][][] expected) throws InterruptedException, TimeoutException, ExecutionException {
    // establish background data and desired data to explain
    List<PredictionInput> toExplain = createPIFromMatrix(toExplainRaw);
    // 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)));
    }
    // evaluate if the explanations match the expected value
    for (int i = 0; i < toExplain.size(); i++) {
        // explanations shape: outputSize x nfeatures
        Saliency[] explanationSaliencies = ske.explainAsync(predictions.get(i), model).get(5, TimeUnit.SECONDS).getSaliencies();
        RealMatrix explanations = saliencyToMatrix(explanationSaliencies)[0];
        for (int j = 0; j < explanations.getRowDimension(); j++) {
            assertArrayEquals(expected[i][j], explanations.getRow(j), 1e-6);
        }
    }
}
Also used : SimplePrediction(org.kie.kogito.explainability.model.SimplePrediction) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) 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)

Example 80 with PredictionInput

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

the class ShapKernelExplainerTest method testOneVarianceMultiOutput.

// test a multi-output model with one varying feature
@Test
void testOneVarianceMultiOutput() throws InterruptedException, TimeoutException, ExecutionException {
    PredictionProvider model = TestUtils.getSumSkipTwoOutputModel(1);
    List<PredictionInput> background = createPIFromMatrix(backgroundNoVariance);
    ShapConfig skConfig = testConfig.withBackground(background).build();
    shapTestCase(model, skConfig, toExplainOneVariance, oneVarianceMultiOutputSHAP);
}
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

PredictionInput (org.kie.kogito.explainability.model.PredictionInput)187 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)143 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)135 Prediction (org.kie.kogito.explainability.model.Prediction)126 Feature (org.kie.kogito.explainability.model.Feature)109 Test (org.junit.jupiter.api.Test)107 ArrayList (java.util.ArrayList)97 SimplePrediction (org.kie.kogito.explainability.model.SimplePrediction)95 Random (java.util.Random)86 PerturbationContext (org.kie.kogito.explainability.model.PerturbationContext)67 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)60 Output (org.kie.kogito.explainability.model.Output)55 LimeConfig (org.kie.kogito.explainability.local.lime.LimeConfig)54 LinkedList (java.util.LinkedList)53 LimeExplainer (org.kie.kogito.explainability.local.lime.LimeExplainer)52 Value (org.kie.kogito.explainability.model.Value)52 Saliency (org.kie.kogito.explainability.model.Saliency)50 List (java.util.List)39 LimeConfigOptimizer (org.kie.kogito.explainability.local.lime.optim.LimeConfigOptimizer)33 Type (org.kie.kogito.explainability.model.Type)31