Explainable AI explained! | #5 Counterfactual explanations and adversarial attacks

DeepFindr
16 Mar 202116:56

Summary

TLDRIn diesem Video wird die Erklärung von Gegenfaktualen in der Künstlichen Intelligenz vorgestellt. Der Sprecher erklärt, wie Gegenfaktuale dazu verwendet werden können, um einem Modellbenutzer zu zeigen, welche minimalen Änderungen an den Eingabedaten eine andere Vorhersage zur Folge haben. Das Beispiel einer Schlaganfallvorhersage zeigt, wie ein Patient durch Anpassungen seines Body-Mass-Index seine Risikobewertung verändern kann. Gegenfaktuale werden sowohl zur Erklärung von Blackbox-Modellen als auch zur Erzeugung von Angriffen verwendet. Verschiedene Berechnungsmethoden, wie zufällige Sampling oder genetische Algorithmen, werden ebenfalls besprochen.

Takeaways

  • 📊 Counterfactuals bieten eine alternative Erklärungsmethode für KI-Modelle, indem sie zeigen, was geändert werden könnte, um eine andere Vorhersage zu erhalten.
  • 🤖 Im Gegensatz zu Feature-Importance-Techniken fokussieren sich Counterfactuals darauf, wie kleine Änderungen in den Eingabewerten das Vorhersageergebnis beeinflussen können.
  • 🧑‍⚕️ Ein Beispiel im Video zeigt, wie John durch die Anpassung seines Body-Mass-Index seine Schlaganfallprognose von 90 % auf 70 % senken könnte.
  • 📝 Counterfactuals stellen die minimal notwendigen Änderungen dar, die erforderlich sind, um das Zielklassenergebnis zu ändern, wie von Lewis (1973) theoretisch definiert.
  • 📚 Der Ansatz wurde 2017 durch das Papier 'Counterfactual Explanations Without Opening the Black Box' in die maschinelle Lernforschung eingeführt.
  • ⚔️ Es gibt Ähnlichkeiten zwischen der Erzeugung von Gegenfakten und der Erstellung von adversarialen Beispielen, da beide auf minimale Änderungen abzielen, um unterschiedliche Ergebnisse zu erzielen.
  • ⚙️ Es gibt weiße und schwarze Box-Ansätze zur Berechnung von Gegenfakten, je nachdem, ob der Zugriff auf die Modelldetails besteht oder nicht.
  • 🧬 Einige Methoden zur Berechnung von Counterfactuals verwenden genetische Algorithmen, die Selektion, Mutation und Crossovers beinhalten.
  • 🔍 Microsoft bietet mit der Python-Bibliothek 'Dice' ein Tool zur Berechnung von kontrafaktischen Erklärungen, das verschiedene Gegenfakten generieren kann.
  • 🎲 Da es oft keine beste Gegenfaktenerklärung gibt, kann es sinnvoll sein, mehrere diverse Gegenfakten zu betrachten, um die passende Erklärung für die jeweilige Situation zu finden.

