IBM / LNN

A `Neural = Symbolic` framework for sound and complete weighted real-value logic
https://IBM.github.io/LNN/
Apache License 2.0
230 stars 442 forks source link

Errors on educational example. #81

Open daisylab opened 1 year ago

daisylab commented 1 year ago

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'
  2. how did I do to patch the error:

# Inspect the query node
# print(model['foursided_objects'].true_groundings)
print(model[query].true_groundings)
NaweedAghmad commented 1 year ago

Using str lookups was the old API, try using the foursided_objects directly and not its str name. If that works, it would be great if you could open up a PR to help update the docs

daisylab commented 1 year ago

Unfortunately, using foursided_objects did not work, which I somewhat expected, because we did not declare that at all.

In fact, the foursided_objects which we are looking for is actually the query, and it can be found by:

query = Exists(x, foursides(x))

print(model[query].true_groundings)
daisylab commented 1 year ago

Hello, it's me again.

I was able to produce the result shown in the tutorial. However, after updating the LNN package to the master branch, the tutorial code no longer works.

the expected output is {'c', 'k'}, but the code just says set().

BigCakeLove commented 2 months ago

query = Exists(x, foursides(x))

Add predicates and rules to the model

model.add_knowledge(square, rectangle, square_rect, rect_foursides, query, world=World.AXIOM)

Add facts to the model

model.add_data({square: {'c': Fact.TRUE, 'k': Fact.TRUE}})

Perform inference

model.infer()

print(model[query].true_groundings)

after adding world=World.AXIOM, I got the result