3831 20

[休闲其它] 【独家发布】Pattern Recognition and Machine Learning [推广有奖]

  • 0关注
  • 10粉丝

教授

8%

还不是VIP/贵宾

-

TA的文库  其他...

Must-Read Book

Winrats NewOccidental

Matlab NewOccidental

威望
1
论坛币
31179 个
通用积分
3.6519
学术水平
96 点
热心指数
43 点
信用等级
79 点
经验
9658 点
帖子
287
精华
10
在线时间
40 小时
注册时间
2013-12-14
最后登录
2024-4-12

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

eBook: 0.zip (8.18 MB)
Matlab Code:https://github.com/PRML/PRML
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Recognition cognition Learning machine Pattern

已有 1 人评分经验 收起 理由
苹果六人行 + 40 精彩帖子

总评分: 经验 + 40   查看全部评分

本帖被以下文库推荐

  1. function y = pdfDirichletLn(X, a)
  2. % Compute log pdf of a Dirichlet distribution.
  3. %   X: d x n data matrix satifying (sum(X,1)==ones(1,n) && X>=0)
  4. %   a: d x k parameters
  5. %   y: k x n probability density
  6. % Written by Mo Chen (mochen80@gmail.com).
  7. X = bsxfun(@times,X,1./sum(X,1));
  8. if size(a,1) == 1
  9.     a = repmat(a,size(X,1),1);
  10. end
  11. c = gammaln(sum(a,1))-sum(gammaln(a),1);
  12. g = (a-1)'*log(X);
  13. y = bsxfun(@plus,g,c');
复制代码


使用道具

  1. function y = pdfGaussLn(X, mu, sigma)
  2. % Compute log pdf of a Gaussian distribution.
  3. % Written by Mo Chen (mochen80@gmail.com).

  4. [d,n] = size(X);
  5. k = size(mu,2);
  6. if n == k && size(sigma,1) == 1           
  7.     X = bsxfun(@times,X-mu,1./sigma);
  8.     q = dot(X,X,1);  % M distance
  9.     c = d*log(2*pi)+2*log(sigma);          % normalization constant
  10.     y = -0.5*(c+q);
  11. elseif size(sigma,1)==d && size(sigma,2)==d && k==1   % one mu and one dxd sigma
  12.     X = bsxfun(@minus,X,mu);
  13.     [R,p]= chol(sigma);
  14.     if p ~= 0
  15.         error('ERROR: sigma is not PD.');
  16.     end
  17.     Q = R'\X;
  18.     q = dot(Q,Q,1);  % quadratic term (M distance)
  19.     c = d*log(2*pi)+2*sum(log(diag(R)));   % normalization constant
  20.     y = -0.5*(c+q);
  21. elseif size(sigma,1)==d && size(sigma,2)==k % k mu and k diagonal sigma
  22.     lambda = 1./sigma;
  23.     ml = mu.*lambda;
  24.     q = bsxfun(@plus,X'.^2*lambda-2*X'*ml,dot(mu,ml,1)); % M distance
  25.     c = d*log(2*pi)+2*sum(log(sigma),1); % normalization constant
  26.     y = -0.5*bsxfun(@plus,q,c);
  27. elseif size(sigma,1)==1 && (size(sigma,2)==k || size(sigma,2)==1) % k mu and (k or one) scalar sigma
  28.     X2 = repmat(dot(X,X,1)',1,k);
  29.     D = bsxfun(@plus,X2-2*X'*mu,dot(mu,mu,1));
  30.     q = bsxfun(@times,D,1./sigma);  % M distance
  31.     c = d*(log(2*pi)+2*log(sigma));          % normalization constant
  32.     y = -0.5*bsxfun(@plus,q,c);
  33. else
  34.     error('Parameters mismatched.');
  35. end
复制代码


使用道具

Compute Linear Model Reponse

  1. function [y, sigma, p] = linInfer(X, model, t)
  2. % Compute linear model reponse y = w'*x+b and likelihood
  3. % X: d x n data
  4. % t: 1 x n response
  5. w = model.w;
  6. b = model.w0;
  7. y = w'*X+b;
  8. if nargout > 1
  9.     beta = model.beta;

  10.     if isfield(model,'V')   % V*V'=inv(S) 3.54
  11.         X = model.V'*bsxfun(@minus,X,model.xbar);
  12.         sigma = sqrt(1/beta+dot(X,X,1));   % 3.59
  13.     else
  14.         sigma = sqrt(1/beta);
  15.     end
  16.    
  17.     if nargin == 3 && nargout == 3
  18. %         p = exp(logGaussPdf(t,y,sigma));
  19.         p = exp(-0.5*(((t-y)./sigma).^2+log(2*pi))-log(sigma));
  20.     end
  21. end
复制代码


使用道具

Fit Empirical Bayesian Linear Model with EM

  1. function [model, llh] = regressEbEm(X, t, alpha, beta)
  2. % Fit empirical Bayesian linear model with EM
  3. % X: d x n data
  4. % t: 1 x n response
  5. if nargin < 3
  6.     alpha = 0.02;
  7.     beta = 0.5;
  8. end
  9. [d,n] = size(X);

  10. xbar = mean(X,2);
  11. tbar = mean(t,2);

  12. X = bsxfun(@minus,X,xbar);
  13. t = bsxfun(@minus,t,tbar);

  14. C = X*X';
  15. Xt = X*t';
  16. dg = sub2ind([d,d],1:d,1:d);
  17. I = eye(d);
  18. tol = 1e-4;
  19. maxiter = 100;
  20. llh = -inf(1,maxiter+1);
  21. for iter = 2:maxiter
  22.     A = beta*C;
  23.     A(dg) = A(dg)+alpha;
  24.     U = chol(A);
  25.     V = U\I;

  26.     w = beta*(V*(V'*Xt));
  27.     w2 = dot(w,w);
  28.     err = sum((t-w'*X).^2);
  29.    
  30.     logdetA = 2*sum(log(diag(U)));   
  31.     llh(iter) = 0.5*(d*log(alpha)+n*log(beta)-alpha*w2-beta*err-logdetA-n*log(2*pi));
  32.     if llh(iter)-llh(iter-1) < tol*abs(llh(iter-1)); break; end
  33.    
  34.     trS = dot(V(:),V(:));
  35.     alpha = d/(w2+trS);   % 9.63
  36.    
  37.     gamma = d-alpha*trS;
  38.     beta = n/(err+gamma/beta);
  39. end
  40. b = tbar-dot(w,xbar);

  41. llh = llh(2:iter);
  42. model.b = b;
  43. model.w = w;
  44. model.alpha = alpha;
  45. model.beta = beta;
  46. model.xbar = xbar;
  47. model.V = V;
复制代码


使用道具

Bayesian Linear Model with Mackay Fixed Point Method

  1. function [model, llh] = regressEbFp(X, t, alpha, beta)
  2. % Fit empirical Bayesian linear model with Mackay fixed point method
  3. % X: d x n data
  4. % t: 1 x n response
  5. if nargin < 3
  6.     alpha = 0.02;
  7.     beta = 0.5;
  8. end
  9. [d,n] = size(X);

  10. xbar = mean(X,2);
  11. tbar = mean(t,2);

  12. X = bsxfun(@minus,X,xbar);
  13. t = bsxfun(@minus,t,tbar);

  14. C = X*X';
  15. Xt = X*t';
  16. dg = sub2ind([d,d],1:d,1:d);
  17. I = eye(d);
  18. tol = 1e-4;
  19. maxiter = 100;
  20. llh = -inf(1,maxiter+1);
  21. for iter = 2:maxiter
  22.     A = beta*C;
  23.     A(dg) = A(dg)+alpha;
  24.     U = chol(A);
  25.     V = U\I;

  26.     w = beta*(V*(V'*Xt));
  27.     w2 = dot(w,w);
  28.     err = sum((t-w'*X).^2);
  29.    
  30.     logdetA = 2*sum(log(diag(U)));   
  31.     llh(iter) = 0.5*(d*log(alpha)+n*log(beta)-alpha*w2-beta*err-logdetA-n*log(2*pi));
  32.     if llh(iter)-llh(iter-1) < tol*abs(llh(iter-1)); break; end
  33.    
  34.     trS = dot(V(:),V(:));
  35.     gamma = d-alpha*trS;
  36.     alpha = gamma/w2;
  37.     beta = (n-gamma)/err;
  38. end
  39. b = tbar-dot(w,xbar);

  40. llh = llh(2:iter);
  41. model.b = b;
  42. model.w = w;
  43. model.alpha = alpha;
  44. model.beta = beta;
  45. model.xbar = xbar;
  46. model.V = V;
复制代码


使用道具

Fisher Linear Discriminant Analysis

  1. function U = classFda(X, y, d)
  2. % Fisher (linear) discriminant analysis
  3. n = size(X,2);
  4. k = max(y);

  5. E = sparse(1:n,y,true,n,k,n);  % transform label into indicator matrix
  6. nk = full(sum(E));

  7. m = mean(X,2);
  8. Xo = bsxfun(@minus,X,m);
  9. St = (Xo*Xo')/n;

  10. mk = bsxfun(@times,X*E,1./nk);
  11. mo = bsxfun(@minus,mk,m);
  12. mo = bsxfun(@times,mo,sqrt(nk/n));
  13. Sb = mo*mo';
  14. % Sw = St-Sb;

  15. [U,A] = eig(Sb,St,'chol');
  16. [~,idx] = sort(diag(A),'descend');
  17. U = U(:,idx(1:d));
复制代码


使用道具

Logistic Regression for Binary Classification (Bernoulli Likelihood)

  1. function [model, llh] = classLogitBin(X, t, lambda)
  2. % logistic regression for binary classification (Bernoulli likelihood)
  3. if any(unique(t) ~= [0,1])
  4.     error('t must be a 0/1 vector!');
  5. end
  6. if nargin < 3
  7.     lambda = 1e-4;
  8. end
  9. [d,n] = size(X);
  10. dg = sub2ind([d,d],1:d,1:d);
  11. X = [X; ones(1,n)];
  12. d = d+1;

  13. tol = 1e-4;
  14. maxiter = 100;
  15. llh = -inf(1,maxiter);
  16. converged = false;
  17. iter = 1;


  18. h = ones(1,n);
  19. h(t==0) = -1;
  20. w = zeros(d,1);
  21. z = w'*X;
  22. while ~converged && iter < maxiter
  23.     iter = iter + 1;
  24.     y = sigmoid(z);
  25.    
  26.     g = X*(y-t)'+lambda*w;
  27.     r = y.*(1-y);
  28.     R = spdiags(r(:),0,n,n);   
  29.     H = X*R*X';
  30.     H(dg) = H(dg)+lambda;
  31.    
  32.     p = -H\g;

  33.     wo = w;
  34.     w = wo+p;
  35.     z = w'*X;   
  36.     llh(iter) = -sum(log1pexp(-h.*z))-0.5*lambda*dot(w,w);
  37.     converged = norm(p) < tol || abs(llh(iter)-llh(iter-1)) < tol;
  38.     while ~converged && llh(iter) < llh(iter-1)
  39.         p = 0.5*p;
  40.         w = wo+p;
  41.         z = w'*X;   
  42.         llh(iter) = -sum(log1pexp(-h.*z))-0.5*lambda*dot(w,w);
  43.         converged = norm(p) < tol || abs(llh(iter)-llh(iter-1)) < tol;
  44.     end
  45. end
  46. llh = llh(2:iter);
  47. model.w = w;
复制代码


使用道具

Logistic Regression for Multiclass Problem

  1. function [model, llh] = classLogitMul(X, t, lambda, method)
  2. % logistic regression for multiclass problem (Multinomial likelihood)
  3. if nargin < 4
  4.     method = 1;
  5. end

  6. if nargin < 3
  7.     lambda = 1e-4;
  8. end

  9. X = [X; ones(1,size(X,2))];

  10. if method == 1
  11.     [W, llh] = NewtonSolver(X, t, lambda);
  12. else
  13.     [W, llh] = blockNewtonSolver(X, t, lambda);
  14. end
  15. model.W = W;

  16. function [W, llh] = NewtonSolver(X, t, lambda)

  17. [d,n] = size(X);
  18. k = max(t);

  19. tol = 1e-4;
  20. maxiter = 100;
  21. llh = -inf(1,maxiter);
  22. converged = false;
  23. iter = 1;
  24. dk = d*k;
  25. dg = sub2ind([dk,dk],1:dk,1:dk);
  26. T = sparse(1:n,t,1,n,k,n);
  27. W = zeros(d,k);
  28. HT = zeros(d,k,d,k);
  29. while ~converged && iter < maxiter
  30.     iter = iter+1;
  31.     Z = X'*W;
  32.     logY = bsxfun(@minus,Z,logsumexp(Z,2));
  33.     llh(iter) = dot(T(:),logY(:))-0.5*lambda*dot(W(:),W(:));
  34.     converged = abs(llh(iter)-llh(iter-1)) < tol;
  35.    
  36.     Y = exp(logY);
  37.     for i = 1:k
  38.         for j = 1:k
  39.             r = Y(:,i).*((i==j)-Y(:,j));
  40.             HT(:,i,:,j) = bsxfun(@times,X,r')*X';
  41.         end
  42.     end
  43.     G = X*(Y-T)+lambda*W;
  44.     H = reshape(HT,dk,dk);
  45.     H(dg) = H(dg)+lambda;
  46.     W(:) = W(:)-H\G(:);
  47. end
  48. llh = llh(2:iter);

  49. function [W, llh] = blockNewtonSolver(X, t, lambda)

  50. [d,n] = size(X);
  51. k = max(t);

  52. dg = sub2ind([d,d],1:d,1:d);
  53. tol = 1e-4;
  54. maxiter = 100;
  55. llh = -inf(1,maxiter);
  56. converged = false;
  57. iter = 1;

  58. T = sparse(1:n,t,1,n,k,n);
  59. W = zeros(d,k);
  60. Z = X'*W;
  61. logY = bsxfun(@minus,Z,logsumexp(Z,2));
  62. Y = exp(logY);
  63. while ~converged && iter < maxiter
  64.     iter = iter+1;
  65.     for j = 1:k
  66.         r = Y(:,j).*(1-Y(:,j));
  67.         H = bsxfun(@times,X,r')*X';
  68.         H(dg) = H(dg)+lambda;

  69.         g = X*(Y(:,j)-T(:,j))+lambda*W(:,j);
  70.         W(:,j) = W(:,j)-H\g;
  71.         Z(:,j) = X'*W(:,j);
  72.         logY = bsxfun(@minus,Z,logsumexp(Z,2));
  73.         Y = exp(logY);
  74.     end
  75.    
  76.     llh(iter) = dot(T(:),logY(:))-0.5*lambda*dot(W(:),W(:));
  77.     converged = abs(llh(iter)-llh(iter-1)) < tol;
  78. end
  79. llh = llh(2:iter);
复制代码

使用道具

  1. function [model, llh] = classRvmEbEm(X, t, alpha)
  2. % Relevance Vector Machine classification training by empirical bayesian (ARD)
  3. % using standard EM update
  4. if nargin < 3
  5.     alpha = 0.02;
  6. end
  7. n = size(X,2);
  8. X = [X;ones(1,n)];
  9. d = size(X,1);
  10. alpha = alpha*ones(d,1);

  11. tol = 1e-4;
  12. maxiter = 100;
  13. llh = -inf(1,maxiter);
  14. infinity = 1e+10;
  15. for iter = 2:maxiter
  16.     used = alpha < infinity;
  17.     alphaUsed = alpha(used);
  18.     [w,V,lllh] = optLogitNewton(X(used,:),t,alphaUsed);
  19.     w2 = w.^2;
  20.    
  21.     logdetS = -2*sum(log(diag(V)));   
  22.     llh(iter) = lllh+0.5*(sum(log(alphaUsed))-logdetS-dot(alphaUsed,w2)-n*log(2*pi)); % 7.114
  23.     if abs(llh(iter)-llh(iter-1)) < tol; break; end

  24.     dgS = dot(V,V,2);
  25.     alpha = 1./(w2+dgS);    % 9.67
  26. end
  27. llh = llh(2:iter);

  28. model.used = used;
  29. model.w = w;
  30. model.alpha = alpha;
复制代码


使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-11-5 20:32