Q & A

  • Was sind Kontrafaktische Erklärungen im Vergleich zu Feature-Wichtigkeitstechniken wie SHAP und LIME?

    -Kontrafaktische Erklärungen geben an, welche minimalen Änderungen an den Eingabefeatures vorgenommen werden müssen, um das Modell zu einem anderen Ergebnis zu bringen, im Gegensatz zu SHAP und LIME, die die Wichtigkeit der Features und deren Einfluss auf die Vorhersage zeigen.

  • Wie könnte eine kontrafaktische Erklärung für den Patienten John aussehen?

    -Ein Beispiel für eine kontrafaktische Erklärung für John wäre: 'Momentan liegt dein Schlaganfallrisiko bei 90 %. Wenn du deinen Body-Mass-Index auf 25 senken würdest, würde sich das Risiko auf 70 % senken.'

  • Wie werden kontrafaktische Erklärungen mathematisch definiert?

    -Eine kontrafaktische Erklärung ist die kleinste Änderung in den Eingabefeatures, die die Vorhersage zu einem anderen Ergebnis führt, wie etwa von Schlaganfall zu keinem Schlaganfall.

  • Was ist der Unterschied zwischen kontrafaktischen und kontrastiven Erklärungen?

    -Kontrafaktische und kontrastive Erklärungen sind in der Literatur ähnlich; beide zielen darauf ab, die minimale Änderung zu identifizieren, die eine andere Vorhersage erzeugt.

  • Welche Rolle spielt die Entscheidungsmatrix bei der Generierung kontrafaktischer Erklärungen?

    -Die Entscheidungsmatrix stellt die verschiedenen Kombinationen der Feature-Werte dar, die die Grenze zwischen zwei verschiedenen Vorhersagen bestimmen. Durch Änderung einzelner Werte kann die minimal notwendige Änderung identifiziert werden, die die Vorhersage ändert.

  • Warum gibt es oft mehrere mögliche kontrafaktische Erklärungen?

    -Es gibt oft mehrere mögliche kontrafaktische Erklärungen, weil es verschiedene Kombinationen von Änderungen in den Features geben kann, die das gewünschte Ergebnis erzielen. Dieses Phänomen wird als Rashomon-Effekt bezeichnet.

  • Wie können kontrafaktische Erklärungen in Machine Learning genutzt werden?

    -Kontrafaktische Erklärungen können genutzt werden, um zu verstehen, welche Änderungen an den Eingabefeatures erforderlich wären, um eine bestimmte Vorhersage zu ändern, z. B. das Risiko eines Schlaganfalls zu senken oder einen Kredit zu erhalten.

  • Welche Rolle spielen White-Box- und Black-Box-Ansätze bei der Berechnung kontrafaktischer Erklärungen?

    -White-Box-Ansätze nutzen interne Modellinformationen wie Gewichte von Neuronalen Netzwerken zur Berechnung kontrafaktischer Erklärungen, während Black-Box-Ansätze das Modell mehrmals abfragen, um die Beziehung zwischen Eingabe und Ausgabe zu erkennen.

  • Was ist der Zweck von genetischen Algorithmen bei der Berechnung kontrafaktischer Erklärungen?

    -Genetische Algorithmen nutzen Auswahl, Mutation und Kreuzung, um kontrafaktische Erklärungen zu berechnen. Sie simulieren evolutionäre Prozesse, um die optimalen Änderungen zu finden.

  • Was ist der Nutzen der DICE-Bibliothek zur Berechnung kontrafaktischer Erklärungen?

    -Die DICE-Bibliothek von Microsoft kann mehrere kontrafaktische Erklärungen generieren, die möglichst unterschiedlich sind, damit Benutzer die am besten geeignete Erklärung für ihre spezifische Situation auswählen können.

Outlines

00:00

🎬 Einführung in Gegenfaktische Erklärungen

In diesem Abschnitt wird der Begriff der Gegenfaktischen Erklärungen eingeführt, die im Gegensatz zu traditionellen Interpretierbarkeitsmethoden wie SHAP oder LIME stehen. Der Sprecher erklärt anhand eines Beispiels, wie man durch minimale Anpassungen der Eingabedaten andere Vorhersagen erhalten kann, z. B. wie ein Patient durch die Senkung des BMI das Risiko eines Schlaganfalls reduzieren könnte. Gegenfaktische Erklärungen zeigen also, wie Änderungen bestimmter Faktoren zu alternativen Ergebnissen führen.

05:01

🤖 Gegenfaktische Erklärungen in der Praxis

Hier wird beschrieben, wie Gegenfaktische Erklärungen mathematisch berechnet werden und welche Ansätze (White-Box und Black-Box) dafür existieren. Der Fokus liegt darauf, wie diese Änderungen an den Eingabedaten durch Abfragen des Modells oder durch Zugriff auf die internen Modellparameter durchgeführt werden können. Es werden auch Beispiele für die Verwendung genetischer Algorithmen zur Berechnung von Gegenfaktischen genannt.

10:02

📊 Beispiel für eine Tabellendaten-Erklärung

Der Abschnitt zeigt an einem tabellarischen Datenbeispiel, wie kleine Änderungen an den Eingabewerten wie dem BMI zu einer neuen Vorhersage führen können. Der Sprecher erklärt, dass durch wiederholte Änderungen der Eingabe eine gute Annäherung an die Entscheidungsgrenze des Modells erreicht wird. Es wird auch die Rolle von Bibliotheken wie DICE (Diverse Counterfactual Explanations) hervorgehoben, die mehrere Gegenfaktische Erklärungen für dieselbe Situation bieten können.

