• 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;
  • 相关阅读:
    警匪游戏规则
    敏捷开发流程总结
    天际PRO-CR16 改装方案
    世界时间(卡西欧电波表24个城市缩写翻译及简介)
    jmeter 获取总的线程数
    linux 重启jmeter服务
    jmeter 生成不重复的手机号
    Python和Java两门编程语言,学习哪个更好?
    JAVA和前端该选哪个?
    2020年Java程序员的就业前景如何?
  • 原文地址:https://www.cnblogs.com/hrlnw/p/3145110.html
Copyright © 2020-2023  润新知