Beruflich Dokumente
Kultur Dokumente
Xanadu Halkias
Overview
Creating/accessing/indexing multidimensional
arrays
Using and computing with multidimensional
arrays
Multidimensional cell arrays
Multidimensional structure arrays
Corpus hypercubus
%Creating multidimensional
arrays
a = [1 2 3;0 6 4;2 5 4];
a(:,:,2) = [4 2 1;0 5 1;6 3 2];
a(:,:,3) = 3;
%turning a into a 3x3x3x2
a(:,:,:,2) = ones(3,3,3);
a(:,:,1,2) = 4;
More on creating
%different way
b = repmat(2,[3 3 3 2]);
c = repmat(rand(2),[3 1 2]);
%one more
d = cat(4,[1 4; 2 3],[5 8;2 3]);
Memory: try to pre-allocate eg. zeros or use repmat for best
performance
Murphys mhmm_em.m
%each page gets its own cell
if ~iscell(data)
data = num2cell(data, [1 2]);
end
numex = length(data); %num of pages is number
mixtures
O = size(data{1},1); %getting the dimensionality
Q = length(prior); %number of states
if isempty(mixmat)%number of mixtures
mixmat = ones(Q,1);
end
M = size(mixmat,2);
if M == 1
adj_mix = 0;
end
Murphys more-EM
while (num_iter <= max_iter) & ~converged
% E step
[loglik, exp_num_trans, exp_num_visits1, postmix,
m, ip, op] = ...
ess_mhmm(prior, transmat, mixmat, mu, Sigma,
data);
% M step
if verbose, fprintf(1, 'iteration %d, loglik =
%f\n', num_iter, loglik); end
num_iter = num_iter + 1;
converged = em_converged(loglik,previous_loglik,
thresh);
previous_loglik = loglik;
LL = [LL loglik];
end
The E of EM
function [loglik, exp_num_trans, exp_num_visits1,
postmix, m, ip, op] = ... ess_mhmm(prior,
transmat, mixmat, mu, Sigma, data)
Loglik: likelihood of the model
Exp_num_trans: expected number of transitions is
the transition matrix
Exp_num_visits1: expected number of visits to
each state is our priors
Postmix: is the mixing matrix
M: is the weighted observations
Ip: is the weighted inner product of the
observations
Op: is the weighted outer product of the
observations
More on E
%initialization
verbose = 0;
numex = length(data);
O = size(data{1},1);
Q = length(prior);
M = size(mixmat,2);
exp_num_trans = zeros(Q,Q);
exp_num_visits1 = zeros(Q,1);
postmix = zeros(Q,M);
m = zeros(O,Q,M);
op = zeros(O,O,Q,M);
ip = zeros(Q,M);
mix = (M>1);
Almost done
loglik = loglik + current_loglik;
if verbose, fprintf(1, 'll at ex %d = %f\n', ex,
loglik); end
exp_num_trans = exp_num_trans + sum(xi,3);
exp_num_visits1 = exp_num_visits1 + gamma(:,1);
if mix
postmix = postmix + sum(gamma2,3);
else
postmix = postmix + sum(gamma,2);
gamma2 = reshape(gamma, [Q 1 T]);
end
Last one
for i=1:Q
for k=1:M
w = reshape(gamma2(i,k,:), [1 T]); % w(t) =
w(i,k,t,l)
wobs = obs .* repmat(w, [O 1]); % wobs(:,t) =
w(t) * obs(:,t)
m(:,i,k) = m(:,i,k) + sum(wobs, 2); % m(:) =
sum_t w(t) obs(:,t)
op(:,:,i,k) = op(:,:,i,k) + wobs * obs'; %
op(:,:) = sum_t w(t) * obs(:,t) * obs(:,t)'
ip(i,k) = ip(i,k) + sum(sum(wobs .* obs, 2));
% ip = sum_t w(t) * obs(:,t)' * obs(:,t)
end
end
end
if verbose, fprintf(1, '\n'); end