GithubHelp home page GithubHelp logo

ibm / lnn Goto Github PK

View Code? Open in Web Editor NEW
217.0 15.0 430.0 9.97 MB

A `Neural = Symbolic` framework for sound and complete weighted real-value logic

Home Page: https://IBM.github.io/LNN/

License: Apache License 2.0

Shell 0.05% Python 88.75% Jupyter Notebook 11.04% Makefile 0.17%
neuro-symbolic-ai machine-learning reasoning logic

lnn's Introduction

Build Status PRs Welcome License CII Best Practices Code style: black

Logical Neural Networks

LNNs are a novel Neuro = Symbolic framework designed to seamlessly provide key properties of both neural nets (learning) and symbolic logic (knowledge and reasoning).

  • Every neuron has a meaning as a component of a formula in a weighted real-valued logic, yielding a highly interpretable disentangled representation.
  • Inference is omnidirectional rather than focused on predefined target variables, and corresponds to logical reasoning, including classical first-order logic theorem proving as a special case.
  • The model is end-to-end differentiable, and learning minimizes a novel loss function capturing logical contradiction, yielding resilience to inconsistent knowledge.
  • It also enables the open-world assumption by maintaining bounds on truth values which can have probabilistic semantics, yielding resilience to incomplete knowledge.

Quickstart

To install the LNN:

  1. Make sure that the python version you use in line with our setup file, using a fresh environment is always a good idea:
    conda create -n lnn python=3.9 -y
    conda activate lnn
    
  2. Install the master branch to keep up to date with the latest supported features:
    pip install git+https://github.com/IBM/LNN
    

Contribution

Contributions to the LNN codebase are welcome!

Please have a look at the contribution guide for more information on how to set up the LNN for contributing and how to follow our development standards.

Documentation

Read the Docs Academic Papers Educational Resources Neuro-Symbolic AI API Overview Python Module
Docs Academic Papers Getting Started Neuro-Symbolic AI API Python Module

Citation

If you use Logical Neural Networks for research, please consider citing the reference paper:

@article{riegel2020logical,
  title={Logical neural networks},
  author={Riegel, Ryan and Gray, Alexander and Luus, Francois and Khan, Naweed and Makondo, Ndivhuwo and Akhalwaya, Ismail Yunus and Qian, Haifeng and Fagin, Ronald and Barahona, Francisco and Sharma, Udit and others},
  journal={arXiv preprint arXiv:2006.13155},
  year={2020}
}

lnn's People

Contributors

ibm-open-source-bot avatar imgbotapp avatar kyleerwin avatar michalkordyzon avatar mikulatomas avatar momolefe24 avatar mwbaert avatar namin avatar naweedaghmad avatar ndiv 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  avatar  avatar  avatar

lnn's Issues

LNN fails installing pygraphviz on windows

If you run in this error running the command pip install git+https://github.com/IBM/LNN.git on Windows

  Building wheel for pygraphviz (setup.py) ... error
  ERROR: Command errored out with exit status 1:
  ....

install pygraphviz with the command below and then install LNN

pip install --global-option=build_ext --global-option="-IC:\Program Files\Graphviz\include" --global-option="-LC:\Program Files\Graphviz\lib" pygraphviz

ERROR: Failed building wheel for pycddlib

While pip installing the LNN getting the error as below. using fresh docker anaconda environment for the installation.

This has been the case when installing on a normal mode as well. please fix the build file.

and all the documentation link were broken.

can't install lnn-contrib

after submitting:
pip install -e git+https://github.com/IBM/LNN.git
I get:
ERROR: Could not detect requirement name for 'git+https://github.com/IBM/LNN.git', please specify one with #egg=your_package_name
my environment:

Monterey 12.6
Python 3.9.13
setuptools 63.4.1

Variable Arguments

Referring to https://ibm.github.io/LNN/usage.html#using-the-lnn

from lnn import Variable

x = Variable('x', dtype='person')
y = Variable('y', dtype='person')

it seems that the Variable object argument "dtype" is actually "ctype"

i.e

>>> Variable('x',ctype='person')
<lnn.symbolic.logic.Variable object at 0x7f6e76c43b80>

