6. スパース回帰:回帰における外れ値の問題
最小二乗法:L2ノルム最小化に基づく線形回帰
問題点:少数の外れ値(outlier)で線形回帰の結果が大きく変わる
x = sort(4*(rand(25,1)-.5)); % Random data from [-2,2]
y = .9*x + .1*randn(size(x)); % Line y=.9x with noise
atrue = xy; % Least-squares slope (no outliers)
y(end) = -5.5; % Introduce outlier
acorrupt = xy; % New slope
x = sort(4*(rand(25,1)-.5)); % Random data from [-2,2]
y = .9*x + .1*randn(size(x)); % Line y=.9x with noise
atrue = xy; % Least-squares slope (no outliers)
y(1) = -5.5; % Introduce outlier
acorrupt = xy; % New slope
8. スパース回帰:回帰における外れ値問題の解法
cvx_begin;
variable aL1; % aL1 is slope to be optimized
minimize( norm(aL1*x-y,1) ); % minimize L1 error
cvx_end;
1
min k k
a
ax y−
L1ノルム最小解は外れ値に対して頑健(ロバスト)である!
CH03_SEC05_1_RobustRegression_production.m
9. LASSO回帰:優決定系(n>m)におけるスパース回帰
2
2 1
λ− +x b xA
Least absolute shrinkage and selection operator (LASSO) 解
二乗誤差項
「データへの当てはまり度」
L1ノルム項
「解のスパース度」
• 「データへの当てはまり度」と「解のスパース度」のトレードオフ
• パラメタ λ はそのトレードオフを決める
小さいλ → データによくあてはまるが、密な解
大きいλ → 疎ではあるが、データをうまく説明しない解
• パラメタλ の値を適切に選択する必要がある → 交差検証法(クロスバリデーショ
ン)
10. LASSO回帰:優決定系(n>m)におけるスパース回帰
Least absolute shrinkage and selection operator (LASSO) 解
LASSOの振る舞いを調べるため、 の場合を考えてみよう。
( ){ }2 2
2 1
1
i
n
i
i
ix b xλ λ
=
+−− + =∑xx b
=A I
2
2 1
λ− +x b xA
=A I
( )
2
i i ib xx λ− +
各変数が分離されている -> 一変数の最小化問題を考えればよい。
11. LASSO回帰:優決定系(n>m)におけるスパース回帰
( )
( )
( )2
for
2 2
ˆ arg min 0 for
2
for
2 2
sgn
2
x
b b
x J x b
b b
b b
S bλ
λ λ
λ
λ λ
λ
+
−
= <
+
= −
=
≥
−
≤
( ) ( )
2
b xJ x x λ−= +
一変数の最小化問題
12. LASSO回帰:優決定系(n>m)におけるスパース回帰
( ) ( )
2
b xJ x x λ−= +
一変数の最小化問題
( )
2
2
2
2
for
4 2
min for
4 2
for
4 2
2 4
x
b b
J x b
b b
b
λ λλ
λ λ
λ λλ
λ λ
λ
+
−
<
− −
=
≥
≤
− +
−
13. LASSO回帰:優決定系(n>m)におけるスパース回帰
( ) ( )
2
b xJ x x λ−= +
一変数の最小化問題
Least absolute shrinkage and selection operator
L1ノルムを最小化 解を縮小 非ゼロ成分を選択(疎)
19. LASSO回帰:優決定系におけるスパース回帰
LASSO解 と最小二乗解 の比較
2L
+
=x A b
1
2
L 2 1
arg min λ= − +
x
x Ax b x
1Lx 2Lx
• 正解の成分3と8は正しく求められている
• しかし0であるべき他の成分が非ゼロとなる
(密)
• 正解の成分3と8のみ非ゼロ(疎)
• 成分3と8は正解より若干小さく求められる
Least absolute shrinkage and selection operator
L1ノルムを最小化 解を縮小 非ゼロ成分を選択(疎)
成分3
成分7
21. スパース表現による分類(Sparse representation for classification)
CH03_SEC06_SparseRepresentation_production.m
%% Build Training and Test sets
Training = zeros(size(faces,1),30*20); % 20名×30枚の訓練データ
Test = zeros(size(faces,1),20*20); % 20名×20枚の訓練データ
% 20 subjects, each having 30 training and 20 test images
for k=1:20 % 20名の顔データを行列として整形する
baseind = 0;
if(k>1) baseind = sum(nfaces(1:k-1)); end
inds = baseind + (1:nfaces(k)); % indices for subject k
Training(:,(k-1)*30+1:k*30) = faces(:,inds(1:30));
Test(:,(k-1)*20+1:k*20) = faces(:,inds(31:50));
end
訓練データ(Training): 20名×30枚の顔画像
テストデータ(Test) : 20名×20枚の顔画像
顔分類問題:訓練データが与えられたとき、テストデー
タがどの被験者なのかを当てる。
データの読み込み
23. スパース表現による分類(Sparse representation for classification)
「スパース表現による分類」の基本的な考え方
テストデータをなるべく少ない(疎)な成分で
フィットすることで、テストデータがどの被験
者のものかを決める
1
min subject to =
s
Θss y
1 2
min subject to − <
s
s y Θs
%% L1 SEARCH, TESTCLEAN
b1s = Bsmall(:,1);
eps = .01;
cvx_begin;
variable a1(M);
minimize( norm(a1,1) );
subject to
norm(TrainingSmall*a1 - b1s,2) < eps;
cvx_end;
26. ロバスト主成分分析(Robust PCA)
( ) 0,
min rank subject to+ =+
L S
L S X L S
1, *
min subject toλ+ =+
L S
L S X L S
ロバストPCAの理想:行列Lのランクと行列SのL0ノルムを最小化したい。
ロバストPCAの評価関数:行列Lの核ノルムと行列SのL1ノルムを最小化
ランクとL0ノルムの最小化は計算量が膨大。
凸緩和(convex relaxation)
もともとの評価関数に似ている、より扱いやすい評
価関数に置き換えること
27. ベクトルと行列の凸緩和
・ベクトル の場合
( )0
#=s sの非ゼロ成分
1
1
n
i
i
s
=
= ∑s
n
∈s
L0ノルム
L1ノルム
・行列 の場合
ランク ( ) ( )rank # iσ=X の非ゼロ成分
核ノルム
( )rank
*
1i
iσ
=
= ∑
X
X
L0ノルム
L1ノルム
( )0
#=X X行列 の非ゼロ成分
1
, j
i
i
jX= ∑X
n m×
∈X
28. 拡張ラグランジュ法(Augmented Lagrangian Multiplier (ALM) Method)
( ) ( ){ } 2
* 1 F
t, r,
2
µ
λ+ + − −= − + −Y X L S X LL S Y L S S
ロバストPCAで解くべき拘束条件付き最適化問題
1, *
min subject toλ+ =+
L S
L S X L S
(拡張)ラグランジアン
評価関数 拘束条件 拘束条件の二乗
29. 拡張ラグランジュ法(Augmented Lagrangian Multiplier (ALM) Method)
( ) ( ){ } 2
* 1 F
t, r,
2
µ
λ+ + − −= − + −Y X L S X LL S Y L S S
(拡張)ラグランジアン
評価関数 拘束条件 拘束条件の二乗
( ) ( ) ( )
2
1
* F
, ,
2
µ
µ−
+ +− − −L X SL S Y L LY に依らない項
行列Sを固定した時の行列Lの更新式
( )1
1/SVT µ µ−
= − −X S YLSingular-value thresholding
30. 拡張ラグランジュ法(Augmented Lagrangian Multiplier (ALM) Method)
( ) ( ){ } 2
* 1 F
t, r,
2
µ
λ+ + − −= − + −Y X L S X LL S Y L S S
(拡張)ラグランジアン
評価関数 拘束条件 拘束条件の二乗
( ) ( ) ( )1
1
2
F
, ,
2
µ
λ µ−
+ − − +−= S XL LS Y S SY に依らない項
行列Lを固定した時の行列Sの更新式
( )1
/Sλ µ µ−
−= −X L YSSoft thresholding
31. ロバスト主成分分析(Robust PCA)による画像ノイズ除去
% Example of low-rank matrix from CH01_SEC07_1.m
t = (-3:.01:3)';
N = length(t);
Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)];
Strue = [2 0; 0 .5];
Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)];
L0 = Utrue*Strue*Vtrue';
% sparse noise of 5% density
S0 = sprandn(N,N,0.05);
% linear superposition of low-rank L0 and sparse S0
X = L0+S0;
% robust PCA
[L, S] = RPCA(X);
X 0L 0S
+=
32. ロバスト主成分分析(Robust PCA)による画像ノイズ除去
% Example of low-rank matrix from CH01_SEC07_1.m
t = (-3:.01:3)';
N = length(t);
Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)];
Strue = [2 0; 0 .5];
Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)];
L0 = Utrue*Strue*Vtrue';
% sparse noise of 5% density
S0 = sprandn(N,N,0.05);
% linear superposition of low-rank L0 and sparse S0
X = L0+S0;
% robust PCA
[L, S] = RPCA(X);
X L S
,
RPCA
33. ロバスト主成分分析と特異値分解の比較(1/2)
% Example of low-rank matrix from CH01_SEC07_1.m
% t = (-3:.01:3)';
t = (-3:.05:3)';
N = length(t);
Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)];
Strue = [2 0; 0 .5];
Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)];
L0 = Utrue*Strue*Vtrue';
% sparse noise of 5% density
S0 = 3*sprandn(N,N,0.05);
% linear superposition of low-rank L0 and sparse S0
X = L0+S0;
% robust PCA
[L, S] = RPCA(X);
% SVD and rank-2 reconstruction
[U1, S1, V1] = svd(X);
Xapprox = U1(:,1:2)*S1(1:2,1:2)*V1(:,1:2)';
%%
figure(1); clf;
imagesc(X); colormap(gray); axis equal off;
figure(2); clf;
imagesc(L); colormap(gray); axis equal off;
figure(3); clf;
imagesc(reshape(Xapprox,N,N)); colormap(gray); axis
equal off;
36. function [L,S] = RPCA(X)
[n1,n2] = size(X);
mu = n1*n2/(4*sum(abs(X(:))));
lambda = 1/sqrt(max(n1,n2));
thresh = 1e-7*norm(X,'fro');
L = zeros(size(X)); % low-rank approximation
S = zeros(size(X)); % sparse noise
Y = zeros(size(X)); % Lagrange multiplier
count = 0;
while((norm(X-L-S,'fro')>thresh)&&(count<1000))
% singular-value thresholding
L = SVT(X-S+(1/mu)*Y,1/mu);
% shrinkage
S = shrink(X-L+(1/mu)*Y,lambda/mu);
Y = Y + mu*(X-L-S);
count = count + 1
end
function out = SVT(X,tau)
[U,S,V] = svd(X,'econ');
out = U*shrink(S,tau)*V';
end
function out = shrink(X,tau)
out = sign(X).*max(abs(X)-tau,0);
end
( ) ( ) ( )ax ,0sgn mS x xxλ λ= ⋅ −
① 行列Sの計算 Shrinkage
② 行列Lの計算
Singular-value thresholding
①
②
ロバスト主成分分析(Robust PCA)による顔画像解析
41. • ロバスト主成分分析と特異値分解の比較の例
下記のコードのノイズ強度を変化させて、ロバストPCAとSVDの結果がどのように変化するかを見て
みましょう。
• ロバスト主成分分析による顔画像解析の例
CH03_SEC07_RPCA.m
コードを走らせてみましょう。関数RPCA, SVT, shrinkageの内容を読んでみましょう。
ここで演習
% Example of low-rank matrix from CH01_SEC07_1.m
% t = (-3:.01:3)';
t = (-3:.05:3)';
N = length(t);
Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)];
Strue = [2 0; 0 .5];
Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)];
L0 = Utrue*Strue*Vtrue';
% sparse noise of 5% density
S0 = 3*sprandn(N,N,0.05);
% linear superposition of low-rank L0 and sparse S0
X = L0+S0;
% robust PCA
[L, S] = RPCA(X);
% SVD and rank-2 reconstruction
[U1, S1, V1] = svd(X);
Xapprox = U1(:,1:2)*S1(1:2,1:2)*V1(:,1:2)';
%%
figure(1); clf;
imagesc(X); colormap(gray); axis equal off;
figure(2); clf;
imagesc(L); colormap(gray); axis equal off;
figure(3); clf;
imagesc(reshape(Xapprox,N,N)); colormap(gray); axis
equal off;
42. RPCAコードの修正
function [L,S] = RPCA(X)
[n1,n2] = size(X);
mu = n1*n2/(4*sum(abs(X(:))));
lambda = 1/sqrt(max(n1,n2));
thresh = 1e-7*norm(X,'fro');
S = zeros(size(X));
Y = zeros(size(X));
L = zeros(size(X));
count = 0;
while((norm(X-L-
S,'fro')>thresh)&&(count<1000))
L = SVT(X-S+(1/mu)*Y,1/mu);
S = shrink(X-L+(1/mu)*Y,lambda/mu);
Y = Y + mu*(X-L-S);
count = count + 1;
end
44. 参考文献
LASSO回帰
Tibshirani, R. (1996). Regression shrinkage and selection via the lasso. Journal of
the Royal Statistical Society: Series B (Methodological), 58(1), 267-288.
スパース表現による分類
Wright, J., Yang, A. Y., Ganesh, A., Sastry, S. S., & Ma, Y. (2008). Robust face
recognition via sparse representation. IEEE transactions on pattern analysis and
machine intelligence, 31(2), 210-227.
ロバストPCA
Candès, E. J., Li, X., Ma, Y., & Wright, J. (2011). Robust principal component
analysis?. Journal of the ACM (JACM), 58(3), 1-37.