use of org.kie.kogito.explainability.model.Prediction in project kogito-apps by kiegroup.
the class RecordingLimeExplainerTest method testParallel.
@Test
void testParallel() throws InterruptedException, ExecutionException, TimeoutException {
int capacity = 10;
RecordingLimeExplainer recordingLimeExplainer = new RecordingLimeExplainer(capacity);
PredictionProvider model = mock(PredictionProvider.class);
Callable<?> callable = () -> {
for (int i = 0; i < 10000; i++) {
Prediction prediction = mock(Prediction.class);
try {
recordingLimeExplainer.explainAsync(prediction, model).get(Config.DEFAULT_ASYNC_TIMEOUT, Config.DEFAULT_ASYNC_TIMEUNIT);
} catch (Exception e) {
// ignored for the sake of the test
}
}
return null;
};
Collection<Future<?>> futures = new ArrayList<>();
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 4; i++) {
futures.add(executorService.submit(callable));
}
for (Future<?> f : futures) {
f.get(1, TimeUnit.MINUTES);
}
assertThat(recordingLimeExplainer.getRecordedPredictions().size()).isEqualTo(capacity);
}
use of org.kie.kogito.explainability.model.Prediction in project kogito-apps by kiegroup.
the class ShapKernelExplainerTest method shapTestCase.
/*
* given a specific model, config, background, explanations, and expected shap values,
* test that the computed shape values match expected shap values
*/
private void shapTestCase(PredictionProvider model, ShapConfig skConfig, 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
ShapKernelExplainer ske = new ShapKernelExplainer(skConfig);
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);
}
}
}
use of org.kie.kogito.explainability.model.Prediction in project kogito-apps by kiegroup.
the class ShapKernelExplainerTest method testManyFeatureRegularization.
@Test
void testManyFeatureRegularization() throws ExecutionException, InterruptedException {
RealVector modelWeights = MatrixUtils.createRealMatrix(generateN(1, 25, "5021")).getRowVector(0);
PredictionProvider model = TestUtils.getLinearModel(modelWeights.toArray());
RealMatrix data = MatrixUtils.createRealMatrix(generateN(101, 25, "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(data.getSubMatrix(0, 99, 0, 24).getData());
List<ShapConfig.Builder> testConfigs = List.of(testConfig.copy().withBackground(bg).withRegularizer(ShapConfig.RegularizerType.AIC), testConfig.copy().withBackground(bg).withRegularizer(ShapConfig.RegularizerType.BIC), testConfig.copy().withBackground(bg).withRegularizer(10), testConfig.copy().withBackground(bg).withRegularizer(ShapConfig.RegularizerType.NONE));
List<Integer> nsamples = List.of(1000, 2000, 5000);
for (Integer nsamp : nsamples) {
for (ShapConfig.Builder sk : testConfigs) {
ShapKernelExplainer ske = new ShapKernelExplainer(sk.withNSamples(nsamp).build());
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);
double coefMSE = (data.getRowVector(100).ebeMultiply(modelWeights)).getDistance(explanations.getRowVector(0));
assertTrue(coefMSE < 10);
}
}
}
use of org.kie.kogito.explainability.model.Prediction in project kogito-apps by kiegroup.
the class ShapKernelExplainerTest method testLargeBackground.
// Test cases where search space cannot be fully enumerated ========================================================
@Test
void testLargeBackground() 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).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);
}
}
}
use of org.kie.kogito.explainability.model.Prediction in project kogito-apps by kiegroup.
the class ShapKernelExplainerTest method testExceedSubsetSamplerRange.
@Test
void testExceedSubsetSamplerRange() throws ExecutionException, InterruptedException {
RealVector modelWeights = MatrixUtils.createRealMatrix(generateN(1, 50, "5021")).getRowVector(0);
PredictionProvider model = TestUtils.getLinearModel(modelWeights.toArray());
RealMatrix data = MatrixUtils.createRealMatrix(generateN(101, 50, "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][50]);
ShapConfig sk = testConfig.copy().withBackground(bg).withRegularizer(ShapConfig.RegularizerType.AIC).withNSamples(2000).build();
ShapKernelExplainer ske = new ShapKernelExplainer(sk);
ShapResults shapResults = ske.explainAsync(p, model).get();
assertTrue(true);
}
Aggregations