use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class PmmlRegressionLimeExplainerTest method testExplanationStabilityWithOptimization.
@Test
void testExplanationStabilityWithOptimization() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
List<PredictionInput> samples = getSamples();
List<PredictionOutput> predictionOutputs = model.predictAsync(samples.subList(0, 5)).get();
List<Prediction> predictions = DataUtils.getPredictions(samples, predictionOutputs);
long seed = 0;
LimeConfigOptimizer limeConfigOptimizer = new LimeConfigOptimizer().withDeterministicExecution(true);
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(seed, random, 1);
LimeConfig initialConfig = new LimeConfig().withPerturbationContext(perturbationContext);
LimeConfig optimizedConfig = limeConfigOptimizer.optimize(initialConfig, predictions, model);
assertThat(optimizedConfig).isNotSameAs(initialConfig);
LimeExplainer limeExplainer = new LimeExplainer(optimizedConfig);
PredictionInput testPredictionInput = getTestInput();
List<PredictionOutput> testPredictionOutputs = model.predictAsync(List.of(testPredictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction instance = new SimplePrediction(testPredictionInput, testPredictionOutputs.get(0));
assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, instance, limeExplainer, 1, 0.3, 0.3));
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class PmmlRegressionLimeExplainerTest method testExplanationImpactScoreWithOptimization.
@Test
void testExplanationImpactScoreWithOptimization() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
List<PredictionInput> samples = getSamples();
List<PredictionOutput> predictionOutputs = model.predictAsync(samples.subList(0, 5)).get();
List<Prediction> predictions = DataUtils.getPredictions(samples, predictionOutputs);
long seed = 0;
LimeConfigOptimizer limeConfigOptimizer = new LimeConfigOptimizer().withDeterministicExecution(true).forImpactScore();
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(seed, random, 1);
LimeConfig initialConfig = new LimeConfig().withPerturbationContext(perturbationContext);
LimeConfig optimizedConfig = limeConfigOptimizer.optimize(initialConfig, predictions, model);
assertThat(optimizedConfig).isNotSameAs(initialConfig);
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class PmmlScorecardCategoricalLimeExplainerTest method testExplanationStabilityWithOptimization.
@Test
void testExplanationStabilityWithOptimization() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
List<PredictionInput> samples = getSamples();
List<PredictionOutput> predictionOutputs = model.predictAsync(samples.subList(0, 5)).get();
List<Prediction> predictions = DataUtils.getPredictions(samples, predictionOutputs);
long seed = 0;
LimeConfigOptimizer limeConfigOptimizer = new LimeConfigOptimizer().withDeterministicExecution(true);
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(seed, random, 1);
LimeConfig initialConfig = new LimeConfig().withSamples(10).withPerturbationContext(perturbationContext);
LimeConfig optimizedConfig = limeConfigOptimizer.optimize(initialConfig, predictions, model);
assertThat(optimizedConfig).isNotSameAs(initialConfig);
LimeExplainer limeExplainer = new LimeExplainer(optimizedConfig);
PredictionInput testPredictionInput = getTestInput();
List<PredictionOutput> testPredictionOutputs = model.predictAsync(List.of(testPredictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction instance = new SimplePrediction(testPredictionInput, testPredictionOutputs.get(0));
assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, instance, limeExplainer, 1, 0.5, 0.5));
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class ValidationUtilsTest method testStableEval.
@Test
void testStableEval() throws ExecutionException, InterruptedException, TimeoutException, ValidationUtils.ValidationException {
for (int n = 0; n < 10; n++) {
Random random = new Random();
PerturbationContext perturbationContext = new PerturbationContext(4L, random, 1);
LimeConfig config = new LimeConfig().withPerturbationContext(perturbationContext);
LimeExplainer explainer = new LimeExplainer(config);
PredictionProvider model = TestUtils.getSumThresholdModel(0.1, 0.1);
List<Feature> features = new ArrayList<>();
for (int i = 0; i < 4; i++) {
features.add(FeatureFactory.newNumericalFeature("f-" + i, Type.NUMBER.randomValue(perturbationContext).asNumber()));
}
PredictionInput input = new PredictionInput(features);
List<PredictionOutput> outputs = model.predictAsync(List.of(input)).get(Config.DEFAULT_ASYNC_TIMEOUT, Config.DEFAULT_ASYNC_TIMEUNIT);
Prediction prediction = new SimplePrediction(input, outputs.get(0));
int topK = 1;
double posScore = 0.6;
double minScore = 0.6;
ValidationUtils.validateLocalSaliencyStability(model, prediction, explainer, topK, posScore, minScore);
}
}
use of org.kie.kogito.explainability.model.PerturbationContext in project kogito-apps by kiegroup.
the class FraudScoringDmnLimeExplainerTest method testExplanationWithDataDistribution.
@Test
void testExplanationWithDataDistribution() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
List<PredictionInput> samples = DmnTestUtils.randomFraudScoringInputs();
List<PredictionInput> inputs = samples.subList(0, 10);
Random random = new Random();
random.setSeed(0);
PerturbationContext perturbationContext = new PerturbationContext(random, 1);
LimeConfig initialConfig = new LimeConfig().withSamples(10).withDataDistribution(new PredictionInputsDataDistribution(inputs)).withPerturbationContext(perturbationContext);
LimeExplainer limeExplainer = new LimeExplainer(initialConfig);
PredictionInput testPredictionInput = getTestInput();
List<PredictionOutput> testPredictionOutputs = model.predictAsync(List.of(testPredictionInput)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
Prediction instance = new SimplePrediction(testPredictionInput, testPredictionOutputs.get(0));
assertDoesNotThrow(() -> ValidationUtils.validateLocalSaliencyStability(model, instance, limeExplainer, 1, 0.5, 0.5));
}
Aggregations