Error running Smoking Example

From the Usage page I have abstracted the following code

from lnn import Predicates
from lnn import Variables
from lnn import Implies, Equivalent
from lnn import World
from lnn import Fact

Smokes, Cancer = Predicates('Smokes', 'Cancer')
Friends = Predicates('Friends', arity=2)

x, y = Variables('x', 'y')

Smoking_causes_Cancer = Implies(Smokes(x), Cancer(x))
Smokers_befriend_Smokers = Implies(Friends(x, y), Equivalent(Smokes(x), Smokes(y)))

formulae = [
    Smoking_causes_Cancer,
    Smokers_befriend_Smokers
]
model.add_knowledge(*formulae, world=World.AXIOM)

# add data to the model
model.add_data({
    Friends: {
        ('Anna', 'Bob'): Fact.TRUE,
        ('Bob', 'Anna'): Fact.TRUE,
        ('Anna', 'Edward'): Fact.TRUE,
        ('Edward', 'Anna'): Fact.TRUE,
        ('Anna', 'Frank'): Fact.TRUE,
        ('Frank', 'Anna'): Fact.TRUE,
        ('Bob', 'Chris'): Fact.TRUE},
    Smokes.name: {
        'Anna': Fact.TRUE,
        'Edward': Fact.TRUE,
        'Frank': Fact.TRUE,
        'Gary': Fact.TRUE},
    Cancer.name: {
        'Anna': Fact.TRUE,
        'Edward': Fact.TRUE}
    })
model.print()

I added a comma in the formulae. When I run this, I get

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [17], line 27
     23 model.add_knowledge(*formulae, world=World.AXIOM)
     26 # add data to the model
---> 27 model.add_data({
     28     Friends: {
     29         ('Anna', 'Bob'): Fact.TRUE,
     30         ('Bob', 'Anna'): Fact.TRUE,
     31         ('Anna', 'Edward'): Fact.TRUE,
     32         ('Edward', 'Anna'): Fact.TRUE,
     33         ('Anna', 'Frank'): Fact.TRUE,
     34         ('Frank', 'Anna'): Fact.TRUE,
     35         ('Bob', 'Chris'): Fact.TRUE},
     36     Smokes.name: {
     37         'Anna': Fact.TRUE,
     38         'Edward': Fact.TRUE,
     39         'Frank': Fact.TRUE,
     40         'Gary': Fact.TRUE},
     41     Cancer.name: {
     42         'Anna': Fact.TRUE,
     43         'Edward': Fact.TRUE}
     44     })
     45 model.print()

File ~/src/pas-notebooks/env/lib64/python3.9/site-packages/lnn/model.py:346, in Model.add_data(self, data)
    344 for formula, fact in data.items():
    345     if not isinstance(formula, Formula):
--> 346         raise TypeError(
    347             "formula expected of type Formula, received "
    348             f"{formula.__class__.__name__}"
    349         )
    350     _exceptions.AssertFormulaInModel(self, formula)
    351     if formula.propositional:

TypeError: formula expected of type Formula, received str

Cannot import Join from lnn.

It's related to:

https://ibm.github.io/LNN/education/examples/reasoning.html#more-complex-reasoning-example

the code is:

from lnn import (Predicate, Variable, Join, And,
                 Exists, Implies, Forall, Model, Fact, World)


model = Model()  # Instantiate a model.
x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])

# Define and add predicates to the model.
owns = model['owns'] = Predicate('owns', 2)  # binary predicate
missile = model['missile'] = Predicate('missile')
american = model['american'] = Predicate('american')
enemy = model['enemy'] = Predicate('enemy', 2)
hostile = model['hostile'] = Predicate('hostile')
criminal = model['criminal'] = Predicate('criminal')
weapon = model['weapon'] = Predicate('weapon')
sells = model['sells'] = Predicate('sells', 3)  # ternary predicate


# Define and add the background knowledge to  the model.
america_enemies = (
    Forall(x, Implies(enemy(x, (y, 'America')),
                      hostile(x),
                      ),
           world=World.AXIOM)
    )
model.add_knowledge(america_enemies)

# Define queries
query = Exists(x, criminal(x))
model.add_knowledge(query)