15:03

🖥️ Implementierung von Gegenfaktischen Erklärungen in Python

In diesem Teil wird die praktische Implementierung der Gegenfaktischen Erklärungen in Python mit der DICE-Bibliothek erklärt. Der Sprecher zeigt, wie man die notwendigen Datenobjekte und Modellobjekte erstellt und wie man diverse Gegenfaktische generieren kann. Es wird gezeigt, wie man Eingabewerte ändert und die Änderungen visualisiert, um nachvollziehen zu können, welche Faktoren die Vorhersage beeinflussen.

⚙️ Einschränkungen und Optimierungen von Gegenfaktischen

Hier wird die Möglichkeit erörtert, Einschränkungen bei der Generierung von Gegenfaktischen festzulegen, um realistischere Ergebnisse zu erhalten. Der Sprecher erklärt, wie man machbare Gegenfaktische sicherstellen kann, indem man zulässige Wertebereiche für die Änderungen definiert. Es wird auch darauf hingewiesen, dass die Vielfalt der Gegenfaktischen je nach Modell begrenzt sein kann.

Mindmap

Keywords

💡Erklärbare Künstliche Intelligenz (Explainable AI)

Erklärbare Künstliche Intelligenz (XAI) bezieht sich auf Techniken, die darauf abzielen, die Entscheidungsprozesse von Black-Box-Modellen in maschinellem Lernen verständlicher zu machen. Im Video werden verschiedene Methoden vorgestellt, wie beispielsweise SHAP und LIME, die genutzt werden, um die Bedeutung von Merkmalen und den Einfluss von Eingaben auf Vorhersagen zu erklären. Diese Techniken machen es möglich, die Funktionsweise komplexer Modelle für Menschen nachvollziehbar zu machen.

💡Kontrafaktische Erklärungen

Kontrafaktische Erklärungen bieten alternative Szenarien, die zu einer anderen Vorhersage führen würden. Im Video wird das Beispiel eines Patienten namens John verwendet, dessen Schlaganfallrisiko auf 90 % geschätzt wird. Eine kontrafaktische Erklärung könnte John raten, seinen Body-Mass-Index auf 25 zu senken, um das Risiko auf 70 % zu reduzieren. Der Fokus liegt auf den minimalen Änderungen, die nötig sind, um das Ergebnis zu beeinflussen.

💡Black-Box-Modelle

Black-Box-Modelle sind maschinelle Lernmodelle, deren interne Entscheidungsprozesse schwer zu interpretieren sind. Obwohl sie oft sehr genaue Vorhersagen liefern, ist es für Benutzer oft unklar, wie diese Ergebnisse zustande kommen. Das Video thematisiert, wie kontrafaktische Erklärungen und andere Methoden genutzt werden können, um diese Black-Box-Modelle verständlicher zu machen.

💡Merkmalsbedeutung (Feature Importance)

Merkmalsbedeutung bezieht sich darauf, wie stark bestimmte Eingaben das Ergebnis eines Modells beeinflussen. In früheren Videos der Serie wurden Methoden wie SHAP und LIME verwendet, um zu zeigen, welche Merkmale die größte Auswirkung auf die Vorhersage eines Modells haben. Im Gegensatz dazu bieten kontrafaktische Erklärungen konkrete Handlungsanweisungen, wie die Änderung eines spezifischen Merkmals das Ergebnis verändern würde.

💡Optimierungsproblem

Ein Optimierungsproblem bezieht sich auf den Prozess, den optimalen Wert für eine Funktion zu finden. Im Kontext von kontrafaktischen Erklärungen versucht man, die minimalen Änderungen an den Eingaben zu finden, die zu einer anderen Vorhersage führen. Dies wird im Video durch das Beispiel der Änderung des Body-Mass-Index von John veranschaulicht, um das Risiko eines Schlaganfalls zu verringern.

💡Model Agnostic

