1. 程式人生 > >Robust PCA大法好!

Robust PCA大法好!

Robust PCA

When dealing with high-dimensional data sets, we often leverage on the fact that the data has low intrinsic dimensionality in order to alleviate the curse of dimensionality and scale (perhaps it lies in a low-dimensional subspace or lies on a low-dimensional manifold). Principal component analysis

is handy for eliminating dimensions.

Classical PCA seeks the best rank-k estimate L of M (minimizing ML where L has rank-k). Truncated SVD makes this calculation!

Traditional PCA can handle small noise, but is brittle with respect to grossly corrupted observations– even one grossly corrupt observation can significantly mess up answer.

Robust PCA factors a matrix into the sum of two matrices, M=L+S, where M is the original matrix, L is low-rank, and S is sparse. This is what we’ll be using for the background removal problem!

  • Low-rank means that the matrix has a lot of redundant information– in this case, it’s the background, which is the same in every scene.
    下圖是原矩陣
    M
    (4800, 11300),4800是一幀圖片60x80拉長後的向量長度,11300是幀數。圖中的solid橫線代表background;wave代表行人的moving;column代表 a moment in time。原矩陣 M 每一列都是某時間點上一幀影象拉長後形成的vector,背景矩陣的rows(圖中的horizontal lines)indicates L 應當是個low-rank的矩陣;
    矩陣M
  • Sparse means that the matrix has mostly zero entries– in this case, see how the picture of the foreground (the people) is mostly empty. (In the case of corrupted data, S is capturing the corrupted entries. Assume that corrupt data is sparse).

Robust PCA的應用

Face Recognition

Robust PCA Optimization

Source

The general primary component pursuit algorithm from this Robust PCA paper (Candes, Li, Ma, Wright), in the specific form of Matrix Completion via the Inexact ALM Method found in section 3.1 of this paper (Lin, Chen, Ma).

Optimize Algorithms

I. principal component pursuit

Robust PCA can be written:

minimizeL+λS1subjecttoL+S=M
where:
  • 1 is the L1 norm. Minimizing the L1 norm results in sparse values. For a matrix, the L1 norm is equal to the maximum absolute column norm.

    • Q:這裡為啥用 L1 norm呢?

    因為 L1 norm會lead更好的稀疏性,而我們需要 S to be a sparse matrix.

  • is the nuclear norm, which is the L1 norm of the singular values. Trying to minimize this results in sparse singular values –> low rank.

  • shrinkage operator Sτ[x]=sgn(x)max(|x|τ,0)
    S 中的singular value,然後減去 τ ,如果value比 τ 小,就round them to 0. 如果 value 大於 τ 就將兩者的差反號,使得singular value更接近0。總的來說這個shrinkage操作就是使得singular value smaller,從而使它們更加sparse。不要忘了我們的目標是:找到一個sparse的 S

  • singular value thresholder D
    就是先對矩陣做SVD分解,然後通過 S 的 shrinkage 操作使得singular value變小之後,再reconstruct Dτ(X)=USτ()V 得到Low_rank矩陣 L 的approximation。

    這個操作有點像 truncated SVD,也就是說把singular value比較小(小於 τ)的那些vector cut off。

  • 第3、4步的意思大概是,因為我們的目標是要把原矩陣分解成一個 Low_rank 矩陣 L 加上一個 Sparse 矩陣 S ,所以這裡我們要不斷讓 Low_rank矩陣等於原矩陣 M 減去 Sparse 矩陣 S,同時讓 S 儘可能等於 ML。是一個比較典型的交替更新的方法。

  • μ1Yk 是為了 keep track of what you still have to approximate(residual error).

II. ALM

Section 3.1 of Chen, Lin, Ma contains a faster vartiation of this:

And Section 4 has some very helpful implementation details on how many singular values to calculate (as well as how to choose the parameter values):

Implement principal component pursuit

from scipy import sparse
from sklearn.utils.extmath import randomized_svd
import fbpca

# TOL是收斂的boundary
TOL=1e-9
MAX_ITERS=3

def converged(Z, d_norm):
    err = np.linalg.norm(Z, 'fro') / d_norm
    print('error: ', err)
    return err < TOL

shrinkage operator:把小於 τ 的singular value都置為0,從另一角度來看就是類似在truncating matrix,ignore那些很小的singular value。

def shrink(M, tau):
    S = np.abs(M) - tau
    return np.sign(M) * np.where(S>0, S, 0)
# 寫作pca, 讀作svd,實際上是一個fast svd implementation
def _svd(M, rank): return fbpca.pca(M, k=min(rank, np.min(M.shape)), raw=True)

