Giter Site home page Giter Site logo

pietrobarbiero / pytorch_explain Goto Github PK

View Code? Open in Web Editor NEW
127.0 12.0 10.0 43.09 MB

PyTorch Explain: Interpretable Deep Learning in Python.

License: Apache License 2.0

Python 9.08% Jupyter Notebook 90.92%
logic pytorch python machine-learning neural-network deep-learning explainability explainable-ai lens entropy

pytorch_explain's Introduction

image


Travis (.com) Codecov

Read the Docs (version) Requires.io

PyPI license PyPI

image

PyTorch, Explain! is an extension library for PyTorch to develop explainable deep learning models going beyond the current accuracy-interpretability trade-off.

The library includes a set of tools to develop:

  • Deep Concept Reasoner (Deep CoRe): an interpretable concept-based model going beyond the current accuracy-interpretability trade-off;
  • Concept Embedding Models (CEMs): a class of concept-based models going beyond the current accuracy-explainability trade-off;
  • Logic Explained Networks (LENs): a class of concept-based models generating accurate compound logic explanations for their predictions without the need for a post-hoc explainer.

Table of Content

Quick start

You can install torch_explain along with all its dependencies from PyPI:

pip install torch-explain

Quick tutorial on Concept Embedding Models

Using concept embeddings we can solve concept-based problems very efficiently! For this simple tutorial, let's approach the XOR benchmark dataset:

import torch
import torch_explain as te
from torch_explain import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

x, c, y = datasets.xor(500)
x_train, x_test, c_train, c_test, y_train, y_test = train_test_split(x, c, y, test_size=0.33, random_state=42)

We just need to define a task predictor and a concept encoder using a concept embedding layer:

import torch
import torch_explain as te

embedding_size = 8
concept_encoder = torch.nn.Sequential(
    torch.nn.Linear(x.shape[1], 10),
    torch.nn.LeakyReLU(),
    te.nn.ConceptEmbedding(10, c.shape[1], embedding_size),
)
task_predictor = torch.nn.Sequential(
    torch.nn.Linear(c.shape[1]*embedding_size, 1),
)
model = torch.nn.Sequential(concept_encoder, task_predictor)

We can now train the network by optimizing the cross entropy loss on concepts and tasks:

optimizer = torch.optim.AdamW(model.parameters(), lr=0.01)
loss_form_c = torch.nn.BCELoss()
loss_form_y = torch.nn.BCEWithLogitsLoss()
model.train()
for epoch in range(501):
    optimizer.zero_grad()

    # generate concept and task predictions
    c_emb, c_pred = concept_encoder(x_train)
    y_pred = task_predictor(c_emb.reshape(len(c_emb), -1))

    # compute loss
    concept_loss = loss_form_c(c_pred, c_train)
    task_loss = loss_form_y(y_pred, y_train)
    loss = concept_loss + 0.5*task_loss

    loss.backward()
    optimizer.step()

Once trained we can check the performance of the model on the test set:

c_emb, c_pred = concept_encoder.forward(x_test)
y_pred = task_predictor(c_emb.reshape(len(c_emb), -1))

task_accuracy = accuracy_score(y_test, y_pred > 0)
concept_accuracy = accuracy_score(c_test, c_pred > 0.5)

As you can see the performance of the model is now great as the task task accuracy is around ~100%.

Quick tutorial on Deep Concept Reasoning

Using deep concept reasoning we can solve the same problem as above, but with an intrinsically interpretable model! In fact, Deep Concept Reasoners (Deep CoRes) make task predictions by means of interpretable logic rules using concept embeddings.

Using the same example as before, we can just change the task predictor using a Deep CoRe layer:

from torch_explain.nn.concepts import ConceptReasoningLayer
import torch.nn.functional as F

y_train = F.one_hot(y_train.long().ravel()).float()
y_test = F.one_hot(y_test.long().ravel()).float()

