zoukankan      html  css  js  c++  java
  • UFLDL教程练习(exercise)答案(2)

    主成分分析与白化,这部分很简单,当然,其实是用Matlab比较简单,要是自己写SVD分解算法,足够研究好几个月的了。下面是我自己实现的练习答案,不保证完全正确,不过结果和网站上面给出的基本一致。

    1.PCA in 2D

    1.1 Step 1a: Implement PCA to obtain U

    u = zeros(size(x, 1)); % You need to compute this
    sigma = x * x' / size(x, 2);
    [u,s,v]=svd(sigma);

    1.2 Step 1b: Compute xRot, the projection on to the eigenbasis

    xRot = zeros(size(x)); % You need to compute this
    xRot=u'*x;

    1.3 Step 2: Reduce the number of dimensions from 2 to 1. 

    k = 1; % Use k = 1 and project the data onto the first eigenbasis
    xHat = zeros(size(x)); % You need to compute this
    x_ap=u(:,1:k)'*x;
    xHat(1:k,:)=x_ap;
    xHat=u*xHat;

    1.4 Step 3: PCA Whitening

    xPCAWhite = zeros(size(x)); % You need to compute this
    xPCAWhite = diag(1./sqrt(diag(s) + epsilon)) * u' * x;

    1.5 Step 3: ZCA Whitening

    xZCAWhite = zeros(size(x)); % You need to compute this
    xZCAWhite=u * diag(1./sqrt(diag(s) + epsilon)) * u' * x;

    2.PCA and Whitening

    2.1 Step 0b: Zero-mean the data (by row)

    avg = mean(x, 1);
    x = x - repmat(avg, size(x, 1), 1);

    2.2 Step 1a: Implement PCA to obtain xRot

    xRot = zeros(size(x)); % You need to compute this
    sigma = x * x' / size(x, 2);
    [U,S,V]=svd(sigma);
    xRot=U'*x;

    2.3 Step 1b: Check your implementation of PCA

    covar = zeros(size(x, 1)); % You need to compute this
    covar = xRot * xRot' / size(xRot, 2);

    2.4 Step 2: Find k, the number of components to retain

    k = 0; % Set k accordingly
    sum_k=0;
    sum=trace(S);
    for k=1:size(S,1)
        sum_k=sum_k+S(k,k);
        if(sum_k/sum>=0.99) %0.9
            break;
        end
    end

    2.5 Step 3: Implement PCA with dimension reduction

    xHat = zeros(size(x));  % You need to compute this
    xTilde = U(:,1:k)' * x;
    xHat(1:k,:)=xTilde;
    xHat=U*xHat;

    2.6 Step 4a: Implement PCA with whitening and regularisation

    xPCAWhite = diag(1./sqrt(diag(S) + epsilon)) * U' * x;

    2.7 Step 4b: Check your implementation of PCA whitening

    covar = zeros(size(xPCAWhite, 1));
    covar = xPCAWhite * xPCAWhite' / size(xPCAWhite, 2);

    2.8 Step 5: Implement ZCA whitening

    xZCAWhite=U * diag(1./sqrt(diag(S) + epsilon)) * U' * x;
  • 相关阅读:
    什么样的代码称得上是好代码?
    九年程序人生 总结分享
    Docker入门 第一课 --.Net Core 使用Docker全程记录
    阿里云 Windows Server 2012 r2 部署asp.net mvc网站 平坑之旅
    Visual studio 2015 Community 安装过程中遇到问题的终极解决
    Activiti6.0 spring5 工作流引擎 java SSM流程审批 项目框架
    java 进销存 库存管理 销售报表 商户管理 springmvc SSM crm 项目
    Leetcode名企之路
    24. 两两交换链表中的节点
    21. 合并两个有序链表
  • 原文地址:https://www.cnblogs.com/hrlnw/p/3145110.html
Copyright © 2011-2022 走看看