Der Begriff 'Model Agnostic' beschreibt Ansätze, die unabhängig von den spezifischen inneren Mechanismen eines Modells funktionieren. Diese Methoden benötigen keinen Zugang zu den internen Parametern oder Gewichten des Modells. Das Video zeigt, dass kontrafaktische Erklärungen sowohl mit 'White-Box'- als auch mit 'Black-Box'-Modellen erstellt werden können, wobei 'Model Agnostic'-Methoden genutzt werden, wenn kein Zugriff auf das Modell selbst besteht.

💡Entscheidungsgrenze (Decision Boundary)

Eine Entscheidungsgrenze ist die Linie oder Grenze, die verschiedene Klassen in einem Klassifikationsproblem trennt. Das Video zeigt, wie kontrafaktische Erklärungen genutzt werden, um diese Grenze zu verstehen und wie minimal nötige Änderungen an den Eingabedaten vorgenommen werden können, um die Vorhersage eines Modells zu verändern.

💡Rashomon-Effekt

Der Rashomon-Effekt beschreibt die Situation, in der mehrere gleichwertige Erklärungen für das gleiche Ereignis existieren. Im Video wird dies im Kontext kontrafaktischer Erklärungen diskutiert, da es oft mehrere mögliche Änderungen an den Eingaben gibt, die zu der gleichen Vorhersage führen. Zum Beispiel könnten unterschiedliche Kombinationen von Einkommens- und Kredithistorien zu einer erfolgreichen Kreditvergabe führen.

💡Adversarielle Angriffe

Adversarielle Angriffe beziehen sich auf gezielte Änderungen von Eingabedaten, um ein Modell zu täuschen und falsche Vorhersagen zu erzeugen. Das Video vergleicht adversarielle Angriffe mit kontrafaktischen Erklärungen, da beide minimale Änderungen in den Eingabedaten verwenden, um die Modellvorhersagen zu beeinflussen. Während kontrafaktische Erklärungen nützlich sind, um Modelle verständlicher zu machen, werden adversarielle Angriffe oft genutzt, um Schwachstellen von Modellen auszunutzen.

💡Gradientenabstieg (Gradient Descent)

Der Gradientenabstieg ist ein Optimierungsalgorithmus, der verwendet wird, um den optimalen Satz von Parametern für ein Modell zu finden. In White-Box-Modellen, wie neuronalen Netzen, kann der Gradientenabstieg verwendet werden, um kontrafaktische Erklärungen effizient zu berechnen. Das Video erwähnt, dass dies ein schnellerer Ansatz sein kann, um minimale Änderungen zu identifizieren, die das Modell dazu bringen, eine andere Vorhersage zu machen.

Highlights

Introduction to explainable AI series and focus on interpretability techniques.

Shift from feature importance to counterfactual explanations, providing actionable insights.

Definition of counterfactuals: Minimum input change that alters prediction to a different output.

Example of counterfactuals using a stroke prediction model: Changing BMI to lower stroke risk.

Explanation of contrastive explanations: How they differ from feature importance methods.

Discussion on the historical background of counterfactuals in psychology and AI.

Comparison between counterfactuals and adversarial examples, both aiming to change model outputs.

Overview of optimization problems used to find counterfactuals or adversarial samples.

Introduction to white-box and black-box approaches to compute counterfactuals.

Detailed example of generating counterfactuals for tabular data using model-agnostic methods.

Mention of Dice library for creating diverse counterfactual explanations in Python.

Discussion of the Rashomon effect: Multiple valid counterfactuals for the same prediction.

Use of genetic algorithms and gradient-based methods to generate counterfactuals.

Demonstration of code to generate counterfactuals using a random forest model.

Conclusion and mention of upcoming video on layer-wise relevance propagation for neural networks.

Transcripts

play00:00

[Music]

play00:04

hi everyone welcome back to this

play00:06

explainable ai series in the last videos

play00:09

we've seen different interpretability

play00:11

techniques to better understand black

play00:13

box machine learning models

play00:15

these methods mainly produced feature

play00:17

importances and showed us how the inputs

play00:19

affect our prediction

play00:21

today i want to talk about another type

play00:23

of explanation

play00:24

which is usually called a counterfactual

play00:27

in my opinion it's a pretty powerful