# Add facts to model.
model.set_facts({
    owns: {('Nono', 'M1'): Fact.TRUE},
    missile: {'M1': Fact.TRUE},
    american: {'West': Fact.TRUE},
    enemy: {('Nono', 'America'): Fact.TRUE},
})

model.infer()
print(model[query].true_groundings)

and it says

Traceback (most recent call last):
  File "/home/sungjin/home/lnn-examples/complex2.py", line 1, in <module>
    from lnn import (Predicate, Variable, Join, And,
ImportError: cannot import name 'Join' from 'lnn' (/home/sungjin/.virtualenvs/lnn/lib/python3.10/site-packages/lnn/__init__.py)

Errors on usage.

Hi. I'm a graduate student from korea, majoring computer science.

I think LNN is a great project what I'm looking for. I really appreciate it. Thanks.

However, I found some errors in the usage page.

https://ibm.github.io/LNN/education/examples/reasoning.html#more-complex-reasoning-example

from lnn import (Predicate, Variable, Join, And,
                 Exists, Implies, ForAll, Model, Fact, World)

model = Model()  # Instantiate a model.
x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])

# Define and add predicates to the model.
owns = model['owns'] = Predicate('owns', 2)  # binary predicate
missile = model['missile'] = Predicate('missile')
american = model['american'] = Predicate('american')
enemy = model['enemy'] = Predicate('enemy', 2)
hostile = model['hostile'] = Predicate('hostile')
criminal = model['criminal'] = Predicate('criminal')
weapon = model['weapon'] = Predicate('weapon')
sells = model['sells'] = Predicate('sells', 3)  # ternary predicate

which tells that

      5 x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])
      7 # Define and add predicates to the model.
----> 8 owns = model['owns'] = Predicate('owns', 2)  # binary predicate
      9 missile = model['missile'] = Predicate('missile')
     10 american = model['american'] = Predicate('american')

TypeError: 'Model' object does not support item assignment

I'm new to LNN, and not yet fully understand it. Can you please tell me how to fix this error? Thank you very much.

Model equivalence

Allow two models to be compared to one another using __eq__ dunder.
This compares all the formulae, their connectivity and corresponding facts at convergence (i.e. does not consider any gradient computations)

Downward inference from fully quantified universal

@KyleErwin the code below seems to have two issues

  1. Setting the truth of the universal quantifier doesn't directly update the proposition
  2. Downward inference from the universal seems to break
from lnn import *
x = Variable('x')
cry = Predicate('Cry')
sad = Predicate('Sad')
rule = Forall(x, Implies(cry(x), sad(x)))

model = Model()
model.add_knowledge(rule, cry)
model.add_data({
    cry: {
        'John': Fact.TRUE,
        'Bob': Fact.TRUE,
    },
    rule: Fact.TRUE,
})

model.print()
model.infer()
model.print()
File "lnn/symbolic/logic/unary_operator.py", line 165, in downward
  return self._fully_quantified_downward()
File "lnn/symbolic/logic/unary_operator.py", line 270, in _fully_quantified_downward
  return operand.neuron.aggregate_bounds(groundings, bounds[..., 0])
IndexError: index 0 is out of bounds for dimension 1 with size 0

Throw exception for predicates in compound formulae w/o variables

Compound formulae expect predicates to be called (call)

e.g.
given:

P = Predicate('P')
Q = Predicate('Q', arity=2)

correct

And(P(x), Q(x, y))

incorrect

And(P, Q)

not having called compound formulae is only the syntax for propositional inputs
e.g.

P = Proposition('P')
Q = Proposition('Q')
And(P, Q)

right now it seems to fail silently by reverting to the default configuration

Issue with training model error: list indices must be integers or slices, not Loss.

I try to train a model using :

model.train(losses=Loss.SUPERVISED)

Error on:

model.train(losses=Loss.SUPERVISED)

Error message:

list indices must be integers or slices, not Loss.

My code:

from lnn import And, Loss, Implies, Or, Equivalent, Fact, Predicates, Model, Variables, World
model = Model()

AgeGreat60 = Predicates('AgeGreat60')
IncomeLess400 = Predicates('IncomeLessthan400')
Risk = Predicates('Risk', arity=2)