def norm_op(M): return _svd(M, 1)[1][0]

# D操作
# 這裡的rank是因為fbpca.pca做的是randomized svd,所以需要指定你要保留的rank數
def svd_reconstruct(M, rank, min_sv):
    u, s, v = _svd(M, rank)
    # 這裡面min_sv就是下面提到的sv
    s -= min_sv
    nnz = (s > 0).sum()
    return u[:,:nnz] @ np.diag(s[:nnz]) @ v[:nnz], nnz

principal component pursuit關鍵程式碼實現:

# principal component pursuit是Robust PCA的其中一種algorithm
def pcp(X, maxiter=10, k=10): # refactored
    m, n = X.shape
    trans = m<n
    if trans: X = X.T; m, n = X.shape

    lamda = 1/np.sqrt(m)
    op_norm = norm_op(X)
    Y = np.copy(X) / max(op_norm, np.linalg.norm( X, np.inf) / lamda)
    mu = k*1.25/op_norm; mu_bar = mu * 1e7; rho = k * 1.5

    d_norm = np.linalg.norm(X, 'fro')
    L = np.zeros_like(X); sv = 1

    examples = []

    for i in range(maxiter):
        print("rank sv:", sv)
        X2 = X + Y/mu

        # update estimate of Sparse Matrix by "shrinking/truncating": original - low-rank
        # 對應step 4
        S = shrink(X2 - L, lamda/mu)

        # update estimate of Low-rank Matrix by doing truncated SVD of rank sv & reconstructing.
        # count of singular values > 1/mu is returned as svp
        # 對應step 3
        L, svp = svd_reconstruct(X2 - S, sv, 1/mu)

        '''
        這個sv是個劃重點的東西,代表了你在truncating svd希望保留的rank數目,這個值需要不停地更新
        原始矩陣為400*11300,很明顯我們不可能做full svd,肯定賊慢
        所以我們先甩掉singular value小於1/μ對應的那些vector,
            * 如果我們得到的rank:svp < sv,說明我們想要的value(不小於1/μ的)都已經保留了,
              即我們甩掉的value都是該甩的,則下一輪只將 sv + 1(因為第sv之後的singular value也沒包含太多資訊了)
            * 否則即 svp = sv 說明第sv個singular value之後的value可能還有大於1/μ的,
              我們甩多了,所以下一輪就要多保留一些sv
        看Results中的輸出來check一下
        '''

        # If svp < sv, you are already calculating enough singular values.
        # If not, add 20% (in this case 240) to sv
        sv = svp + (1 if svp < sv else round(0.05*n))

        # residual
        Z = X - L - S
        Y += mu*Z; mu *= rho

        # keep track Time=140 時的visual
        examples.extend([S[140,:], L[140,:]])

        if m > mu_bar: m = mu_bar
        if converged(Z, d_norm): break

    if trans: L=L.T; S=S.T
    return L, S, examples

Results

m, n = M.shape
round(m * .05)

#240
L, S, examples =  pcp(M, maxiter=5, k=10)
rank sv: 1
error:  0.131637937114
rank sv: 241
error:  0.0458515689041
rank sv: 49
error:  0.00588796042886
rank sv: 289
error:  0.000565564416732
rank sv: 529
error:  2.47254909523e-05

sv 初始化為 1,然後加上 n 的 20%,即 240。
然後 svp = 48 < 241,所以 sv = 48 + 1 =49。

相關推薦

Robust PCA大法

Robust PCA When dealing with high-dimensional data sets, we often leverage on the fact that the data has low intrinsic dimensional

卡常數大法

保存 下一條 最快 else inline block ram comm font 咳咳咳……好東西 _(:зゝ∠)_ 轉自某位大佬 http://www.cnblogs.com/widerg/p/7353866.html C++ Interesting卡常數 作為一名O

【對拍大法

 :loop    data.exe   a.exe   b.exe   fc a.out b.out   if not errorlevel 1 goto loop

表示式求值——————Python大法

