Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Errors on educational example. #81

Open
daisylab opened this issue Mar 3, 2023 · 4 comments
Open

Errors on educational example. #81

daisylab opened this issue Mar 3, 2023 · 4 comments

Comments

@daisylab
Copy link

daisylab commented Mar 3, 2023

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)
@NaweedAghmad
Copy link
Collaborator

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
Copy link
Author

daisylab commented Mar 3, 2023

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
Copy link
Author

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
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants