use of org.kie.kogito.explainability.model.Saliency in project kogito-apps by kiegroup.
the class DummyModelsLimeExplainerTest method testUnusedFeatureClassification.
@ParameterizedTest
@ValueSource(longs = { 0 })
void testUnusedFeatureClassification(long seed) throws Exception {
Random random = new Random();
int idx = 2;
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f1", 6));
features.add(FeatureFactory.newNumericalFeature("f2", 3));
features.add(FeatureFactory.newNumericalFeature("f3", 5));
PredictionProvider model = TestUtils.getEvenSumModel(idx);
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));
LimeConfig limeConfig = new LimeConfig().withSamples(100).withPerturbationContext(new PerturbationContext(seed, random, 1));
LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (Saliency saliency : saliencyMap.values()) {
assertNotNull(saliency);
List<FeatureImportance> topFeatures = saliency.getTopFeatures(3);
assertEquals(3, topFeatures.size());
assertEquals(1d, ExplainabilityMetrics.impactScore(model, prediction, topFeatures));
}
int topK = 1;
double minimumPositiveStabilityRate = 0.5;
double minimumNegativeStabilityRate = 0.5;
TestUtils.assertLimeStability(model, prediction, limeExplainer, topK, minimumPositiveStabilityRate, minimumNegativeStabilityRate);
List<PredictionInput> inputs = new ArrayList<>();
for (int i = 0; i < 100; i++) {
List<Feature> fs = new LinkedList<>();
fs.add(TestUtils.getMockedNumericFeature());
fs.add(TestUtils.getMockedNumericFeature());
fs.add(TestUtils.getMockedNumericFeature());
inputs.add(new PredictionInput(fs));
}
DataDistribution distribution = new PredictionInputsDataDistribution(inputs);
int k = 2;
int chunkSize = 10;
String decision = "sum-even-but" + idx;
double precision = ExplainabilityMetrics.getLocalSaliencyPrecision(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(precision).isEqualTo(1);
double recall = ExplainabilityMetrics.getLocalSaliencyRecall(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(recall).isEqualTo(1);
double f1 = ExplainabilityMetrics.getLocalSaliencyF1(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(f1).isEqualTo(1);
}
use of org.kie.kogito.explainability.model.Saliency in project kogito-apps by kiegroup.
the class DummyModelsLimeExplainerTest method testTextSpamClassification.
@ParameterizedTest
@ValueSource(longs = { 0 })
void testTextSpamClassification(long seed) throws Exception {
Random random = new Random();
List<Feature> features = new LinkedList<>();
Function<String, List<String>> tokenizer = s -> Arrays.asList(s.split(" ").clone());
features.add(FeatureFactory.newFulltextFeature("f1", "we go here and there", tokenizer));
features.add(FeatureFactory.newFulltextFeature("f2", "please give me some money", tokenizer));
features.add(FeatureFactory.newFulltextFeature("f3", "dear friend, please reply", tokenizer));
PredictionInput input = new PredictionInput(features);
PredictionProvider model = TestUtils.getDummyTextClassifier();
List<PredictionOutput> outputs = model.predictAsync(List.of(input)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction prediction = new SimplePrediction(input, outputs.get(0));
LimeConfig limeConfig = new LimeConfig().withSamples(100).withPerturbationContext(new PerturbationContext(seed, random, 1));
LimeExplainer limeExplainer = new LimeExplainer(limeConfig);
Map<String, Saliency> saliencyMap = limeExplainer.explainAsync(prediction, model).toCompletableFuture().get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (Saliency saliency : saliencyMap.values()) {
assertNotNull(saliency);
List<FeatureImportance> topFeatures = saliency.getPositiveFeatures(1);
assertEquals(1, topFeatures.size());
assertEquals(1d, ExplainabilityMetrics.impactScore(model, prediction, topFeatures));
}
int topK = 1;
double minimumPositiveStabilityRate = 0.5;
double minimumNegativeStabilityRate = 0.2;
TestUtils.assertLimeStability(model, prediction, limeExplainer, topK, minimumPositiveStabilityRate, minimumNegativeStabilityRate);
List<PredictionInput> inputs = new ArrayList<>();
for (int i = 0; i < 100; i++) {
List<Feature> fs = new LinkedList<>();
fs.add(TestUtils.getMockedNumericFeature());
fs.add(TestUtils.getMockedNumericFeature());
fs.add(TestUtils.getMockedNumericFeature());
inputs.add(new PredictionInput(fs));
}
DataDistribution distribution = new PredictionInputsDataDistribution(inputs);
int k = 2;
int chunkSize = 10;
String decision = "spam";
double precision = ExplainabilityMetrics.getLocalSaliencyPrecision(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(precision).isEqualTo(1);
double recall = ExplainabilityMetrics.getLocalSaliencyRecall(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(recall).isEqualTo(1);
double f1 = ExplainabilityMetrics.getLocalSaliencyF1(decision, model, limeExplainer, distribution, k, chunkSize);
assertThat(f1).isEqualTo(1);
}
use of org.kie.kogito.explainability.model.Saliency 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);
}
use of org.kie.kogito.explainability.model.Saliency 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);
}
use of org.kie.kogito.explainability.model.Saliency 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);
}
}
}
Aggregations