LNN
LNN copied to clipboard
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?
Let me provide the full code as I made it based on your example. It gives me an error when I try to run it.
from lnn import (Predicate, Variable, And, Join,
Exists, Implies, ForAll, Model, Fact, World)
model = Model()
# Variablle
x, y, z, w = map(Variable, ['x', 'y', 'z', 'w'])
# Define and add predicates to the model.
owns = Predicate(arity=2, name='owns')
missile = Predicate(arity=1, name='missile')
american = Predicate(arity=1, name='american')
enemy = Predicate(arity=2, name='enemy')
hostile = Predicate(arity=1, name='hostile')
criminal = Predicate(arity=1, name='criminal')
weapon = Predicate(arity=1, name='weapon')
sells = Predicate(arity=3, name='sells')
# Define and add the background knowledge to the model.
is_criminal = ForAll(x, y, z,
Implies(
And(american(x), weapon(y), sells(x, y, z), hostile(z)), criminal(x),
join=Join.OUTER),
name='is-criminal', join=Join.OUTER, world=World.AXIOM)
is_selling = ForAll(x,
Implies(
And(missile(x), owns('Nono', x)), sells('West', x, 'Nono'),
name='is-selling', join=Join.OUTER),
name='is-selling', join=Join.OUTER, world=World.AXIOM)
missile_is_weapon = ForAll(x,
Implies(missile(x), weapon(x),
name='is-weapon', join=Join.OUTER),
name='missile-is-weapon', join=Join.OUTER, world=World.AXIOM)
american_enemies = (
ForAll(x, Implies(enemy(x, "America"),
hostile(x),
join=Join.OUTER),
join=Join.OUTER,
world=World.AXIOM)
)
# Query
query = Exists(x, criminal(x), name='criminal-west')
# Add predicates and rules to the model
model.add_knowledge(owns, missile, american, enemy, hostile, criminal, weapon, sells, is_criminal, is_selling, missile_is_weapon, american_enemies, query)
# Add facts to the model
model.add_data({
owns: {('Nono', 'M1'): Fact.TRUE},
missile: {'M1': Fact.TRUE},
american: {'West': Fact.TRUE},
enemy: {('Nono', 'America'): Fact.TRUE},
})
# Perform inference
steps, facts_inferred = model.infer()
print(query.true_groundings)
The error is as follows:
Traceback (most recent call last):
File "/lnn_test.py", line 60, in <module>
steps, facts_inferred = model.infer()
File "/venv/lib/python3.10/site-packages/lnn/model.py", line 497, in infer
return self._infer(
File "/venv/lib/python3.10/site-packages/lnn/model.py", line 541, in _infer
bounds_diff += self._traverse_execute(
File "/venv/lib/python3.10/site-packages/lnn/model.py", line 438, in _traverse_execute
val = getattr(node, func)(**kwds) if hasattr(node, func) else None
File "/venv/lib/python3.10/site-packages/lnn/symbolic/logic/connective_neuron.py", line 93, in upward
upward_bounds = _gm.upward_bounds(self, self.operands, groundings)
File "/venv/lib/python3.10/site-packages/lnn/symbolic/_gm.py", line 39, in upward_bounds
result = _operational_bounds(self, Direction.UPWARD, operands, groundings)
File "/venv/lib/python3.10/site-packages/lnn/symbolic/_gm.py", line 166, in _operational_bounds
ground_tuples, ground_objects = _hash_join_outer(self, tmp_bindings)
File "/venv/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/venv/lib/python3.10/site-packages/lnn/symbolic/_gm.py", line 437, in _hash_join_outer
g_obj = operands[i]._ground(op_tup)
File "/venv/lib/python3.10/site-packages/lnn/symbolic/logic/formula.py", line 905, in _ground
raise Exception(
Exception: expected grounding length to be of arity 3, received ('M1',)
I get the same/very similar error. My code:
from lnn import (Predicate, Variable, Join, And, Exists, Implies, ForAll, Model,
Fact, World)
model = Model()
x, y, z, w = map(Variable, ["x", "y", "z", "w"])
owns = Predicate('owns', 2) # binary predicate
missile = Predicate('missile')
american = Predicate('american')
enemy = Predicate('enemy', 2)
hostile = Predicate('hostile')
criminal = Predicate('criminal')
weapon = Predicate('weapon')
sells = Predicate('sells', 3) # ternary predicate
america_enemies = (
ForAll(x,
Implies(enemy(x, 'America'), hostile(x), join=Join.OUTER),
join=Join.OUTER,
world=World.AXIOM
)
)
model.add_knowledge(america_enemies)
model.add_knowledge(*[
ForAll(x, Implies(missile(x), weapon(x))),
ForAll(x, Implies(
And(missile(x), owns('nono', x)), sells('west', x, 'nono')
)
),
ForAll(x, y, z, Implies(
And(american(x), weapon(y), sells(x,y,z), hostile(z)),
criminal(x)
))
])
model.add_data({
owns: {
('nono', 'm1'): Fact.TRUE
},
missile: {
"m1": Fact.TRUE,
"m2": Fact.TRUE,
"m3": Fact.TRUE
},
american: {
"west": Fact.TRUE
},
enemy: {
('nono', 'america'): Fact.TRUE
}
}
)
query = Exists(x, criminal(x))
model.add_knowledge(query)
model.infer()
error:
Exception Traceback (most recent call last)
Cell In [16], line 1
----> 1 model.infer()
File ~/repositories/neurosymb/LNN/lnn/model.py:497, in Model.infer(self, direction, source, max_steps, lifted, **kwds)
494 if lifted:
495 self.lift(lifted)
--> 497 return self._infer(
498 direction=direction,
499 source=source,
500 max_steps=max_steps,
501 lifted=False,
502 **kwds,
503 )
File ~/repositories/neurosymb/LNN/lnn/model.py:541, in Model._infer(self, direction, source, max_steps, **kwds)
539 bounds_diff = 0.0
540 for d in direction:
--> 541 bounds_diff += self._traverse_execute(
542 d.value.lower(), d, source, **kwds
543 )
544 converged_bounds = (
545 True
546 if direction in ([[Direction.UPWARD], [Direction.DOWNWARD]])
...
908 )
909 else:
910 if self.num_unique_vars != 1 and arity_match:
Exception: expected grounding length to be of arity 2, received ('nono',)