Skip to main content

PSyKI: a (Python) platform for symbolic knowledge injection

Project description

PSyKI

Platform for Symbolic Knowledge Injection

PyPI version Test Codecov Release Black License Stand With Ukraine

PSyKI (Platform for Symbolic Knowledge Injection) is a python library for symbolic knowledge injection (SKI). SKI is a particular subclass of neuro-symbolic (NeSy) integration techniques. PSyKI offers SKI algorithms (a.k.a. injectors) along with quality of service metrics (QoS).

Some quick links:

✨ Update [Nov. 2024]: from version 0.4.1, PSyKI supports FaUCI, a novel fairness method to reduce bias in machine learning models.

Reference papers

If you use this library in your research you must cite the PSyKI paper. Additionally, if you use one of the original injectors, fairness methods or QoS metrics, you must cite the respective paper.

PSyKI (main paper)

Matteo Magnini, Giovanni Ciatto, Andrea Omicini. "On the Design of PSyKI: A Platform for Symbolic Knowledge Injection into Sub-Symbolic Predictors", in: Proceedings of the 4th International Workshop on EXplainable and TRAnsparent AI and Multi-Agent Systems, 2022.

Bibtex:

@incollection{psyki-extraamas2022,
    author = {Magnini, Matteo and Ciatto, Giovanni and Omicini, Andrea},
    booktitle = {Explainable and Transparent AI and Multi-Agent Systems},
    chapter = 6,
    dblp = {conf/atal/MagniniCO22},
    doi = {10.1007/978-3-031-15565-9_6},
    editor = {Calvaresi, Davide and Najjar, Amro and Winikoff, Michael and Främling, Kary},
    eisbn = {978-3-031-15565-9},
    eissn = {1611-3349},
    iris = {11585/899511},
    isbn = {978-3-031-15564-2},
    issn = {0302-9743},
    keywords = {Symbolic Knowledge Injection, Explainable AI, XAI, Neural Networks, PSyKI},
    note = {4th International Workshop, EXTRAAMAS 2022, Virtual Event, May 9--10, 2022, Revised Selected Papers},
    pages = {90--108},
    publisher = {Springer},
    scholar = {7587528289517313138},
    scopus = {2-s2.0-85138317005},
    series = {Lecture Notes in Computer Science},
    title = {On the Design of {PSyKI}: a Platform for Symbolic Knowledge Injection into Sub-Symbolic Predictors},
    url = {https://link.springer.com/chapter/10.1007/978-3-031-15565-9_6},
    urlpdf = {https://link.springer.com/content/pdf/10.1007/978-3-031-15565-9_6.pdf},
    volume = 13283,
    wos = {000870042100006},
    year = 2022
}

FaUCI fairness method

Matteo Magnini, Giovanni Ciatto, Roberta Calegari, and Andrea Omicini, “Enforcing fairness via constraint injection with fauci,” in Proceedings of the 2nd Workshop on Fairness and Bias in AI co-located with 27th European Conference on Artificial Intelligence (ECAI 2024), Santiago de Compostela, Spain, October 20th, 2024, R. Calegari, V. Dignum, and B. O’Sullivan, Eds., ser. CEUR Workshop Proceedings, vol. 3808, CEUR-WS.org, 2024.

Bibtex:

@inproceedings{DBLP:conf/aequitas/MagniniCCO24,
  author       = {Matteo Magnini and
                  Giovanni Ciatto and
                  Roberta Calegari and
                  Andrea Omicini},
  editor       = {Roberta Calegari and
                  Virginia Dignum and
                  Barry O'Sullivan},
  title        = {Enforcing Fairness via Constraint Injection with FaUCI},
  booktitle    = {Proceedings of the 2nd Workshop on Fairness and Bias in {AI} co-located
                  with 27th European Conference on Artificial Intelligence {(ECAI} 2024),
                  Santiago de Compostela, Spain, October 20th, 2024},
  series       = {{CEUR} Workshop Proceedings},
  volume       = {3808},
  publisher    = {CEUR-WS.org},
  year         = {2024},
  url          = {https://ceur-ws.org/Vol-3808/paper8.pdf},
  timestamp    = {Fri, 08 Nov 2024 15:21:04 +0100},
  biburl       = {https://dblp.org/rec/conf/aequitas/MagniniCCO24.bib},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}

KINS injector

Matteo Magnini, Giovanni Ciatto, and Andrea Omicini “Knowledge injection of datalog rules via neural network structuring with KINS,” J. Log. Comput., vol. 33, no. 8, pp. 1832–1850, 2023. doi: 10.1093/LOGCOM/ EXAD037.

Bibtex:

@article{DBLP:journals/logcom/MagniniCO23,
  author       = {Matteo Magnini and
                  Giovanni Ciatto and
                  Andrea Omicini},
  title        = {Knowledge injection of Datalog rules via Neural Network Structuring
                  with {KINS}},
  journal      = {J. Log. Comput.},
  volume       = {33},
  number       = {8},
  pages        = {1832--1850},
  year         = {2023},
  url          = {https://doi.org/10.1093/logcom/exad037},
  doi          = {10.1093/LOGCOM/EXAD037},
  timestamp    = {Tue, 02 Jan 2024 12:25:17 +0100},
  biburl       = {https://dblp.org/rec/journals/logcom/MagniniCO23.bib},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}

KILL injector

Matteo Magnini, Giovanni Ciatto, and Andrea Omicini, “A view to a KILL: knowledge injection via lambda layer,” in Proceedings of the 23rd Workshop "From Objects to Agents", Genova, Italy, September 1-3, 2022, A. Ferrando and V. Mascardi, Eds., ser. CEUR Workshop Proceedings, vol. 3261, CEUR-WS.org, 2022, pp. 61–76.

Bibtex:

@inproceedings{DBLP:conf/woa/MagniniCO22,
  author       = {Matteo Magnini and
                  Giovanni Ciatto and
                  Andrea Omicini},
  editor       = {Angelo Ferrando and
                  Viviana Mascardi},
  title        = {A view to a {KILL:} knowledge injection via lambda layer},
  booktitle    = {Proceedings of the 23rd Workshop "From Objects to Agents", Genova,
                  Italy, September 1-3, 2022},
  series       = {{CEUR} Workshop Proceedings},
  volume       = {3261},
  pages        = {61--76},
  publisher    = {CEUR-WS.org},
  year         = {2022},
  url          = {https://ceur-ws.org/Vol-3261/paper5.pdf},
  timestamp    = {Fri, 10 Mar 2023 16:22:40 +0100},
  biburl       = {https://dblp.org/rec/conf/woa/MagniniCO22.bib},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}

QoS metrics

Andrea Agiollo, Andrea Rafanelli, Matteo Magnini, Giovanni Ciatto, Andrea Omicini. "Symbolic knowledge injection meets intelligent agents: QoS metrics and experiments", in: Autonomous Agents and Multi-Agent Systems, 2023.

Bibtex:

@article{ski-qos23,
  author       = {Andrea Agiollo and
                  Andrea Rafanelli and
                  Matteo Magnini and
                  Giovanni Ciatto and
                  Andrea Omicini},
  title        = {Symbolic knowledge injection meets intelligent agents: QoS metrics
                  and experiments},
  journal      = {Auton. Agents Multi Agent Syst.},
  volume       = {37},
  number       = {2},
  pages        = {27},
  year         = {2023},
  url          = {https://doi.org/10.1007/s10458-023-09609-6},
  doi          = {10.1007/S10458-023-09609-6},
  timestamp    = {Tue, 12 Sep 2023 07:57:44 +0200},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}

Robustness metrics for symbolic knowledge injection

Andrea Rafanelli, Matteo Magnini, Andrea Agiollo, Giovanni Ciatto, and Andrea Omicini, “An empirical study on the robustness of knowledge injection techniques against data degradation,” in Proceedings of the 25th Workshop "From Objects to Agents", Bard (Aosta), Italy, July 8-10, 2024, M. Alderighi, M. Baldoni, C. Baroglio, R. Micalizio, and S. Tedeschi, Eds., ser. CEURWorkshop Proceedings, vol. 3735, CEUR-WS.org, 2024, pp. 20–32.

Bibtex:

@inproceedings{DBLP:conf/woa/RafanelliMACO24,
  author       = {Andrea Rafanelli and
                  Matteo Magnini and
                  Andrea Agiollo and
                  Giovanni Ciatto and
                  Andrea Omicini},
  editor       = {Marco Alderighi and
                  Matteo Baldoni and
                  Cristina Baroglio and
                  Roberto Micalizio and
                  Stefano Tedeschi},
  title        = {An Empirical Study on the Robustness of Knowledge Injection Techniques
                  Against Data Degradation},
  booktitle    = {Proceedings of the 25th Workshop "From Objects to Agents", Bard (Aosta),
                  Italy, July 8-10, 2024},
  series       = {{CEUR} Workshop Proceedings},
  volume       = {3735},
  pages        = {20--32},
  publisher    = {CEUR-WS.org},
  year         = {2024},
  url          = {https://ceur-ws.org/Vol-3735/paper\_02.pdf},
  timestamp    = {Fri, 26 Jul 2024 22:35:03 +0200},
  biburl       = {https://dblp.org/rec/conf/woa/RafanelliMACO24.bib},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}

More in detail

An Injector is a SKI algorithm that may -- or may not -- take a sub-symbolic predictor in conjunction with prior symbolic knowledge to create a new predictor through the inject method. We refer to the new predictor as educated, while predictors that are not affected by symbolic knowledge are called uneducated.

Knowledge can be represented in many ways. The most common is the representation via logic formulae. PSyKI integrates 2ppy, a python porting of 2p-kt (a multi-paradigm logic programming framework). Thanks to this integration, PSyKI supports logic formulae written with the formalism of Prolog. Therefore, all subsets of the Prolog language (including Prolog itself) are potentially supported (e.g., propositional logic, Datalog, etc.). It is worth noting that each injector may have its own requirements on the knowledge representation.

List of available injectors:

  • KBANN, one of the first injector introduced in literature;
  • KILL, constrains a NN by altering its predictions using the knowledge;
  • KINS, structure the knowledge adding ad-hoc layers into a NN.

High level architecture

PSyKI class diagram Class diagram representing the relations between Injector, Theory and Fuzzifier classes

The core abstractions of PSyKI are the following:

  • Injector: a SKI algorithm;
  • Theory: symbolic knowledge plus additional information about the domain;
  • Fuzzifier: entity that transforms (fuzzify) symbolic knowledge into a sub-symbolic data structure.

The class Theory is built upon the symbolic knowledge and the metadata of the dataset (extracted by a Pandas DataFrame). The knowledge can be generated by an adapter that parses the Prolog theory (e.g., a .pl file, a string) and generates a list of Formula objects. Each Injector has one Fuzzifier. The Fuzzifier is used to transform the Theory into a sub-symbolic data structure (e.g., ad-hoc layers of a NN). Different fuzzifiers encode the knowledge in different ways.

To avoid confusion, we use the following terminology:

  • rule is a single logic clause;
  • knowledge is the set of rules;
  • theory is the knowledge plus metadata.

Hello world

The following example shows how to use PSyKI to inject knowledge into a NN.

import pandas as pd
from tensorflow.keras.models import Model
from psyki.logic import Theory
from psyki.ski import Injector


def create_uneducated_predictor() -> Model:
...

dataset = pd.read_csv("path_to_dataset.csv")  # load dataset
knowledge_file = "path_to_knowledge_file.pl"  # load knowledge
theory = Theory(knowledge_file, dataset)      # create a theory
uneducated = create_uneducated_predictor()    # create a NN
injector = Injector.kins(uneducated)          # create an injector
educated = injector.inject(theory)            # inject knowledge into the NN

# From now on you can use the educated predictor as you would use the uneducated one

For more detailed examples, please refer to the demos in the demo-psyki-python repository.


Injection Assessment

Knowledge injection methods aim to enhance the sustainability of machine learning by minimizing the learning time required. This accelerated learning can possibly lead to improvements in model accuracy as well. Additionally, by incorporating prior knowledge, the data requirements for effective training may be loosened, allowing for smaller datasets to be utilized. Injecting knowledge has also the potential to increase model interpretability by preventing the predictor from becoming a black-box.

Most existing works on knowledge injection techniques showcase standard evaluation metrics that aim to quantify these potential benefits. However, accurately quantifying sustainability, accuracy improvements, dataset needs, and interpretability in a consistent manner remains an open challenge.

💾 Memory Footprint

Intuition: injected knowledge can reduce the amount of notions to be learnt data-drivenly.

Idea: measure the amount of total operations (FLOPs or MACs) required by the model.

🗂️ Data Efficiency

Intuition: several concepts are injected → some portions of training data are not required.

Idea: reducing the size of the training set $D$ for the educated predictor by letting the input knowledge $K$ compensate for such lack of data.

⏳ Latency

Intuition: knowledge injection remove unnecessary computations required to draw a prediction.

Idea: measures the average time required to draw a single prediction from a dataset $T$.

⚡ Energy Consumption

Intuition: knowledge injection reduces learning complexity → reduces amount of computations for training and running a model.

Idea: i) measures the average energy consumption (mW) for a single update, on a training dataset $T$; ii) measures the average energy consumption (mW) of a single forward on a test dataset $T$ composed by several samples.

Example

The following example shows how to use QoS metrics.

from psyki.qos.energy import Energy

# Model training
model1 = ... # create model 1
model2 = ... # create model 2

X_train, y_train = ... # get training data

model1.fit(X_train, y_train) 
model2.fit(X_train, y_train)

# Compute energy
params = {'x': X_train, 'y': y_train, 'epochs': 100}
training_energy = Energy.compute_during_training(model1, model2, params)


# Model inference
X_test, y_test = ... # get test data 

model1_preds = model1.predict(X_test)
model2_preds = model2.predict(X_test)

# Compute energy
params = {'x': X_test}  
inference_energy = Energy.compute_during_inference(model1, model2, params)

# The process for the other metrics (data efficiency, latency, and memory) is the same.

Users

PSyKI is deployed as a library on Pypi, and it can therefore be installed as Python package by running:

pip install psyki

Requirements

  • python 3.9+
  • java 11
  • 2ppy 0.4.0
  • tensorflow 2.13.1
  • numpy 1.22.3
  • scikit-learn 1.0.2
  • pandas 1.4.2
  • codecarbon 2.1.4

Developers

Working with PSyKI codebase requires a number of tools to be installed:

  • Python 3.9+
  • JDK 11+ (please ensure the JAVA_HOME environment variable is properly configured)
  • Git 2.20+

Develop PSyKI with PyCharm

To participate in the development of PSyKI, we suggest the PyCharm IDE.

Importing the project

  1. Clone this repository in a folder of your preference using git_clone appropriately
  2. Open PyCharm
  3. Select Open
  4. Navigate your file system and find the folder where you cloned the repository
  5. Click Open

Developing the project

Contributions to this project are welcome. Just some rules:

  • We use git flow, so if you write new features, please do so in a separate feature/ branch
  • We recommend forking the project, developing your stuff, then contributing back vie pull request
  • Commit often
  • Stay in sync with the develop (or main | master) branch (pull frequently if the build passes)
  • Do not introduce low quality or untested code

Issue tracking

If you meet some problem in using or developing PSyKI, you are encouraged to signal it through the project "Issues" section on GitHub.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psyki-0.4.1.tar.gz (43.1 kB view details)

Uploaded Source

Built Distribution

psyki-0.4.1-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file psyki-0.4.1.tar.gz.

File metadata

  • Download URL: psyki-0.4.1.tar.gz
  • Upload date:
  • Size: 43.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for psyki-0.4.1.tar.gz
Algorithm Hash digest
SHA256 778f890cf29285f7e3cfa92e1156a4d40b8b5dc29e0268436fa4e26acee5fa6a
MD5 765f8c5140253b09a7a60ed314c8b069
BLAKE2b-256 1c0ec2884e36091b5dc64ee538e05d6f841b0e481fdf63213d5ada263dc9b0d8

See more details on using hashes here.

File details

Details for the file psyki-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: psyki-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 48.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for psyki-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60bfb5a0a982afabd79ab96e711045ddfad49c7a8534605d992935f051d812b7
MD5 723eb6fbcb8a2fbcab8a713c664694d3
BLAKE2b-256 b77fa9484e1198ea5eb78a982436009972f7af477ef8e8c11d47a1fb1b78f56c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page