IBM / LNN

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

Add data to formula per model #69

Closed HRezaei closed 1 year ago

HRezaei commented 1 year ago

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()
KyleErwin commented 1 year ago

Hi @HRezaei. That is because you're using the same instance of Smokes for both models. Defining an instance for each model will solve your problem. Let me know if you have any further questions.

NaweedAghmad commented 1 year ago

To answer this question a bit more verbosely, a model is simply a collection of formulae - so the Smokes predicate is in fact the same object added to different models. You effectively overwrote the fact of Anna to be False for both models, since there is only one predicate object. Creating two Smokes objects for each model will solve your issue.

HRezaei commented 1 year ago

Thanks @KyleErwin and @NaweedAghmad,

Defining an instance for each model will solve your problem. Let me know if you have any further questions.

I guess you meant defining an instance for each Smokes right? Because, in the above example, I have already defined a new instance for the model.

So, if my guess is correct, then the answer is, that's not always easily possible to define a new instance for each predicate/formula. To demonstrate it in simple scenario steps: 1- you create a model, 2- do some initial inference, 3- make different duplicates of the model, 4- on each model, add different data for some formula, 5- run inference for each.

In step 3, it is not easy to iterate over all formulas of the model, and redefine them, is it? Especially, if we want to keep their latest data which are produced as a result of inference in step 2.