# Variables
x = Variables('x')
y = Variables('y')
z = Variables('z')

# Risk(x,y) <---- AgeGreat60(x) AND IncomeLess400(y)

Root = Implies(Risk(x,y), And(AgeGreat60(x), IncomeLess400(y)))

formulae = [
Root
]
model.add_knowledge(*formulae, world=World.OPEN)
[#model](https://www.linkedin.com/feed/hashtag/?keywords=%23model).add_knowledge(Root)


# Data
model.add_data({
AgeGreat60: {
('70'): Fact.TRUE,
('30'): Fact.FALSE,
('20'): Fact.FALSE,
},
IncomeLess400: {
('300'): Fact.TRUE,
('600'): Fact.FALSE,
},
Risk: {
('70', '300'): Fact.TRUE,
('30', '600'): Fact.FALSE,
('60', '500'): (0.6, 0.2),
('65', '450'): (0.8, 1)
}
})


model.add_labels({
Risk: {
('70', '300'): Fact.TRUE,
('30', '600'): Fact.FALSE,
}
})

# train the model and output results
model.train(losses=Loss.SUPERVISED)
model.print(params=True)

Contributor communication channel

Is there a public-facing communication channel (e.g., Slack or Discord) for current and/or potential contributors/researchers? If not, I am happy to set up one! :)

Cannot import name 'ForAll' from 'lnn'

Hi, I'm currently studying the LNN, and trying to evaluate the example code.

However, I've got this error from the example code:

  1. example code:

https://ibm.github.io/LNN/education/examples/reasoning.html#simple-geometry-reasoning-example

  1. where I stuck:
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.11.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from lnn import (Predicate, Variable,
   ...:                  Exists, Implies, ForAll, Model, Fact, World)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 from lnn import (Predicate, Variable,
      2                  Exists, Implies, ForAll, Model, Fact, World)

ImportError: cannot import name 'ForAll' from 'lnn' (/home/sungjin/.virtualenvs/lnn/lib/python3.10/site-packages/lnn/__init__.py)

I know that LNN is a great tool which will leverage the next step of AI, and I glad to know about it. And I hope there will be a chance to the newcomers like me. Thank you.

Feature Request: Add the other jupyter notebook tutorials in the tutorials folder.

Hello,
I am interested in doing a final year project for my bachelor's degree in this topic, and I need more guidance and tutorials.

Within the tutorials/README.md of this repo, an image of 9 jupyter notebook tutorials is shown, but the repo only has 3.

What happened to the other .ipynb files?
Furthermore is there a tutorial on applying LNN's to knowledge graphs?

Thank you.

Clarification on bounds

From the tutorial, it seems like a CONTRADICTION is when L > U: however, the results bellow happens quite often when L > U is considered APPROX TRUE.

Can someone help to clarify this?

Model before inference:


                            LNN Model

OPEN And: (A ∧ B) APPROX_TRUE (0.51, 0.52)

OPEN Proposition: B APPROX_TRUE (0.9, 0.99)

OPEN Proposition: A APPROX_TRUE (0.9, 0.99)


Model after inference:


                            LNN Model

OPEN And: (A ∧ B) APPROX_TRUE (0.8, 0.52)

OPEN Proposition: B APPROX_TRUE (0.9, 0.99)

OPEN Proposition: A APPROX_TRUE (0.9, 0.99)

Impossible to run tutorials in Colab Jupiter notebooks

Hello, I was unsuccessfully trying to install lnn lib in Google Colab with and without conda use. The issue with conda approach consists in non-activation of the newly created lnn-env even within the same cell, and the lnn package is not visible to python.
The pip3 approach wouldn't work, as the default version of pathlib-1.0.1does not contain the attribute read_text for PosixPath.
Code to reproduce conda approach:

# Get miniconda and install Python 3.9
!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python3 -m ipykernel install --name "py39"

# Install graphviz
!sudo apt-get install python3-dev graphviz libgraphviz-dev pkg-config
!sudo -H apt-get install libgmp-dev python3-dev

