sparsecovariancestest.m

来自「a function inside machine learning」· M 代码 · 共 58 行

M
58
字号
%A script to test sparse covariances (a bit) 
clear;
rand('state',22); 

numExamples = 15; 
numFeatures = 10; 
tol = 10^-5; 

X = rand(numExamples, numFeatures); 
Y = sign(rand(numExamples, 1)-0.5); 

X = centerData(X); 

KX = X*X'; 

[covariances, bs] = sparseCovariances(KX, KX, Y, Y, (1:numExamples)'); 

covariances2 = (diag(diag(KX))^-0.5)*KX'*Y;

if norm(abs(covariances2) - covariances) > tol 
    error('Covariances are computed incorrectly'); 
end 

%Now check bs are correct 
for i=1:numExamples 
    b = zeros(numExamples, 1); 
    b(i) = bs(i); 
    
    if b'*KX*b - 1 > tol 
        error('Dual projections not computed correctly'); 
    end 
end 



%Now simulate deflation by blanking a column 
KX(:, 2) = 0; 
[covariances, bs] = sparseCovariances(KX, KX, Y, Y, (1:numExamples)'); 

if covariances(2) ~= 0 | bs(2) ~= 0
    covariances
    bs
    error('Problem computing alingment for zeroed kernel matrix columns'); 
end 

%Check if multi label covariances work
KX = X*X'; 

numLabels = 5; 
Y = sign(rand(numExamples, numLabels)-0.5); 

[covariances, bs] = sparseCovariances(KX, KX, Y, Y, (1:numExamples)'); 
covariances2 = (diag(diag(KX))^-0.5)*sqrt(diag(KX'*Y*Y'*KX));

if norm(abs(covariances2) - covariances) > tol 
    error('Covariances are computed incorrectly in multilabel case'); 
end 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?