To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / CPDs / @softmax_CPD / convert_to_table.m @ 8:b5b38998ef3b
History | View | Annotate | Download (2.23 KB)
| 1 |
function T = convert_to_table(CPD, domain, evidence) |
|---|---|
| 2 |
% CONVERT_TO_TABLE Convert a softmax CPD to a table, incorporating any evidence |
| 3 |
% T = convert_to_table(CPD, domain, evidence) |
| 4 |
|
| 5 |
self = domain(end); |
| 6 |
ps = domain(1:end-1); |
| 7 |
cnodes = domain(CPD.cpndx); |
| 8 |
cps = myintersect(ps, cnodes); |
| 9 |
dps = domain(CPD.dpndx); |
| 10 |
dps_as_cps = domain(CPD.dps_as_cps.ndx); |
| 11 |
all_dps = union(dps,dps_as_cps); |
| 12 |
odom = domain(~isemptycell(evidence(domain))); |
| 13 |
if ~isempty(cps), assert(myismember(cps, odom)); end % all cts parents must be observed |
| 14 |
|
| 15 |
ns = zeros(1, max(domain)); |
| 16 |
ns(domain) = CPD.sizes; |
| 17 |
ens = ns; % effective node sizes |
| 18 |
ens(odom) = 1; |
| 19 |
|
| 20 |
% dpsize >= glimsz because the glm parameters are tied across the dps_as_cps parents |
| 21 |
dpsize = prod(ens(all_dps)); % size of ALL self'discrete parents |
| 22 |
dpvals = cat(1, evidence{myintersect(all_dps, odom)});
|
| 23 |
cpvals = cat(1, evidence{cps});
|
| 24 |
if ~isempty(dps_as_cps), |
| 25 |
separator = CPD.dps_as_cps.separator; |
| 26 |
dp_as_cpmap = find_equiv_posns(dps_as_cps, all_dps); |
| 27 |
dops_map = find_equiv_posns(myintersect(all_dps, odom), all_dps); |
| 28 |
puredp_map = find_equiv_posns(dps, all_dps); |
| 29 |
subs = ind2subv(ens(all_dps), 1:prod(ens(all_dps))); |
| 30 |
if ~isempty(dops_map), subs(:,dops_map) = subs(:,dops_map)+repmat(dpvals(:)',[size(subs,1) 1])-1; end |
| 31 |
end |
| 32 |
|
| 33 |
[w,b] = extract_params(CPD); |
| 34 |
T = zeros(dpsize, ns(self)); |
| 35 |
for i=1:dpsize, |
| 36 |
active_glm = i; |
| 37 |
dp_as_cpvals=zeros(1,sum(ns(dps_as_cps))); |
| 38 |
if ~isempty(dps_as_cps), |
| 39 |
active_glm = max([1,subv2ind(ns(dps), subs(i,puredp_map))]); |
| 40 |
% Extract the params compatible with the observations (if any) on the 'pure' discrete parents (if any) |
| 41 |
where_one = separator + subs(i,dp_as_cpmap); |
| 42 |
% and get in the dp_as_cp parents... |
| 43 |
dp_as_cpvals(where_one)=1; |
| 44 |
end |
| 45 |
T(i,:) = normalise(exp([dp_as_cpvals(:); cpvals(:)]'*w(:,:,active_glm) + b(:,active_glm)')); |
| 46 |
end |
| 47 |
if myismember(self, odom) |
| 48 |
r = evidence{self};
|
| 49 |
T = T(:,r); |
| 50 |
end |
| 51 |
|
| 52 |
T = myreshape(T, ens(domain)); |