# Create virtual environment and activate it - all in one cell, but the activation doesn't work
!conda create -n lnn-env python=3.9 -y
!source activate lnn-env
!conda activate lnn-env
!python --version
!conda info --envs
!pip install git+https://github.com/IBM/LNN.git

from lnn import Proposition
Messi = Proposition("Messi")
AFC = Proposition("Argentina National Football Club(AFC)")

which shows that conda init didn't work and the newly created environment doesn't have the asterix of activation:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


Python 3.9.1
# conda environments:
#
base                  *  /usr/local
lnn-env                  /usr/local/envs/lnn-env

finally, the lnn library does not exist to python compiler:

ModuleNotFoundError                       Traceback (most recent call last)
[<ipython-input-1-d11eb96f34b4>](https://localhost:8080/#) in <module>()
      7 get_ipython().system('pip install git+https://github.com/IBM/LNN.git')
      8 
----> 9 from lnn import Proposition
     10 Messi = Proposition("Messi")
     11 AFC = Proposition("Argentina National Football Club(AFC)")

ModuleNotFoundError: No module named 'lnn'

The code for pip3 approach:

# Install python 3.9
!sudo apt-get update -y
!sudo apt-get install python3.9

# Change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

# Install dependencies
!sudo apt-get install libgmp3-dev
!sudo apt install python3.9-distutils
!sudo apt-get install python3-pip
!python3 -m pip install --upgrade pip

# Install graphviz
!sudo apt-get install python3-dev graphviz libgraphviz-dev pkg-config
!sudo -H apt-get install libgmp-dev python3-dev

# Install pathlib: the default version taken is 1.0.1
!pip3 install pathlib

# Install lnn lib
!pip3 install git+https://github.com/IBM/LNN.git 

which results in an error from the logs: AttributeError: 'PosixPath' object has no attribute 'read_text'

example error

When trying to run your example I get the following error: TypeError: expected variable, received <class 'tuple'>

american_enemies = (
ForAll(x, Implies(enemy(x, (y,'America')),
hostile(x),
join=Join.OUTER),
join=Join.OUTER,
world=World.AXIOM)
)

When I replace Join.OUTER with Join.INNER it compiles well, but still doesn't give me the correct result. What is the meaning of these Join constants? And what can be wrong with the formula?

Lack of tutorial

It seems like there is only 3 tutorials where Readme.md shows 10

Negation causes training to fail

Introducing a lnn.Not into a formula causes training to fail.
The following will fail unexpectedly:

from lnn import Predicate, Variable, Not, TRUE, FALSE, And
from lnn import Model

model = Model()
P = Predicate('P')
x = Variable('x')
model.add_formulae(And(P(x), Not(P(x)), name="Goal"))


model.add_facts({
    P.name: {"0":TRUE},
})

model.add_labels({
    "Goal": {"0": FALSE},
})


model.train(losses=['supervised'])
model.print(params=True)

Execution causes the following error message:

/path/.venv/bin/python3.9 /path/sol/ex_lnn.py
Traceback (most recent call last):
  File "/path/sol/ex_lnn.py", line 19, in <module>
    model.train(losses=['supervised'])
  File "/path/.venv/lib/python3.9/site-packages/lnn/model.py", line 498, in train
    _, facts_inferred = self.infer(**kwds)
  File "/path/.venv/lib/python3.9/site-packages/lnn/model.py", line 387, in infer
    bounds_diff = bounds_diff + self._traverse_execute(
  File "/path/.venv/lib/python3.9/site-packages/lnn/model.py", line 313, in _traverse_execute
    val = getattr(node, func)(**kwds) if hasattr(node, func) else None
TypeError: downward() got an unexpected keyword argument 'losses'

Removing the negation from the formula causes the code to run without problems. Is this intended behaviour, because negation is not implemented as a neural operator or is this, indeed, a bug?


Configuration:
Python version: 3.9.5

> pip freeze
lnn==1.0
networkx==2.8.4
numpy==1.22.4
torch==1.11.0
tqdm==4.64.0
typing-extensions==4.2.0

Errors on educational example.

While trying to replicate the result from the educational examples, I've got this error.

  1. url:

https://ibm.github.io/LNN/education/examples/reasoning.html#simple-geometry-reasoning-example

  1. code:
from lnn import (Predicate, Variable,
                 Exists, Implies, Forall, Model, Fact, World)

model = Model()

# Variablle
x = Variable('x')

# Predicate declarations
square = Predicate('square')
rectangle = Predicate('rectangle')
foursides = Predicate('foursides')

# Axioms declarations
square_rect = Forall(x, Implies(square(x), rectangle(x)))
rect_foursides = Forall(x, Implies(rectangle(x), foursides(x)))

# Query
query = Exists(x, foursides(x))

# Add predicates and rules to the model
model.add_knowledge(square, rectangle, square_rect, rect_foursides, query)

# Add facts to the model
model.add_data({square: {'c': Fact.TRUE, 'k': Fact.TRUE}})

# Perform inference
steps, facts_inferred = model.infer()

# Inspect the query node
print(model['foursided_objects'].true_groundings)
  1. result:
Traceback (most recent call last):
  File "/home/sungjin/home/lnn-examples/simple.py", line 33, in <module>
    print(model['foursided_objects'].true_groundings)
  File "/home/sungjin/.virtualenvs/lnn/lib/python3.10/site-packages/lnn/model.py", line 133, in __getitem__
    if formula.formula_number is not None and formula.formula_number in self.nodes:
AttributeError: 'str' object has no attribute 'formula_number'
  1. how did I do to patch the error:
# Inspect the query node
# print(model['foursided_objects'].true_groundings)
print(model[query].true_groundings)

Is there a way to input FOL statements en masse?

Is there a way to input data non-manually? I am working with FOLIO dataset which contains multiple premises in FOL form per row and a query based on the premises. The goal is to input the premises and query for each row into LNN for inference and then evaluate the response by comparing it to the label in the dataset. Is there a way to do this without hardcoding each premise and query?

Creating example of LNNs with Traditional NN

Hi,
We had a discussion with the IBM Neuro-Symbolic AI Team regarding contributing to LNN. One particular application type I became interested in is using them with traditional NN-based approaches.
I suggest LNN repository should have some examples with public datasets like keras and Theseus. These examples help practitioners get a head-first experience of the framework.
I look forward to contributing such one-click examples.
Thanks.

pycddlib cannot be installed on MacOS Monterey M1

The pycddlib installation problem cannot be resolved with the commands provided at the ReadMe unfortunately. I've done some digging at the github page of pycddlib and it seems like the currently available version (2.1.6) isn't compatible with MacOS. It seems like they experimented with new versions that are compatible (2.1.7a0) but aren't available to install yet. I've inferred all these information from the page https://github.com/mcmtroffaes/pycddlib/actions

So, how can we work around this issue and be able to install & test LNN regardless?

Currently running the code
brew install gmp; env "CFLAGS=-I/usr/local/include -L/usr/local/lib" pip install pycddlib
returns the following error:

`Collecting pycddlib
Using cached pycddlib-2.1.6.tar.gz (159 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pycddlib
Building wheel for pycddlib (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [18 lines of output]
running bdist_wheel
running build
running build_ext
cythoning cdd.pyx to cdd.c
building 'cdd' extension
creating build
creating build/temp.macosx-10.9-x86_64-3.9
creating build/temp.macosx-10.9-x86_64-3.9/cddlib
creating build/temp.macosx-10.9-x86_64-3.9/cddlib/lib-src
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/ub/opt/anaconda3/envs/lnn/include -arch x86_64 -I/Users/ub/opt/anaconda3/envs/lnn/include -fPIC -O2 -isystem /Users/ub/opt/anaconda3/envs/lnn/include -arch x86_64 -I/usr/local/include -L/usr/local/lib -DGMPRATIONAL -Icddlib/lib-src -I/Users/ub/opt/anaconda3/envs/lnn/include/python3.9 -c cdd.c -o build/temp.macosx-10.9-x86_64-3.9/cdd.o
clang: warning: argument unused during compilation: '-L/usr/local/lib' [-Wunused-command-line-argument]
In file included from cdd.c:749:
In file included from cddlib/lib-src/cdd.h:17:
cddlib/lib-src/cddmp.h:30:11: fatal error: 'gmp.h' file not found
#include "gmp.h"
^~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pycddlib
Running setup.py clean for pycddlib
Failed to build pycddlib
Installing collected packages: pycddlib
Running setup.py install for pycddlib ... error
error: subprocess-exited-with-error

× Running setup.py install for pycddlib did not run successfully.
│ exit code: 1
╰─> [18 lines of output]
running install
running build
running build_ext
skipping 'cdd.c' Cython extension (up-to-date)
building 'cdd' extension
creating build
creating build/temp.macosx-10.9-x86_64-3.9
creating build/temp.macosx-10.9-x86_64-3.9/cddlib
creating build/temp.macosx-10.9-x86_64-3.9/cddlib/lib-src
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/ub/opt/anaconda3/envs/lnn/include -arch x86_64 -I/Users/ub/opt/anaconda3/envs/lnn/include -fPIC -O2 -isystem /Users/ub/opt/anaconda3/envs/lnn/include -arch x86_64 -I/usr/local/include -L/usr/local/lib -DGMPRATIONAL -Icddlib/lib-src -I/Users/ub/opt/anaconda3/envs/lnn/include/python3.9 -c cdd.c -o build/temp.macosx-10.9-x86_64-3.9/cdd.o
clang: warning: argument unused during compilation: '-L/usr/local/lib' [-Wunused-command-line-argument]
In file included from cdd.c:749:
In file included from cddlib/lib-src/cdd.h:17:
cddlib/lib-src/cddmp.h:30:11: fatal error: 'gmp.h' file not found
#include "gmp.h"
^~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> pycddlib

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.`

Couldn't git clone due to legacy-install-failure

Error looks like below:


clang: warning: argument unused during compilation: '-L/usr/local/lib' [-Wunused-command-line-argument]
pygraphviz/graphviz_wrap.c:2711:10: fatal error: 'graphviz/cgraph.h' file not found
#include "graphviz/cgraph.h"
^~~~~~~~~~~~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> pygraphviz


I used brew to install the graphviz in requirements. It seems like the package is installed automatically somewhere else.

Whenever you see legacy-install-failure, always adding the following line in front of pip solves the problem:

env "CFLAGS=-I/opt/homebrew/include -L/opt/homebrew/lib" pip install git+https://github.com/IBM/LNN.git

Add data to formula per model

Hi and thank you for your creative idea of LNN.

I'm trying to define two different models in which a predicate takes different values, but not sure how to check the state of the predicate in a specific model? Just running .state() returns the data of the last model.

from lnn import Predicate, Variable, Implies, Equivalent, Fact, ForAll, Model


Smokes = Predicate('Smokes')

model = Model()
model.add_knowledge(Smokes)
model.add_data({
    Smokes: {
        'Anna': Fact.TRUE,
   }
})

model_fictional = Model()
model_fictional.add_knowledge(Smokes)
model_fictional.add_data({
    Smokes: {
        'Anna': Fact.FALSE
   }
})


print(Smokes.state('Anna'))

#The output of these two lines should be different, but they are the same:
model[Smokes].print()
model_fictional[Smokes].print()

URL broken

Hi.

I'm a big fan of LNN, and this github site is a great pleasure to me.

However, recently, the link to the documentation is broken, which is the section "Documentation" of the README.md.

The link is

https://ibm.github.io/LNN/introduction.html

but it says http 404 error.

Errors on educational example (it's not duplicated).

While trying to replicate the result from the educational examples, I've got this error.

  1. url:
    https://ibm.github.io/LNN/education/examples/reasoning.html#more-complex-reasoning-example

  2. code:

from lnn import (Predicate, Variable, Join, And,
                 Exists, Implies, Forall, Model, Fact, World)

model = Model()  # Instantiate a model.
x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])

# Define and add predicates to the model.
owns = model['owns'] = Predicate('owns', 2)  # binary predicate
  1. result:
Traceback (most recent call last):
  File "/home/sungjin/home/lnn-examples/complex.py", line 9, in <module>
    owns = model['owns'] = Predicate('owns', 2)  # binary predicate
TypeError: 'Model' object does not support item assignment

Runtime questions

Hi there, sorry for another question:

This is the full program for the Missle logic program:

from lnn import (Predicate, Variable, Join, And,
                 Exists, Implies, ForAll, Model, Fact, World)

model = Model()  # Instantiate a model.
x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])