樸素的表示式求值演算法 加減乘除,用py來寫的 用雙棧來實現就可以 我後續會進行C/C++程式碼的補充; 先來看一下python的寫法 第一種 直接模擬雙棧,python的寫法比較簡單一下 def compare(op1, op2): """ 比較兩個

python大法 這三行中的第二行程式碼可以說盡顯霸氣了

Exercise: Follow the instructions and implement model(). When examples[index] contains one dinosaur name (string), to create an exa

sort大法———自定義的註意事項

網址 返回 .html ron 必須 ref 好用 nbsp targe 眾所周知,在c++中,sort是一個非常好用的排序函數,方便使用、可自定義的特性,讓眾多oier如我不能自拔。但是在自定義時也有一些大坑需要註意(敲黑板),下面就是oi入門的第不知道多

搭建CNN模型破解網站驗證碼Python大法真的

專案介紹 在文章CNN大戰驗證碼中,我們利用TensorFlow搭建了簡單的CNN模型來破解某個網站的驗證碼。驗證碼如下:   網站驗證碼 在本文中,我們將會用Keras來搭建一個稍微複雜的CNN模型來破解以上的驗證碼。 資料集 對於驗證碼圖片的處理過程在本文

BNU 34974 MATLAB大法

output 內存泄露 轉置 輸出 查錯 ava 大小 2個 clear 題目鏈接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=34974 MATLAB大法好 Time Limit: 8000ms Memory

【微軟大法】VS Tools for AI全攻略(2)

port shell orf 方式 virt cnblogs 我們 玩耍 虛擬 接著上文,我們來討論如何使用Azure資源來訓練我們的tensorflow項目。Azure雲我個人用得很多,主要是因為微軟爸爸批了150刀每月的額度,我可以愉快地玩耍。 那麽針對Azure,有成

各位前輩

blog pan 交流 soft mil font 不足 nbsp style 大家好,剛申請的博客,以便平時記錄筆記以及與大家進行交流。初來乍到,請各位前輩多多關照,如有不足,歡迎吐槽!各位前輩好!

5.5提越南越

徹底 越南 ext mage 最大的 erl 技術 現在 .com 環境越艱難,成長的速度越快,千萬提防新疆那個夥伴, 做噩夢 專業課123 數學99 英語88 政治79 嚇死我了,給我嚇醒了, 數學只有99分,TMD!!! 其他的科目都是平平,普普

MySQL 分庫分表方案,總結的非常

導致 一個 磁盤空間 所有 bsp 功能 編程 從庫 框架 前言 公司最近在搞服務分離,數據切分方面的東西,因為單張包裹表的數據量實在是太大,並且還在以每天60W的量增長。 之前了解過數據庫的分庫分表,讀過幾篇博文,但就只知道個模糊概念, 而且現在回想起來什麽都是模模糊糊的

區塊鏈技術前景那麽

區塊鏈技術研究區塊鏈是什麽,區塊鏈在今後會有怎樣的發展?   區塊鏈技術是繼互聯網、無線通信、雲計算、大數據之後計算和網絡技術的又一創新。      區塊鏈可能是2017年的大熱門了,為此而衍生的比特幣和國內大量ICO項目更是抄的熱火朝天,雖然前段時間國內發文把ICO項目一刀切了,也是為了防止騙局無限放大,但

三分大法

png 解決 算法 san 極值 如果 例題 strong right 三分算法解決凸形或者凹形函數的極值; 如下圖 lmid = (Left + Right) / 2 rmid = (lmid + Right) / 2; 如果lmid靠近極值點,則Right = rmi

python大法——python的下載與安裝、第一個程序

src 結果 這就是 anaconda text image 分享圖片 百度 .com 吃夠了java的苦,所以python好。 打今天起,要走python了。 首先呢,學習python需要python環境、和一款得心應手的集成開發環境。 python環境下載:htt

python大法——模塊(內置模塊未完)

inter form pre mktime 隨機 時間戳 範圍 內置模塊 基礎 模塊 模塊是非常簡單的Python文件,單個Python文件就是一個模塊,兩個文件就是兩個模塊。 Python模塊有什麽作用? 1、模塊內有許多函數方法,利用這些方法可以更簡單的完成許多工

python大法——Python 正則表達式

strong 替換字符 區別 all 表達 多行模式 位置 one 否則 Python 正則表達式 正則表達式是一個特殊的字符序列,它能幫助你方便的檢查一個字符串是否與某種模式匹配。 Python 自1.5版本起增加了re 模塊,它提供 Perl 風格的正則表達式模式。

python大法——飛機大戰完整吧

rom type index Coding 記錄 說明 fir 檢測 als # -*- coding:utf-8 -*-import pygamefrom pygame.locals import *import time‘‘‘說明1.按下b鍵,讓玩家飛機爆炸 2.爆炸效

python大法——mysql防註入

ges 一個 bject res 如何 cte mysql 指定 reg MySQL 及 SQL 註入 如果您通過網頁獲取用戶輸入的數據並將其插入一個MySQL數據庫,那麽就有可能發生SQL註入安全的問題。 本章節將為大家介紹如何防止SQL註入,並通過腳本來過濾SQL中

珂學大法

com ast xe8 asa sin 分享圖片 圖片 ora see 珂朵莉好可愛啊\(QAQ\)! 珂學大法好