Skip to content

Commit

Permalink
Merge pull request #32 from sth4nth/master
Browse files Browse the repository at this point in the history
extracted demos
  • Loading branch information
sth4nth committed Mar 20, 2016
2 parents 1149629 + 62846e4 commit dcf0a7b
Show file tree
Hide file tree
Showing 60 changed files with 636 additions and 501 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The goal of the code are as follows:
2. Efficient: utilizing matlab vectorization trick as much as possible to make the function fast. Many functions are even comparable with C implementation. Usually, functions in this package are orders faster than matlab builtin functions which provide same functionality (such as kmeans). If anyone found any matlab implementation which is faster than mine, I am happy to further optimize.
3. Robust: many numerical stability techniques are applied, such as probability computation in log scale to avoid numerical underflow and overflow, square root form update of symetric matrix, etc.
4. Easy to learn: the code is heavily commented. Reference formulas in PRML book are indicated for corresponding code lines.
5. Practical: the package is designed not only for users to learn the algorithms in the book, but also to facilitate ML reseearch. Many functions in this package are already among the top downloads in Matlab [file exchange](http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A49739) site and widely used.
5. Practical: the package is designed not only for users to learn the algorithms in the book, but also to facilitate ML research. Many functions in this package are already among the top downloads in Matlab [file exchange](http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A49739) site and widely used.

License
-------
Expand Down
3 changes: 2 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TODO:
ch08: BP
ch10: EP

ch13: LDS stability
ch05: MLP bias
32 changes: 19 additions & 13 deletions chapter02/logGauss.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
function y = logGauss(X, mu, Sigma)
function y = logGauss(X, mu, sigma)
% Compute log pdf of a Gaussian distribution.
% Input:
% X: d x n data matrix
% mu: d x 1 mean vector of Gaussian
% Sigma: d x d covariance matrix of Gaussian
% sigma: d x d covariance matrix of Gaussian
% Output:
% y: 1 x n probability density in logrithm scale y=log p(x)
% Written by Mo Chen ([email protected]).
[d,k] = size(mu);
assert(all(size(Sigma)==d) && k==1) % one mu and one dxd Sigma
X = bsxfun(@minus,X,mu);
[R,p]= chol(Sigma);
if p ~= 0
error('ERROR: Sigma is not PD.');
end
Q = R'\X;
q = dot(Q,Q,1); % quadratic term (M distance)
c = d*log(2*pi)+2*sum(log(diag(R))); % normalization constant
y = -0.5*(c+q);

if all(size(sigma)==d) && k==1 % one mu and one dxd sigma
X = bsxfun(@minus,X,mu);
[R,p]= chol(sigma);
if p ~= 0
error('ERROR: sigma is not PD.');
end
Q = R'\X;
q = dot(Q,Q,1); % quadratic term (M distance)
c = d*log(2*pi)+2*sum(log(diag(R))); % normalization constant
y = -0.5*(c+q);
elseif size(sigma,1)==1 && size(sigma,2)==size(mu,2) % k mu and (k or one) scalar sigma
X2 = repmat(dot(X,X,1)',1,k);
D = bsxfun(@plus,X2-2*X'*mu,dot(mu,mu,1));
q = bsxfun(@times,D,1./sigma); % M distance
c = d*(log(2*pi)+2*log(sigma)); % normalization constant
y = -0.5*bsxfun(@plus,q,c);
end
32 changes: 0 additions & 32 deletions chapter03/demo.m

This file was deleted.

19 changes: 0 additions & 19 deletions chapter04/demo.m

This file was deleted.

6 changes: 0 additions & 6 deletions chapter05/demo.m

This file was deleted.

16 changes: 16 additions & 0 deletions chapter05/mlpPred.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function y = mlpPred(model, X)
% Multilayer perceptron prediction
% Input:
% model: model structure
% X: d x n data matrix
% Ouput:
% Y: p x n response matrix
% Written by Mo Chen ([email protected]).
W = model.W;
L = length(W)+1;
Z = cell(L);
Z{1} = X;
for l = 2:L
Z{l} = sigmoid(W{l-1}'*Z{l-1});
end
y = Z{L};
18 changes: 0 additions & 18 deletions chapter06/knRegPlot.m

This file was deleted.

37 changes: 0 additions & 37 deletions chapter07/demo1.m

This file was deleted.

11 changes: 0 additions & 11 deletions chapter08/bpMaxSum.m

This file was deleted.

10 changes: 0 additions & 10 deletions chapter08/bpSumProd.m

This file was deleted.

85 changes: 0 additions & 85 deletions chapter09/demo.m

This file was deleted.

43 changes: 0 additions & 43 deletions chapter10/demo.m

This file was deleted.

9 changes: 6 additions & 3 deletions chapter10/rvmRegVb.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
c0 = prior.c;
d0 = prior.d;
end
idx = (1:m)';
dg = sub2ind([m,m],idx,idx);
I = eye(m);
xbar = mean(X,2);
tbar = mean(t,2);
Expand All @@ -36,11 +38,12 @@

a = a0+1/2;
c = c0+n/2;
Ealpha = 1e-4;
Ebeta = 1e-4;
Ealpha = 1e-2;
Ebeta = 1e-2;
for iter = 2:maxiter
% q(w)
invS = diag(Ealpha)+Ebeta*XX;
invS = Ebeta*XX;
invS(dg) = invS(dg)+Ealpha;
U = chol(invS);
Ew = Ebeta*(U\(U'\Xt));
KLw = -sum(log(diag(U)));
Expand Down
2 changes: 2 additions & 0 deletions chapter13/LDS/TODO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(1) test against matlab implementation of kalman filter
(2) simplify ldsEm with less parameters (G=diag(g), S=I)
7 changes: 0 additions & 7 deletions chapter14/cart.m

This file was deleted.

7 changes: 0 additions & 7 deletions chapter14/cartPred.m

This file was deleted.

Loading

0 comments on commit dcf0a7b

Please sign in to comment.