missile = Predicate('missile')
american = Predicate('american')
hostile = Predicate('hostile')
criminal = Predicate('criminal')
weapon = Predicate('weapon')
owns = Predicate('owns', 2)    # binary predicate
enemy = Predicate('enemy', 2)  # binary predicate
sells = Predicate('sells', 3)  # ternary predicate

americaenemyishostile = ForAll(x, Implies(enemy(x, 'america'), hostile(x)))
missleareweapon = ForAll(x, Implies(missile(x), weapon(x)))
nonomisslesoldbywest = ForAll(x, Implies(And(missile(x), owns('nono', x)),sells('west', x, 'nono')))
youarecriminalif = ForAll(x,y,z, Implies(And(american(x), sells(x,y,z), weapon(y), hostile(z)), criminal(x)))

query = Exists(x, criminal(x))
model.add_knowledge(americaenemyishostile, missleareweapon, nonomisslesoldbywest, youarecriminalif, query)

model.add_data({
    owns: {('nono', 'm1'): Fact.TRUE},
    missile: {'m1': Fact.TRUE},
    american: {'west': Fact.TRUE},
    enemy: {('nono', 'america'): Fact.TRUE, ('wakanda', 'america'): Fact.TRUE, ('gotham', 'america'): Fact.TRUE},
})