play00:29

approach

play00:30

and i hope that at the end of this video

play00:32

you will be familiar with it as well

play00:36

let's start with an intuitive basic

play00:38

explanation of what counterfactuals are

play00:41

in the second video i introduced the

play00:43

data set for this series which is a

play00:45

binary classification problem for

play00:47

stroke prediction we said that our test

play00:50

patient

play00:51

john is interested in why he gets a

play00:53

certain prediction from our model

play00:55

previously our explanations using sharp

play00:58

and lime

play00:59

had a form like h is the most important

play01:01

feature or

play01:02

the higher h gets the higher the stroke

play01:04

risk will be and so on

play01:06

so these were mainly feature importances

play01:08

and dependency plots

play01:10

now counterfactuals go in another

play01:12

direction

play01:13

here we want to tell john what he could

play01:15

do to avoid a stroke so it's a counter

play01:18

fact

play01:19

that gives him the possibility to change

play01:21

the situation

play01:22

a counterfactual could look like this

play01:25

hey john

play01:26

right now your prediction is 90 stroke

play01:29

if you would decrease your body mass

play01:31

index to 25

play01:33

the prediction would be 70 no stroke so

play01:36

the orange part here is now the

play01:38

counterfactual

play01:39

which is just another data point that

play01:41

leads to a different prediction

play01:43

we can think of it like a newly created

play01:45

person that would end up with

play01:47

no stroke so all values for our data

play01:50

point for john stay the same

play01:52

just body mass index is changed to

play01:54

another value

play01:56

and if this is done we get another

play01:58

output for our black box model

play01:59

which is highlighted in green we can

play02:02

also give a more

play02:03

formal prediction for counterfactuals

play02:06

generally a counterfactual

play02:08

is the smallest change in the input

play02:10

features

play02:11

that changes the prediction to another

play02:12

output just like in our example going

play02:15

from stroke to no stroke

play02:18

just a side note counterfactual

play02:20

explanations are sometimes also called

play02:22

contrastive explanations in the

play02:24

literature

play02:25

so here i wanted to visualize the basic

play02:27

idea for tabular data

play02:30

one instance so one row in the tabular

play02:32

data set

play02:33

corresponds to one person and the

play02:36

columns are the different features

play02:38

our model uses so age body mass index

play02:40

and so on

play02:42

so all we have to do now is change a

play02:44

specific value of our inputs

play02:46

so that the prediction alters to the

play02:48

target class for example no

play02:50

stroke it was shown that providing

play02:53

explanations of that sort so

play02:54

counterfactuals

play02:56

lead to a high explanatory value for

play02:58

humans

play02:59

counterfactuals exist already a long

play03:01

time in psychology

play03:03

a theoretical definition was initially

play03:05

presented by

play03:06

lewis in 1973. the idea for using them

play03:10

in machine learning was

play03:11

first presented in a paper called

play03:13

counterfactual explanations without

play03:15

opening the black box

play03:17

this paper was published in 2017

play03:21

the basic idea for calculating

play03:22

counterfactuals comes from a different

play03:24

ai

play03:25

field which is also called ai safety

play03:28

the goal in this field is to make

play03:30

machine learning models more secure

play03:32

against manipulation adversarial

play03:35

examples are

play03:36

well-designed input samples that use the

play03:39

shortages

play03:40

of machine learning algorithms to

play03:42

generate false predictions

play03:44

an example for this is this classifier

play03:47

for which the input on the left

play03:49

is slightly changed by adding

play03:51

adversarial noise in the middle

play03:53

as a result the prediction changes from

play03:55

panda to gibbon

play03:58

the approach to generate adversary

play04:00

examples is quite similar to

play04:02

generating counterfactuals as both look

play04:04

for the minimal changes in the input

play04:06

to generate a different output in both

play04:09

cases we want to solve the following

play04:11

optimization problem

play04:13

find x prime which is the counterfactual

play04:16

or

play04:16

adversarial sample that changes the

play04:18

prediction of our black box model

play04:20

to a target class c in the example on

play04:24

the left

play04:24

access the panda image with a panda

play04:27

prediction

play04:28

and on the right we would have x prime

play04:30

which is the adjusted input