task_predictor = ConceptReasoningLayer(embedding_size, y_train.shape[1])
model = torch.nn.Sequential(concept_encoder, task_predictor)

We can now train the network by optimizing the cross entropy loss on concepts and tasks:

optimizer = torch.optim.AdamW(model.parameters(), lr=0.01)
loss_form = torch.nn.BCELoss()
model.train()
for epoch in range(501):
    optimizer.zero_grad()

    # generate concept and task predictions
    c_emb, c_pred = concept_encoder(x_train)
    y_pred = task_predictor(c_emb, c_pred)

    # compute loss
    concept_loss = loss_form(c_pred, c_train)
    task_loss = loss_form(y_pred, y_train)
    loss = concept_loss + 0.5*task_loss

    loss.backward()
    optimizer.step()

Once trained the Deep CoRe layer can explain its predictions by providing both local and global logic rules:

local_explanations = task_predictor.explain(c_emb, c_pred, 'local')
global_explanations = task_predictor.explain(c_emb, c_pred, 'global')

For global explanations, the reasoner will return a dictionary with entries such as {'class': 'y_0', 'explanation': '~c_0 & ~c_1', 'count': 94}, specifying for each logic rule, the task it is associated with and the number of samples associated with the explanation.

Quick tutorial on Logic Explained Networks

For this simple experiment, let's solve the XOR problem (augmented with 100 dummy features):

import torch
import torch_explain as te
from torch.nn.functional import one_hot

x0 = torch.zeros((4, 100))
x_train = torch.tensor([
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1],
], dtype=torch.float)
x_train = torch.cat([x_train, x0], dim=1)
y_train = torch.tensor([0, 1, 1, 0], dtype=torch.long)
y_train_1h = one_hot(y_train).to(torch.float)

We can instantiate a simple feed-forward neural network with 3 layers using the EntropyLayer as the first one:

layers = [
    te.nn.EntropyLinear(x_train.shape[1], 10, n_classes=y_train_1h.shape[1]),
    torch.nn.LeakyReLU(),
    torch.nn.Linear(10, 4),
    torch.nn.LeakyReLU(),
    torch.nn.Linear(4, 1),
]
model = torch.nn.Sequential(*layers)

We can now train the network by optimizing the cross entropy loss and the entropy_logic_loss loss function incorporating the human prior towards simple explanations:

optimizer = torch.optim.AdamW(model.parameters(), lr=0.001)
loss_form = torch.nn.BCEWithLogitsLoss()
model.train()
for epoch in range(2001):
    optimizer.zero_grad()
    y_pred = model(x_train).squeeze(-1)
    loss = loss_form(y_pred, y_train_1h) + 0.0001 * te.nn.functional.entropy_logic_loss(model)
    loss.backward()
    optimizer.step()

Once trained we can extract first-order logic formulas describing how the network composed the input features to obtain the predictions:

from torch_explain.logic.nn import entropy
from torch.nn.functional import one_hot

y1h = one_hot(y_train)
global_explanations, local_explanations = entropy.explain_classes(model, x_train, y_train, c_threshold=0.5, y_threshold=0.)

Explanations will be logic formulas in disjunctive normal form. In this case, the explanation will be y=1 if and only if (f1 AND ~f2) OR (f2 AND ~f1) corresponding to f1 XOR f2.

The function automatically assesses the quality of logic explanations in terms of classification accuracy and rule complexity. In this case the accuracy is 100% and the complexity is 4.

Benchmark datasets

We provide a suite of 3 benchmark datasets to evaluate the performance of our models in the folder torch_explain/datasets. These 3 datasets were proposed as benchmarks for concept-based models in the paper "Concept Embedding Models: Beyond the Accuracy-Explainability Trade-Off".

Real-world datasets can be downloaded from the links provided in the supplementary material of the paper.

Theory

Theoretical foundations can be found in the following papers.

Deep Concept Reasoning (recently accepted at ICML-23):