model[query].print()
model.infer()
model[query].print()
model[query].true_groundings

Running with

model.add_data({
    owns: {('nono', 'm1'): Fact.TRUE},
    missile: {'m1': Fact.TRUE},
    american: {'west': Fact.TRUE},
    enemy: {('nono', 'america'): Fact.TRUE, ('wakanda', 'america'): Fact.TRUE, ('gotham', 'america'): Fact.TRUE},
})

yields the correct result in less than 2s:

OPEN Exists: (∃0, criminal(0))                              TRUE (1.0, 1.0)

{'west'}

However, running with two extra facts that should not impact the results

model.add_data({
    owns: {('nono', 'm1'): Fact.TRUE}, ('wakanda', 'm2'): Fact.TRUE}, ('gotham', 'm3'): Fact.TRUE}
    missile: {'m1': Fact.TRUE, 'm2': Fact.TRUE, 'm3': Fact.TRUE},
    american: {'west': Fact.TRUE},
    enemy: {('nono', 'america'): Fact.TRUE, ('wakanda', 'america'): Fact.TRUE, ('gotham', 'america'): Fact.TRUE},
})

stalls for > 1hr.

Is this common and do you know if LNN in general will be able to scale to much larger problems?

Errors in usage page.

Relates to this: https://ibm.github.io/LNN/usage.html

