dualgreedygstrain.m.svn-base

来自「a function inside machine learning」· SVN-BASE 代码 · 共 53 行

SVN-BASE
53
字号
function [subspaceInfo, trainInfo] = dualGreedyGSTrain(trainData, params)
%A kernel greedy version of Gram Schmidt 

if (nargin ~= 2)
    fprintf('%s\n', help(sprintf('%s', mfilename)));
    error('Incorrect number of inputs - see above usage instructions.');
end

[nameX, nameY] = getSpaceNames(params); 
[numTrainExamples, numFeatures] = getDataFieldSize(trainData, nameX);

%Store parameters
T = params.iterations; 
kernelFunction = char(params.X.kernel.name); 
kernelParams = params.X.kernel.params; 

trainKj = feval(kernelFunction, getDataFieldValue(trainData, nameX), getDataFieldValue(trainData, nameX), kernelParams);

b = zeros(numTrainExamples, T); 
tau = zeros(numTrainExamples, T); 

%We just deflate the columns of the kernel matrix by the column with max
%diagonal (I think)
for j=1:T 
    displayCount(j, T, 30);
    
    [maxK, maxInd] = max(diag(trainKj));
    
    b(maxInd, j) = 1/sqrt(maxK); %This just ensures ||q|| = ||b'Kb|| = 1
    tau(:, j) = trainKj*b(:, j); 
    
    trainKj = trainKj - tau(:, j)*tau(:, j)'/(tau(:, j)'*b(:, j));
end 

%Think the new data is the projection of the examples onto the q's which in
%kernel case is Xq = XX'b = tau 
trainInfo.data = data; 
trainInfo.data = addDataField(trainInfo.data, 'X', tau, 'examples'); 

a = b; 

%Deflate the b's to get a 
for i=1:T
    for j=i-1:-1:1
        a(:, i) = a(:, i) - a(:, i)'*tau(:, j)*b(:, j)/(b(:, j)'*tau(:, j));
    end
end

subspaceInfo = struct; 
subspaceInfo.X.a = a;  
subspaceInfo.X.b = b; 
subspaceInfo.X.tau = tau; 

⌨️ 快捷键说明

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