ML-KULeuven / problog

ProbLog is a Probabilistic Logic Programming Language for logic programs with probabilities.
https://dtai.cs.kuleuven.be/problog/
308 stars 36 forks source link

LFI mode crashes when learning facts with multiple groundings #49

Closed lewtds closed 3 years ago

lewtds commented 3 years ago

Hi. I have this very simple model (adapted from Example 3 here https://dtai.cs.kuleuven.be/problog/tutorial/learning/03_naivebayes.html):

t(_, T)::topic(T).

query(topic(t1)).

with this evidence file:

evidence(topic(t1),true).
evidence(topic(t2),false).
-----
evidence(topic(t1),false).
evidence(topic(t2),true).
-----

I ran this command:

problog lfi learn3.pl learn3_ev.pl

And got this error message:

Huge error message, click to open ``` [ERROR] [ERROR] Error encountered: [ERROR] Traceback (most recent call last): [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main [ERROR] results = run_lfi(program, examples, knowledge=knowledge, **options) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi [ERROR] score = lfi.run() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run [ERROR] score = self.step() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step [ERROR] results = self._evaluate_examples() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples [ERROR] result = evaluator(example) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in __call__ [ERROR] results.append(self._call_internal(at, val, cval, comp, n)) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal [ERROR] evaluator = comp.get_evaluator(semiring=self, evidence=evidence) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator [ERROR] evaluator.propagate() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate [ERROR] self._initialize() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize [ERROR] weights = self.formula.extract_weights(self.semiring, self.given_weights) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights [ERROR] semiring.pos_value(w, name), [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value [ERROR] return self.value(a) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value [ERROR] return self._get_weight(*a.args) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight [ERROR] return weight[args] [ERROR] KeyError: t(t1) [ERROR] [ERROR] Error encountered: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main results = run_lfi(program, examples, knowledge=knowledge, **options) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi score = lfi.run() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run score = self.step() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step results = self._evaluate_examples() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples result = evaluator(example) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in __call__ results.append(self._call_internal(at, val, cval, comp, n)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal evaluator = comp.get_evaluator(semiring=self, evidence=evidence) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator evaluator.propagate() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate self._initialize() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize weights = self.formula.extract_weights(self.semiring, self.given_weights) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights semiring.pos_value(w, name), File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value return self.value(a) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value return self._get_weight(*a.args) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight return weight[args] KeyError: t(t1) Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main results = run_lfi(program, examples, knowledge=knowledge, **options) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi score = lfi.run() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run score = self.step() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step results = self._evaluate_examples() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples result = evaluator(example) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in __call__ results.append(self._call_internal(at, val, cval, comp, n)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal evaluator = comp.get_evaluator(semiring=self, evidence=evidence) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator evaluator.propagate() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate self._initialize() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize weights = self.formula.extract_weights(self.semiring, self.given_weights) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights semiring.pos_value(w, name), File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value return self.value(a) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value return self._get_weight(*a.args) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight return weight[args] KeyError: t(t1) An unexpected error has occurred. ```
wenchiyang commented 3 years ago

This is still under development. Extra arguments in t(_) has a high priority on our todo list, but currently is not supported yet. Essentially, example 3 on the website is a syntax sugar of the following.

is_topic(t1). is_topic(t2). is_word(w1). is_word(w2).

t()::topic(T) :- T = t1. t()::topic(T) :- T = t2. t(_)::word(W) :- is_topic(T), isword(W), topic(T), W = w1, T = t1. t()::word(W) :- is_topic(T), isword(W), topic(T), W = w1, T = t2. t()::word(W) :- is_topic(T), isword(W), topic(T), W = w2, T = t1. t()::word(W) :- is_topic(T), isword(W), topic(T), W = w2, T = t2. t()::word(W) :- is_topic(T), isword(W), +topic(T), W = w1, T = t1. t()::word(W) :- is_topic(T), isword(W), +topic(T), W = w1, T = t2. t()::word(W) :- is_topic(T), isword(W), +topic(T), W = w2, T = t1. t()::word(W) :- is_topic(T), is_word(W), +topic(T), W = w2, T = t2.

Please use this program instead of the one on the website (https://dtai.cs.kuleuven.be/problog/tutorial/learning/03_naivebayes.html).

On 14 Dec 2020, at 06:49, Trung Ngo notifications@github.com<mailto:notifications@github.com> wrote:

Hi. I have this very simple model (adapted from Example 3 here https://dtai.cs.kuleuven.be/problog/tutorial/learning/03_naivebayes.html):

t(_, T)::topic(T).

query(topic(t1)).

with this evidence file:

evidence(topic(t1),true). evidence(topic(t2),false).

evidence(topic(t1),false). evidence(topic(t2),true).

I ran this command:

problog lfi learn3.pl learn3_ev.pl

And got this error message:

Huge error message, click to open

[ERROR] [ERROR] Error encountered: [ERROR] Traceback (most recent call last): [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main [ERROR] results = run_lfi(program, examples, knowledge=knowledge, *options) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi [ERROR] score = lfi.run() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run [ERROR] score = self.step() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step [ERROR] results = self._evaluate_examples() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples [ERROR] result = evaluator(example) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in call [ERROR] results.append(self._call_internal(at, val, cval, comp, n)) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal [ERROR] evaluator = comp.get_evaluator(semiring=self, evidence=evidence) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator [ERROR] evaluator.propagate() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate [ERROR] self._initialize() [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize [ERROR] weights = self.formula.extract_weights(self.semiring, self.given_weights) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights [ERROR] semiring.pos_value(w, name), [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value [ERROR] return self.value(a) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value [ERROR] return self._get_weight(a.args) [ERROR] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight [ERROR] return weight[args] [ERROR] KeyError: t(t1) [ERROR] [ERROR] Error encountered: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main results = run_lfi(program, examples, knowledge=knowledge, *options) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi score = lfi.run() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run score = self.step() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step results = self._evaluate_examples() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples result = evaluator(example) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in call results.append(self._call_internal(at, val, cval, comp, n)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal evaluator = comp.get_evaluator(semiring=self, evidence=evidence) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator evaluator.propagate() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate self._initialize() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize weights = self.formula.extract_weights(self.semiring, self.given_weights) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights semiring.pos_value(w, name), File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value return self.value(a) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value return self._get_weight(a.args) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight return weight[args] KeyError: t(t1)

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1576, in main results = run_lfi(program, examples, knowledge=knowledge, *options) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1425, in run_lfi score = lfi.run() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1018, in run score = self.step() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 995, in step results = self._evaluate_examples() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 848, in _evaluate_examples result = evaluator(example) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1217, in call results.append(self._call_internal(at, val, cval, comp, n)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1242, in _call_internal evaluator = comp.get_evaluator(semiring=self, evidence=evidence) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 562, in get_evaluator evaluator.propagate() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 447, in propagate self._initialize() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/dd_formula.py", line 432, in _initialize weights = self.formula.extract_weights(self.semiring, self.given_weights) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/formula.py", line 192, in extract_weights semiring.pos_value(w, name), File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/evaluator.py", line 116, in pos_value return self.value(a) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1206, in value return self._get_weight(a.args) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/problog/learning/lfi.py", line 1182, in _get_weight return weight[args] KeyError: t(t1)

An unexpected error has occurred.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/ML-KULeuven/problog/issues/49, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALJYBM2DJJU6XADQ2F7TDFDSUVAGNANCNFSM4UZ3G3PA.

[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/ML-KULeuven/problog/issues/49", "url": "https://github.com/ML-KULeuven/problog/issues/49", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

lewtds commented 3 years ago

Oh thanks. That's a bummer. What I'm trying to build is a Markov Chain with a few thousand states. I was hoping I could model the chain as simply as:

t(_, State1, State2)::follow(State1, State2).

And have it learn all the states and transition probabilities from data. Is there some way I can achieve this with the current capabilities of the system?

Thanks!

wenchiyang commented 3 years ago

Extra arguments in t(_) is not enough for learning the transition function for a Markov Chain.

Say there are n states, you are essentially learning sets of conditional probabilities with the constraint that P(S1 | Si) + P(S2 | Si) + … + P(Sn | Si) = 1 for each state 1 =< i =< n. This can be done by using annotated disjunctions. https://dtai.cs.kuleuven.be/problog/tutorial/basic/02_bayes.html#annotated-disjunctions-dealing-with-multi-valued-variables

For example, a Markov chain with two states a, b can look like this file, say model.pl .

state(a). state(b).

t("") :: follows(S1, S1) ;t("") :: follows(S1, S2) :- state(S1), state(S2), S1 \= S2. query(follows(,)).

One can run "problog ground model.pl” to get some the ground program to get something like the following.

t("")::follows(a,a); t("")::follows(a,b). t("")::follows(b,b); t("")::follows(b,a).

Then replace t(“”) with t(_), then you can run LFI.

Some concepts might help with modeling Markov models and many states:

On 14 Dec 2020, at 16:10, Trung Ngo notifications@github.com<mailto:notifications@github.com> wrote:

Oh thanks. That's a bummer. What I'm trying to build is a Markov Chain with a few thousand states. I was hoping I could model the chain as simply as:

t(_, State1, State2)::follow(State1, State2).

And have it learn all the states and transition probabilities from data. Is there some way I can achieve this with the current capabilities of the system?

Thanks!

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/ML-KULeuven/problog/issues/49#issuecomment-744259800, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALJYBM5VFTO5POMKUFF6ARDSUXB7TANCNFSM4UZ3G3PA.

[ { "@context": "http://schema.org", "@type": "EmailMessage", "potentialAction": { "@type": "ViewAction", "target": "https://github.com/ML-KULeuven/problog/issues/49#issuecomment-744259800", "url": "https://github.com/ML-KULeuven/problog/issues/49#issuecomment-744259800", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { "@type": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

lewtds commented 3 years ago

Thanks for the pointer. I managed to construct arbitrary annotated disjunctions chains through metaprogramming to fit the data now.