improve learning in CLP(BN)
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2093 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
parent
efe6c4eaef
commit
afb734b51a
35
CLPBN/learning/learn_utils.yap
Normal file
35
CLPBN/learning/learn_utils.yap
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
%
|
||||||
|
% Utilities for learning
|
||||||
|
%
|
||||||
|
|
||||||
|
:- module(bnt_learn_utils, [run_all/1,
|
||||||
|
clpbn_vars/2]).
|
||||||
|
|
||||||
|
run_all([]).
|
||||||
|
run_all([G|Gs]) :-
|
||||||
|
call(user:G),
|
||||||
|
run_all(Gs).
|
||||||
|
|
||||||
|
clpbn_vars(Vs,BVars) :-
|
||||||
|
get_clpbn_vars(Vs,CVs),
|
||||||
|
keysort(CVs,KVs),
|
||||||
|
merge_vars(KVs,BVars).
|
||||||
|
|
||||||
|
get_clpbn_vars([],[]).
|
||||||
|
get_clpbn_vars([V|GVars],[K-V|CLPBNGVars]) :-
|
||||||
|
clpbn:get_atts(V, [key(K)]), !,
|
||||||
|
get_clpbn_vars(GVars,CLPBNGVars).
|
||||||
|
get_clpbn_vars([_|GVars],CLPBNGVars) :-
|
||||||
|
get_clpbn_vars(GVars,CLPBNGVars).
|
||||||
|
|
||||||
|
merge_vars([],[]).
|
||||||
|
merge_vars([K-V|KVs],[V|BVars]) :-
|
||||||
|
get_var_has_same_key(KVs,K,V,KVs0),
|
||||||
|
merge_vars(KVs0,BVars).
|
||||||
|
|
||||||
|
get_var_has_same_key([K-V|KVs],K,V,KVs0) :- !,
|
||||||
|
get_var_has_same_key(KVs,K,V,KVs0).
|
||||||
|
get_var_has_same_key(KVs,_,_,KVs).
|
||||||
|
|
||||||
|
|
||||||
|
|
42
CLPBN/learning/mle.yap
Normal file
42
CLPBN/learning/mle.yap
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
%
|
||||||
|
% This assumes we have a single big example.
|
||||||
|
%
|
||||||
|
|
||||||
|
:- use_module(library('clpbn_learning/utils'),
|
||||||
|
[run_all/1,
|
||||||
|
clpbn_vars/2]).
|
||||||
|
|
||||||
|
:- module(bnt_mle, [learn_parameters/2]).
|
||||||
|
|
||||||
|
%
|
||||||
|
% full evidence learning
|
||||||
|
%
|
||||||
|
learn_parameters(Items, Tables, Extras) :-
|
||||||
|
run_all(Items),
|
||||||
|
attributes:all_attvars(AVars),
|
||||||
|
% sort and incorporte evidence
|
||||||
|
clpbn_vars(AVars, AllVars),
|
||||||
|
mk_sample(AllVars, Sample),
|
||||||
|
compute_tables(Extras, Sample, Tables).
|
||||||
|
|
||||||
|
mk_sample(AllVars, NVars, LL) :-
|
||||||
|
add2sample(AllVars, Sample),
|
||||||
|
msort(Sample, AddL),
|
||||||
|
compute_params(AddL, Parms).
|
||||||
|
|
||||||
|
add2sample([], []).
|
||||||
|
add2sample([V|Vs],[val(Id,[Ev|EParents])|Vals]) :-
|
||||||
|
clpbn:get_atts(V, [evidence(Ev),dist(Id,Parents)]),
|
||||||
|
get_eparents(Parents, EParents),
|
||||||
|
add2sample(Vs, Vals).
|
||||||
|
|
||||||
|
get_eparents([P|Parents], [E|EParents]) :-
|
||||||
|
clpbn:get_atts(V, [evidence(Ev)]),
|
||||||
|
get_eparents(Parents, EParents).
|
||||||
|
get_eparents([], []).
|
||||||
|
|
||||||
|
|
||||||
|
compute_tables([], Sample, Tables) :-
|
||||||
|
mle(Sample, Tables).
|
||||||
|
compute_tables([laplace|_], Sample, Tables) :-
|
||||||
|
laplace(Sample, Tables).
|
Reference in New Issue
Block a user