实例介绍
【实例截图】
【核心代码】
% LEARN - Increments the specified example into the current SVM solution.
% Assumes alpha_c = 0 initially.
%
% Syntax: nstatus = learn(indc,rflag)
%
% nstatus: new status for indc
% indc: index of the example to learn
% rflag: flag indicating whether or not to check if any reserve vectors
% become margin vectors during learning
%
% Version 3.22e -- Comments to diehl@alumni.cmu.edu
%
function nstatus = learn(indc,rflag)
% flags for example state
MARGIN = 1;
ERROR = 2;
RESERVE = 3;
UNLEARNED = 4;
% define global variables
global a; % alpha coefficients
global b; % bias
global C; % regularization parameters
global deps; % jitter factor in kernel matrix
global g; % partial derivatives of cost function w.r.t. alpha coefficients
global ind; % structure containing indices of margin, error, reserve and unlearned vectors
global perturbations; % number of perturbations
global Q; % extended kernel matrix for all vectors
global Rs; % inverse of extended kernel matrix for margin vectors
global scale; % kernel scale
global type; % kernel type
global X; % matrix of margin, error, reserve and unlearned vectors stored columnwise
global y; % column vector of class labels (-1/ 1) for margin, error, reserve and unlearned vectors
% compute g(indc)
[f_c,K] = svmeval(X(:,indc));
g(indc) = y(indc)*f_c - 1;
% if g(indc) > 0, place this example into the reserve set directly
if (g(indc) >= 0)
% move the example to the reserve set
bookkeeping(indc,UNLEARNED,RESERVE);
nstatus = RESERVE;
return;
end;
% compute Qcc and Qc if necessary
num_MVs = length(ind{MARGIN});
Qc = cell(3,1);
if (num_MVs == 0)
if (length(ind{ERROR}) > 0)
Qc{ERROR} = (y(ind{ERROR})*y(indc)).*kernel(X(:,ind{ERROR}),X(:,indc),type,scale);
end;
else
Qc{MARGIN} = (y(ind{MARGIN})*y(indc)).*K(1:num_MVs);
if (length(ind{ERROR}) > 0)
Qc{ERROR} = (y(ind{ERROR})*y(indc)).*K(num_MVs 1:length(K));
end;
end;
if (length(ind{RESERVE}) > 0)
Qc{RESERVE} = (y(ind{RESERVE})*y(indc)).*kernel(X(:,ind{RESERVE}),X(:,indc),type,scale);
end;
Qcc = kernel(X(:,indc),X(:,indc),type,scale) deps;
converged = 0;
while (~converged)
perturbations = perturbations 1;
if (num_MVs > 0) % change in alpha_c permitted
% compute Qc, beta and gamma
beta = -Rs*[y(indc) ; Qc{MARGIN}];
gamma = zeros(size(Q,2),1);
ind_temp = [ind{ERROR} ind{RESERVE} indc];
gamma(ind_temp) = [Qc{ERROR} ; Qc{RESERVE} ; Qcc] Q(:,ind_temp)'*beta;
% check if gamma_c < 0 (kernel matrix is not positive semi-definite)
if (gamma(indc) < 0)
error('LEARN: gamma_c < 0');
end;
else % change in alpha_c not permitted since the constraint on the sum of the
% alphas must be preserved. only b can change.
% set beta and gamma
beta = y(indc);
gamma = y(indc)*y;
end;
% minimum acceptable parameter change (change in alpha_c (num_MVs > 0) or b (num_MVs = 0))
[min_delta_param,indss,cstatus,nstatus] = min_delta_acb(indc,gamma,beta,1,rflag);
% update a, b, and g
if (num_MVs > 0)
a(indc) = a(indc) min_delta_param;
a(ind{MARGIN}) = a(ind{MARGIN}) beta(2:num_MVs 1)*min_delta_param;
end;
b = b beta(1)*min_delta_param;
g = g gamma*min_delta_param;
% update Qc and perform bookkeeping
converged = (indss == indc);
if (converged)
cstatus = UNLEARNED;
Qc{nstatus} = [Qc{nstatus} ; Qcc];
else
ind_temp = find(ind{cstatus} == indss);
Qc{nstatus} = [Qc{nstatus} ; Qc{cstatus}(ind_temp)];
Qc{cstatus}(ind_temp) = [];
end;
[indco,removed_i] = bookkeeping(indss,cstatus,nstatus);
if ((nstatus == RESERVE) & (removed_i > 0))
Qc{nstatus}(removed_i) = [];
end;
% set g(ind{MARGIN}) to zero
g(ind{MARGIN}) = 0;
% update Rs and Q if necessary
if (nstatus == MARGIN)
num_MVs = num_MVs 1;
if (num_MVs > 1)
if (converged)
gamma = gamma(indss);
else
% compute beta and gamma for indss
beta = -Rs*Q(:,indss);
gamma = kernel(X(:,indss),X(:,indss),type,scale) deps Q(:,indss)'*beta;
end;
end;
% expand Rs and Q
updateRQ(beta,gamma,indss);
elseif (cstatus == MARGIN)
% compress Rs and Q
num_MVs = num_MVs - 1;
updateRQ(indco);
end;
end;
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论