Step 2b: Sample code has a missing comma

formulae = [
    Smoking_causes_Cancer
    Smokers_befriend_Smokers
]

Step 3: Sample code at start generates error: TypeError: formula expected of type Formula, received str

Trace:

File LNN/lnn/model.py:346, in Model.add_data(self, data)
    344 for formula, fact in data.items():
    345     if not isinstance(formula, Formula):
--> 346         raise TypeError(
    347             "formula expected of type Formula, received "
    348             f"{formula.__class__.__name__}"
    349         )
    350     _exceptions.AssertFormulaInModel(self, formula)
    351     if formula.propositional:

Installation on Mac m1

I am trying to setup lnn on my Mac, but I get an error when I run the commands in this order below:

brew install graphviz
brew install gmp
env "CFLAGS=-I/usr/local/include -L/usr/local/lib" pip install pycddlib

I have tried installing pycddlib without the flags, but this gives the same error.

The error message:

clang-14: warning: argument unused during compilation: '-L/usr/local/lib' [-Wunused-command-line-argument]
In file included from cdd.c:6:
/Users/ismaeelbashir/miniforge3/envs/lnn/include/python3.9/Python.h:25:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
error: command '/Users/ismaeelbashir/miniforge3/envs/lnn/bin/clang' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> pycddlib

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.