support for randomising or making uniform all distributions before starting EM

This commit is contained in:
Vitor Santos Costa 2008-11-01 21:09:08 +00:00
parent 4f1d7d0524
commit ac34be01b0
3 changed files with 61 additions and 4 deletions

View File

@ -20,8 +20,11 @@
get_evidence_from_position/3, get_evidence_from_position/3,
dist_to_term/2, dist_to_term/2,
empty_dist/2, empty_dist/2,
dist_new_table/2, all_dist_ids/1,
all_dist_ids/1 randomise_all_dists/0,
randomise_dist/1,
uniformise_all_dists/0,
uniformise_dist/1
]). ]).
:- use_module(library(lists),[is_list/1,nth0/3]). :- use_module(library(lists),[is_list/1,nth0/3]).
@ -32,6 +35,10 @@
matrix_to_list/2, matrix_to_list/2,
matrix_to_logs/1]). matrix_to_logs/1]).
:- use_module(library('clpbn/matrix_cpt_utils'),
[random_CPT/2,
uniform_CPT/2]).
/* /*
:- mode dist(+, -). :- mode dist(+, -).
@ -258,3 +265,24 @@ copy_Lstructure([H|LKey], [NH|LKey0]) :-
copy_structure(H, NH), copy_structure(H, NH),
copy_Lstructure(LKey, LKey0). copy_Lstructure(LKey, LKey0).
randomise_all_dists :-
randomise_dist(_),
fail.
randomise_all_dists.
randomise_dist(Dist) :-
recorded(clpbn_dist_psizes, db(Dist,DSizes), _),
random_CPT(DSizes, NewCPT),
dist_new_table(Dist, NewCPT).
uniformise_all_dists :-
uniformise_dist(_),
fail.
uniformise_all_dists.
uniformise_dist(Dist) :-
recorded(clpbn_dist_psizes, db(Dist,DSizes), _),
uniform_CPT(DSizes, NewCPT),
dist_new_table(Dist, NewCPT).

View File

@ -15,7 +15,9 @@
multiply_factors/3, multiply_factors/3,
normalise_possibly_deterministic_CPT/2, normalise_possibly_deterministic_CPT/2,
column_from_possibly_deterministic_CPT/3, column_from_possibly_deterministic_CPT/3,
multiply_possibly_deterministic_factors/3]). multiply_possibly_deterministic_factors/3,
random_CPT/2,
uniform_CPT/2]).
:- use_module(dists, :- use_module(dists,
[get_dist_domain_size/2, [get_dist_domain_size/2,
@ -26,6 +28,7 @@
matrix_new_set/4, matrix_new_set/4,
matrix_select/4, matrix_select/4,
matrix_dims/2, matrix_dims/2,
matrix_size/2,
matrix_shuffle/3, matrix_shuffle/3,
matrix_expand/3, matrix_expand/3,
matrix_op/4, matrix_op/4,
@ -229,3 +232,25 @@ normalise_possibly_deterministic_CPT(MAT,NMAT) :-
matrix_agg_lines(MAT, +, Sum), matrix_agg_lines(MAT, +, Sum),
matrix_op_to_lines(MAT, Sum, /, NMAT). matrix_op_to_lines(MAT, Sum, /, NMAT).
random_CPT(Dims, M) :-
mult_all(Dims,1,Size),
generate_random_entries(Size, Randoms),
matrix_new(floats, Dims, Randoms, M1),
normalise_possibly_deterministic_CPT(M1, M).
mult_all([],Size,Size).
mult_all([D|Dims],Size0,Size) :-
Size1 is Size0*D,
mult_all(Dims,Size1,Size).
generate_random_entries(0, []) :- !.
generate_random_entries(Size, [R|Randoms]) :-
R is random,
Size1 is Size-1,
generate_random_entries(Size1, Randoms).
uniform_CPT(Dims, M) :-
matrix_new_set(floats,Dims,1.0,M1),
normalise_possibly_deterministic_CPT(M1, M).

View File

@ -16,7 +16,9 @@
[get_dist_domain_size/2, [get_dist_domain_size/2,
empty_dist/2, empty_dist/2,
dist_new_table/2, dist_new_table/2,
get_dist_key/2]). get_dist_key/2,
randomise_all_dists/0,
uniformise_all_dists/0]).
:- use_module(library('clpbn/connected'), :- use_module(library('clpbn/connected'),
[clpbn_subgraphs/2]). [clpbn_subgraphs/2]).
@ -62,6 +64,8 @@ em(Items, MaxError, MaxIts, Tables, Likelihood) :-
% and more detailed info on distributions, namely with a list of all instances for the distribution. % and more detailed info on distributions, namely with a list of all instances for the distribution.
init_em(Items, state( AllDists, AllDistInstances, MargVars, SolverVars)) :- init_em(Items, state( AllDists, AllDistInstances, MargVars, SolverVars)) :-
run_all(Items), run_all(Items),
% randomise_all_dists,
uniformise_all_dists,
attributes:all_attvars(AllVars0), attributes:all_attvars(AllVars0),
sort_vars_by_key(AllVars0,AllVars1,[]), sort_vars_by_key(AllVars0,AllVars1,[]),
% remove variables that do not have to do with this query. % remove variables that do not have to do with this query.