Skip to main content

Python-based implementation of PSyKI, i.e. a Platform for Symbolic Knowledge Injection

Project description

PSyKI

Platform for Symbolic Knowledge Injection

PyPI version Test Codecov Release Black License Stand With Ukraine

Some quick links:

Reference 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:

@inproceedings{PsykiExtraamas2022,
	keywords = {Symbolic Knowledge Injection,  Explainable AI, XAI, Neural Networks, PSyKI},
	year = 2022,
	talk = {Talks.PsykiExtraamas2022},
	author = {Magnini, Matteo and Ciatto, Giovanni and Omicini, Andrea},
	venue_e = {Events.Extraamas2022},
	sort = {inproceedings},
	publisher = {Springer},
	status = {In press},
	title = {On the Design of PSyKI: a Platform for Symbolic Knowledge Injection into Sub-Symbolic Predictors},
	booktitle = {Proceedings of the 4th International Workshop on EXplainable and TRAnsparent AI and Multi-Agent Systems}
}

Intro

PSyKI (Platform for Symbolic Knowledge Injection) is a python library for symbolic knowledge injection (SKI). In the literature, SKI may also be referred as neuro-symbolic integration. PSyKI offers SKI algorithms (injectors) along with quality of service metrics (QoS) and other utility functionalities. Finally, the library is open to extendability.

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.

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.7.0
  • 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.3.11.tar.gz (44.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psyki-0.3.11-py3-none-any.whl (40.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: psyki-0.3.11.tar.gz
  • Upload date:
  • Size: 44.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for psyki-0.3.11.tar.gz
Algorithm Hash digest
SHA256 55c2693c4833c813a39f942c1e10d907245ff7852301911c9df7d5b3257d5337
MD5 9fdfa7311e4ca05ce4b6a594c3668cb7
BLAKE2b-256 4bdeff374822d21a51d7a6489ca21296e01235c9a7a1c810c634280497b3cd37

See more details on using hashes here.

File details

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

File metadata

  • Download URL: psyki-0.3.11-py3-none-any.whl
  • Upload date:
  • Size: 40.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for psyki-0.3.11-py3-none-any.whl
Algorithm Hash digest
SHA256 09bb2293b89cb5d38c430b8d1456be0525aeff84abade248ae55d77fb3d5ecf8
MD5 f082512ec12808d94864c6a8bc757150
BLAKE2b-256 70fdae5858b5f98f683cb6783da717bbb6c9e194d907c534db97c125b7672a1f

See more details on using hashes here.

Supported by

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