play04:32

that leads to a given prediction in this

play04:35

optimization problem on the right

play04:37

we also see a distance function d which

play04:39

is minimized

play04:41

this distance function makes sure that

play04:42

we stay as close

play04:44

as possible to the original inputs

play04:46

that's because we still want to have a

play04:48

panda on the image

play04:49

but aim to get a different prediction by

play04:52

our blackbox model

play04:54

so to summarize it in both cases when

play04:57

explaining black boxes and also when

play04:59

attacking black boxes

play05:01

we want to find a similar input data

play05:03

point

play05:04

that changes the prediction if you are

play05:06

further interested in the second case

play05:08

adversarial attacks i've also uploaded a

play05:11

video on how to code that for

play05:13

convolutional neural networks

play05:15

okay so now we know what counter factors

play05:18

are

play05:18

and how they can be formulated

play05:20

mathematically but

play05:22

how do we calculate them in real life

play05:24

there exist

play05:25

several approaches to compute

play05:27

contractuals for a prediction

play05:29

all we need to do is solve the

play05:31

previously presented optimization

play05:33

problem

play05:34

generally the approaches can be divided

play05:36

into white box and black box approaches

play05:40

if we have access to the model internals

play05:42

such as the weights in the neural

play05:43

network

play05:44

then that is perfect and we can use this

play05:46

information to faster find the minimum

play05:48

in the optimization problem

play05:50

for example in a neural network we can

play05:52

simply calculate gradients for our

play05:54

problem

play05:55

that guide us using gradient descent if

play05:58

we don't have access

play06:00

we need to rely on the relationship

play06:02

between inputs and outputs

play06:04

that means we have to find a solution by

play06:06

querying the model many times

play06:08

that is the model agnostic way of

play06:11

calculating counterfactuals

play06:13

while the white box approaches on the

play06:14

left are

play06:16

model specific for both categories many

play06:20

different ideas exist and i won't go

play06:22

into the detail of the individual papers

play06:24

however here i linked some references if

play06:26

you want to read more about this topic

play06:28

just an example the work for certif ai

play06:32

uses a genetic algorithm to create

play06:34

counterfactuals

play06:36

that means it uses selection mutation

play06:38

and crossovers

play06:39

so the typical operations in

play06:41

evolutionary algorithms

play06:43

the work by vachta et al is model

play06:46

specific

play06:47

and uses an atom optimizer and gradient

play06:49

descent to find the optimal

play06:50

counterfactual

play06:52

all of these approaches perform some

play06:53

sort of perturbation on the input

play06:55

that means they change the feature

play06:57

values either randomly or guided in some

play06:59

way

play07:00

to better explain this let's again

play07:02

quickly jump to our tabular example

play07:05

here we would for instance randomly

play07:07

change values

play07:08

such as here we decrease the body mass

play07:10

index to 32

play07:12

we do this many times for example again

play07:15

for 29

play07:16

and then again for 22.

play07:22

the last change would flip the

play07:23

prediction to no stroke

play07:25

if we do this many times we get a good

play07:28

approximation of the decision boundary

play07:30

for our model and can determine the

play07:32

minimal changes that are required to

play07:34

predict no stroke

play07:36

and then we can use this information and

play07:38

tell to this person for example john

play07:40

hey if you decrease your body mass index

play07:42

to 22 you will probably not get a stroke

play07:46

again this approach is model agnostic

play07:48

and quite similar to brute force

play07:50

but if we can use the model internals we

play07:53

can apply these feature changes more

play07:55

intelligently

play07:56

this approximation of the decision

play07:58

boundary is also nicely visualized by

play08:00

this example from

play08:01

dice which is a counterfactuals python

play08:04

library by

play08:05

microsoft in this example they want to

play08:08

provide an explanation

play08:10

why a loan has been approved or rejected

play08:13

and how to change the situation

play08:15

at this point you might say wait a

play08:17

minute there are many different

play08:19

possibilities for counterfactuals

play08:21

in this example we can either increase

play08:23

the income by ten thousand dollars

play08:25

or we increase the income by five

play08:27

thousand and have

play08:29

one more year of credit history both

play08:32

will get us on the other side so both

play08:34

will get the loan approved