@article{barbiero2023interpretable,
  title={Interpretable Neural-Symbolic Concept Reasoning},
  author={Barbiero, Pietro and Ciravegna, Gabriele and Giannini, Francesco and Zarlenga, Mateo Espinosa and Magister, Lucie Charlotte and Tonda, Alberto and Lio, Pietro and Precioso, Frederic and Jamnik, Mateja and Marra, Giuseppe},
  journal={arXiv preprint arXiv:2304.14068},
  year={2023}
}

Concept Embedding Models:

@article{espinosa2022concept,
  title={Concept Embedding Models: Beyond the Accuracy-Explainability Trade-Off},
  author={Espinosa Zarlenga, Mateo and Barbiero, Pietro and Ciravegna, Gabriele and Marra, Giuseppe and Giannini, Francesco and Diligenti, Michelangelo and Shams, Zohreh and Precioso, Frederic and Melacci, Stefano and Weller, Adrian and others},
  journal={Advances in Neural Information Processing Systems},
  volume={35},
  pages={21400--21413},
  year={2022}
}

Logic Explained Networks:

@article{ciravegna2023logic,
  title={Logic explained networks},
  author={Ciravegna, Gabriele and Barbiero, Pietro and Giannini, Francesco and Gori, Marco and Li{\'o}, Pietro and Maggini, Marco and Melacci, Stefano},
  journal={Artificial Intelligence},
  volume={314},
  pages={103822},
  year={2023},
  publisher={Elsevier}
}

Entropy-based LENs:

@inproceedings{barbiero2022entropy,
  title={Entropy-based logic explanations of neural networks},
  author={Barbiero, Pietro and Ciravegna, Gabriele and Giannini, Francesco and Li{\'o}, Pietro and Gori, Marco and Melacci, Stefano},
  booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
  volume={36},
  number={6},
  pages={6046--6054},
  year={2022}
}

Psi network ("learning of constraints"):

@inproceedings{ciravegna2020constraint,
  title={A constraint-based approach to learning and explanation},
  author={Ciravegna, Gabriele and Giannini, Francesco and Melacci, Stefano and Maggini, Marco and Gori, Marco},
  booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
  volume={34},
  number={04},
  pages={3658--3665},
  year={2020}
}

Learning with constraints:

@inproceedings{marra2019lyrics,
  title={LYRICS: A General Interface Layer to Integrate Logic Inference and Deep Learning},
  author={Marra, Giuseppe and Giannini, Francesco and Diligenti, Michelangelo and Gori, Marco},
  booktitle={Joint European Conference on Machine Learning and Knowledge Discovery in Databases},
  pages={283--298},
  year={2019},
  organization={Springer}
}

Constraints theory in machine learning:

@book{gori2017machine,
  title={Machine Learning: A constraint-based approach},
  author={Gori, Marco},
  year={2017},
  publisher={Morgan Kaufmann}
}

Authors

  • Pietro Barbiero, University of Cambridge, UK.
  • Mateo Espinosa Zarlenga, University of Cambridge, UK.
  • Giuseppe Marra, Katholieke Universiteit Leuven, BE.
  • Steve Azzolin, University of Trento, IT.
  • Francesco Giannini, University of Florence, IT.
  • Gabriele Ciravegna, University of Florence, IT.
  • Dobrik Georgiev, University of Cambridge, UK.

Licence

Copyright 2020 Pietro Barbiero, Mateo Espinosa Zarlenga, Giuseppe Marra, Steve Azzolin, Francesco Giannini, Gabriele Ciravegna, and Dobrik Georgiev.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

pytorch_explain's People

Contributors

pietrobarbiero avatar steveazzolin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch_explain's Issues

RuntimeError: The size of tensor a (571) must match the size of tensor b (2) at non-singleton dimension 0

Getting run-time error while trying to run the cross-validation with explainer network code on Colab.

Package versions used:
torch: 1.9.0+cu102
torch-explain: 0.5.6

Following is the error message:

RuntimeError Traceback (most recent call last)
in ()
57
58 start = time.time()
---> 59 trainer.fit(model, train_loader, val_loader)
60 model.freeze()
61 model_results = trainer.test(model, test_dataloaders=test_loader)

20 frames
/usr/local/lib/python3.7/dist-packages/torch_explain/nn/logic.py in forward(self, input)
46 self.alpha_norm = self.alpha / self.alpha.max(dim=1)[0].unsqueeze(1)
47 self.concept_mask = self.alpha_norm > 0.5
---> 48 x = input.multiply(self.alpha_norm.unsqueeze(1))
49
50 # compute linear map

RuntimeError: The size of tensor a (571) must match the size of tensor b (2) at non-singleton dimension 0

keras?

Hello,
any possibility to use keras instread pytorch? because keras is too much simpler and easer then pytorch

Can not runnable with error

the py script I run:

import torch
import torch_explain as te
from torch.nn.functional import one_hot

x0 = torch.zeros((4, 100))
x_train = torch.tensor([
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1],
], dtype=torch.float)
x_train = torch.cat([x_train, x0], dim=1)
y_train = torch.tensor([0, 1, 1, 0], dtype=torch.long)
y_train_1h = one_hot(y_train).to(torch.float)

layers = [
    te.nn.EntropyLinear(x_train.shape[1], 10, n_classes=y_train_1h.shape[1]),
    torch.nn.LeakyReLU(),
    torch.nn.Linear(10, 4),
    torch.nn.LeakyReLU(),
    torch.nn.Linear(4, 1),
]
model = torch.nn.Sequential(*layers)

optimizer = torch.optim.AdamW(model.parameters(), lr=0.001)
loss_form = torch.nn.BCEWithLogitsLoss()
model.train()
for epoch in range(2001):
    optimizer.zero_grad()
    y_pred = model(x_train).squeeze(-1)
    loss = loss_form(y_pred, y_train_1h) + 0.0001 * te.nn.functional.entropy_logic_loss(model)
    loss.backward()
    optimizer.step()

from torch_explain.logic.nn import entropy
from torch.nn.functional import one_hot

y1h = one_hot(y_train)
explanation, _ = entropy.explain_class(model, x_train, y1h, x_train, y1h, target_class=1)

from torch_explain.logic.metrics import test_explanation, complexity

accuracy, preds = test_explanation(explanation, x_train, y1h, target_class=1)
explanation_complexity = complexity(explanation)

Error:

Traceback (most recent call last):
  File "/home/cil/DataFolder/PycharmProjects/logic/logic_test.py", line 39, in <module>
    explanation, _ = entropy.explain_class(model, x_train, y1h, x_train, y1h, target_class=1)
  File "/home/cil/miniconda3/lib/python3.9/site-packages/torch_explain/logic/nn/entropy.py", line 111, in explain_class
    c_correct, y_correct, correct_mask, active_mask = _get_correct_data(c, y, train_mask, model, target_class,
  File "/home/cil/miniconda3/lib/python3.9/site-packages/torch_explain/logic/nn/entropy.py", line 436, in _get_correct_data
    active_mask = y[train_mask, target_class] == 1
IndexError: tensors used as indices must be long, byte or bool tensors

Environment:

Python                  3.9.7
pytorch                   1.10.2          py3.9_cuda10.2_cudnn7.6.5_0
torch-explain             1.3.0

How to use the method on a custom dataset?

Hi. thanks for such an interesting work.
I was wondering if your method can be applied to a custom image dataset.
I've seen that in the paper "Concept Embedding Models: Beyond the Accuracy-Explainability Trade-Off" you also did experiments on CUB dataset but I could not find the associated code.

Can you please provide the code for the CUB dataset or give some instructions on how to prepare a custom dataset of images in order to apply your method?

Thank you.
Best wishes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.