play08:36

so what is the best counterfactual well

play08:39

there is no best

play08:41

both are valid counterfactuals and we

play08:43

cannot really say which one is better

play08:45

this behavior is also known as the

play08:48

rashomon effect

play08:49

that's why this python library i just

play08:51

talked about produces several

play08:53

counterfactuals

play08:54

so for example five dice so the name of

play08:58

the library stands for

play08:59

diverse counterfactual explanations that

play09:02

simply means we want

play09:04

several counterfactuals that are as

play09:06

different as possible

play09:08

this gives us the option to select the

play09:10

most suitable explanation for our

play09:12

personal situation so now that we are

play09:16

familiar with all the concepts

play09:18

let's switch to the code and compute

play09:19

some counterfactuals

play09:21

so here we are in vs code again and just

play09:24

like in the other videos

play09:25

we imported the data the random first

play09:28

model which is our black box classifier

play09:30

and some metrics to calculate the

play09:32

performance of our model

play09:34

so let's run this and you've seen this

play09:38

many times now

play09:39

import the data and we have

play09:43

one second we have a shape of that for

play09:46

the test and train data

play09:47

and then we fit the model which again is

play09:51

having a nice accuracy but imbalanced

play09:54

data sets so

play09:55

our f1 score is not so nice all right

play09:59

and now we come to the contractual part

play10:02

so

play10:02

this is the library i previously talked

play10:05

about

play10:06

you can install it running pip install

play10:08

diceml

play10:09

and it will create several so diverse

play10:12

contractual explanations for us

play10:15

for this library we need to create two

play10:17

things we need a data object

play10:19

and a model object the data object tells

play10:22

us

play10:23

what the data input looks like if there

play10:26

are continuous features

play10:27

that's important because if we want to

play10:29

perturbate the features for example

play10:31

increase or decrease

play10:32

age we need to consider discrete

play10:36

features and continuous features because

play10:37

they have different

play10:38

perturbation strategies so we need to

play10:41

manually pass in

play10:42

which features are continuous in our

play10:44

data set and then we also specify here

play10:46

what is our target variable

play10:49

and then here in the data frame section

play10:51

we pass in the data from our data loader

play10:55

okay in the model part we simply pass

play10:57

the model

play10:58

and we select the back ends so there are

play11:01

back ends for tensorflow pytorch

play11:03

and many others and i will select cycle

play11:06

learn

play11:06

as this model comes from scikit-learn

play11:09

so now using those two things so the

play11:12

data which is called data dice here

play11:14

and the random forest dies which is the

play11:16

model

play11:17

object we can create this explainer

play11:20

instance

play11:21

which is called dice and here we can

play11:24

specify

play11:25

which methods we want to use for

play11:27

generating counterfactuals

play11:29

as i said there exist model agnostic in

play11:31

model specific approaches

play11:33

the most model agnostic one is random

play11:35

sampling

play11:36

but also genetic algorithms and other

play11:38

optimizers are available

play11:40

if you have a deep learning model you

play11:42

can of course use an optimizer for deep

play11:45

learning

play11:46

that simply uses the gradients so here's

play11:48

an overview

play11:49

this is the github page from dice and

play11:52

here are

play11:52

some of the methods so they call it

play11:54

gradient based so that's

play11:56

model specific methods specifically

play11:58

designed for neural networks

play12:00

and for model agnostic approaches they

play12:01

have those three

play12:03

okay that's pretty much it regarding

play12:05

what we need

play12:06

and now what we can do is let me just

play12:08

quickly run this

play12:10

now we can select input data points so

play12:13

as i said

play12:14

this approach is a local explanability

play12:16

approach so we need to select

play12:18

a single data input we can use for our

play12:21

generation

play12:22

and then we call this function generate

play12:24

counterfactuals

play12:25

on the explainer object we created over

play12:28

here

play12:28

and we pass our input data point which

play12:30

is simply a first

play12:31

sample in my test data set and here i

play12:34

specify

play12:35

i want three counterfactuals so three

play12:38

diverse counterfactuals to be generated

play12:41

and as this is a binary classification

play12:44

problem so the stroke prediction data

play12:45

set

play12:46

i can simply pass in here opposite so it

play12:49

will flip

play12:50

the class so let's run this

play12:53

and the second function down here is

play12:56

then

play12:57

so this generates us the counterfactual

play12:59

and then i can call on this

play13:01

counterfactual objects a function called

play13:03

visualize as data frame

play13:05

and this will show us what is the input

play13:08

so what is this

play13:09

individual data sample and what are the

play13:12

three counterfactuals that were

play13:13

generated

play13:14

okay so first let's have a look at this

play13:17

query instance

play13:18

so we see that the original

play13:20

classification was

play13:22

zero so no stroke for this first data

play13:25

point

play13:26

and this is based on our random forest

play13:29

model

play13:30

and we see this is a male uh merits

play13:33

and all the other features and we can

play13:36

see 70 years old

play13:38

body mass index of 30.4 and a glucose

play13:42

level of 72.

play13:45

and now when i call this visualize as

play13:47

data frame

play13:48

i can pass in show only changes

play13:51

that means this table down here will

play13:54

only

play13:55

show the differences to this data points

play13:58

so as i said a counterfactual is just

play14:00

another data point

play14:01

that is slightly changed from this

play14:03

individual

play14:04

input and we can see

play14:07

we see some values that changed in the

play14:09

middle here which is

play14:10

work type private but especially we have

play14:14

many changes regarding body mass index

play14:17

so apparently if the body mass index is

play14:19

too low the stroke risk

play14:21

increases again i wouldn't trust this

play14:23

model but this is just for this example

play14:26

so here we have three counterfactuals

play14:29

that suggest us to

play14:30

decrease the body mass index to get a

play14:33

stroke prediction

play14:34

so usually we would go in the other

play14:37

direction and say

play14:38

when do you not get a stroke prediction

play14:40

but in this example we just select

play14:42

stroke because

play14:42

this first data point is no stroke

play14:46

okay so of course body mass index 0.9

play14:49

doesn't really make sense that's why we

play14:52

also have the option to pass

play14:53

in feasibility criteria for example you

play14:56

can say

play14:58

only change those features because

play15:00

usually you cannot change from

play15:02

male to female for example

play15:05

and additionally we can also say the

play15:08

permitted ranges

play15:09

for changes must lie between those two

play15:12

values

play15:13

and there are in this library a couple

play15:15

of additional options to

play15:18

ensure that we get feasible

play15:19

counterfactuals so counterfactuals that

play15:21

actually make sense

play15:23

so if i run this again i can pass in

play15:25

those two parameters

play15:27

into this generate counterfactuals

play15:28

function again again we want to generate

play15:31

three counterfactuals for the opposite

play15:33

class

play15:34

but now using this feasibility criteria

play15:37

so again we get the same outputs but now

play15:40

we should see okay here we have changes

play15:44

that

play15:44

lie within the permitted range so

play15:48

now it says you need to change the body

play15:50

mass index to the specific value and no

play15:54

other changes in this example so if you

play15:57

would have a more complex models you

play15:59

would have changes at several positions

play16:01

but again a counterfactual

play16:02

is the minimal change to change the

play16:05

prediction

play16:06

and here apparently the minimal change

play16:09

is to decrease the body mass index by

play16:12

around 10 and again we also don't have a

play16:15

lot of diversity in this model

play16:18

that's why those three diverse

play16:20

counterfactuals are still quite similar

play16:23

that's just because the model is not so

play16:25

good

play16:26

alright that's it about counterfactual

play16:28

explanations today i hope you find them

play16:30

as powerful as i do

play16:31

and if you have further questions just

play16:33

let me know in the comments

play16:35

the code is again available on github

play16:38

the link is in the description

play16:40

the next video will be the last part of

play16:42

this explainable ai series

play16:45

where we have a look at layer-wise

play16:47

relevance propagation

play16:48

a method which is specifically designed

play16:50

for neural networks

Rate This

5.0 / 5 (0 votes)

Related Tags
KI ErklärbarkeitKontrafaktische AnalyseMaschinelles LernenStroke PredictionModellinterpretationBlackbox ModelleAdversarial BeispieleErklärbare KIOptimierungsproblemeFeature-Wichtigkeit
Do you need a summary in English?