1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | clear %% generate data prettySpiral = 0; if ~prettySpiral % generate some random gaussian like data rand ( 'state' , 0); randn ( 'state' , 0); N= 50; D= 2; X1 = mgd(N, D, [4 3], [2 -1;-1 2]); X2 = mgd(N, D, [1 1], [2 1;1 1]); X3 = mgd(N, D, [3 -3], [1 0;0 4]); X= [X1; X2; X3]; X= bsxfun (@ rdivide , bsxfun (@minus, X, mean (X)), var (X)); y= [ ones (N, 1); ones (N, 1)*2; ones (N, 1)*3]; scatter (X(:,1), X(:,2), 20, y) else % generate twirl data! N= 50; t = linspace (0.5, 2* pi , N); x = t.* cos (t); y = t.* sin (t); t = linspace (0.5, 2* pi , N); x2 = t.* cos (t+2); y2 = t.* sin (t+2); t = linspace (0.5, 2* pi , N); x3 = t.* cos (t+4); y3 = t.* sin (t+4); X= [[x ' y' ]; [x2 ' y2' ]; [x3 ' y3' ]]; X= bsxfun (@ rdivide , bsxfun (@minus, X, mean (X)), var (X)); y= [ ones (N, 1); ones (N, 1)*2; ones (N, 1)*3]; scatter (X(:,1), X(:,2), 20, y) end %% classify rho = 1; c1 =10; c2 =10; epsilon = 0.2; threshold = 0; result=[]; % ker = 'linear'; ker = 'rbf' ; sigma = 1/10; %sigma = 1/200; par = NonLinearDualBoundSVORIM(X, y, c1, c2, epsilon, rho, ker, sigma); %f = TestPrecisionNonLinear(par,X, y,X, y, ker,epsilon,sigma); %% Plot the figure contour_level = [-epsilon,0, epsilon]; xrange = [-1.5 1.5]; yrange = [-1.5 1.5]; % step size for how finely you want to visualize the decision boundary. inc = 0.005; % generate grid coordinates. this will be the basis of the decision % boundary visualization. [x1, x2] = meshgrid (xrange(1):inc:xrange(2), yrange(1):inc:yrange(2)); % size of the (x, y) image, which will also be the size of the % decision boundary image that is used as the plot background. image_size = size (x1); xy = [x1(:) x2(:)]; % make (x,y) pairs as a bunch of row vectors. % set up the domain over which you want to visualize the decision % boundary % d = []; % for k=1:max(y) % par.normw{k}=1; % d(:,k) = decisionfun(xy, par, X,y,k,epsilon, ker,sigma)'; % end % [~,idx] = min(abs(d)/par.normw{k},[],2); % nd=max(y); nd = ( max (y)*( max (y)-1)/2); d = []; pred= zeros ( size (xy,1),nd); for k=1:nd par.normw{k}=1; d(:,k) = decisionfun(xy, par, X,y,k,epsilon, ker,sigma)'; end pred(d<-threshold) = -1; pred(d >threshold) = 1; nclass = max (y); expLosses= zeros ( size (pred,1),nclass); for i =1:nclass, expLosses(:, i ) = sum (pred == repmat (par.Code( i ,:), size (pred,1),1),2); end [minVal,finalOutput] = max (expLosses,[],2); idx = finalOutput; plt = 2; %1, just show the decison region with different colors; 2, show the decision hyperlane between class 1 and class 3 switch plt case 1 % reshape the idx (which contains the class label) into an image. decisionmap = reshape (idx, image_size); imagesc (xrange,yrange,decisionmap); % plot the class training data. hold on; set ( gca , 'ydir' , 'normal' ); cmap = [1 0.8 0.8; 0.95 1 0.95; 0.9 0.9 1]; colormap (cmap); plot (X(y==1,1), X(y==1,2), 'o' , 'MarkerFaceColor' , [.9 .3 .3], 'MarkerEdgeColor' , 'k' ); plot (X(y==2,1), X(y==2,2), 'o' , 'MarkerFaceColor' , [.3 .9 .3], 'MarkerEdgeColor' , 'k' ); plot (X(y==3,1), X(y==3,2), 'o' , 'MarkerFaceColor' , [.3 .3 .9], 'MarkerEdgeColor' , 'k' ); hold on; % title(sprintf('%d trees, Train time: %.2fs, Test time: %.2fs\n', opts.numTrees, timetrain, timetest)); case 2 %% show SVs color = {[.9 .3 .3],[.3 .9 .3],[.3 .3 .9]}; SVs = (par.SVs{2}>1e-4); for i =1: max (y) % show the SVs using biger marker plot (X(y== i &SVs==1,1),X(y== i &SVs==1,2), 'o' , 'MarkerFaceColor' , color{ i }, 'MarkerEdgeColor' , 'k' ); hold on % plot the points of not SVs plot (X(y== i &SVs~=1,1),X(y== i &SVs~=1,2), 'o' , 'MarkerFaceColor' , color{ i }, 'MarkerEdgeColor' ,color{ i }); end hold on; title ( sprintf ( 'Ratio of SVs is %.2fs\n' , mean (SVs))); color = { 'r-' , 'g-' , 'b*' , 'r.' , 'go' , 'b*' }; color1 = { 'r-' , 'g--' , 'b*' , 'r.' , 'go' , 'b*' }; contour_level1 = [-epsilon, 0, epsilon]; contour_level2 = [-epsilon, 0, epsilon]; contour_level0 = [-1,0,1]; % for k = 1:nd for k=2 decisionmapk = reshape (d(:,k), image_size); contour (x1,x2, decisionmapk, [-1 1], color1{k}, 'LineWidth' ,0.5); contour (x1,x2, decisionmapk, [contour_level(1) contour_level(1)], color{k}); contour (x1,x2, decisionmapk, [contour_level(2) contour_level(2) ], color{k}, 'LineWidth' ,2); contour (x1,x2, decisionmapk, [contour_level(3) contour_level(3) ], color{k}); contour (x1,x2, decisionmapk, [contour_level0(3) contour_level0(3) ], color1{k}, 'LineWidth' ,0.5); end end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | function par = NonLinearDualBoundSVORIM(traindata, targets, c1, c2, epsilon, rho, ker, sigma) %'traindata' is a training data matrix , each line is a sample vector %'targets' is a label vector,should start from 1 to p model= 'EX' ; % rho is the augmented Lagrangian parameter. % history is a structure that contains the objective value, the primal and % dual residual norms, and the tolerances for the primal and dual residual % norms at each iteration. %Data preprocessing [n, m] = size (traindata); Lab= sort ( unique (targets)); p= length (Lab); % the number of total rank l= zeros (1,p); id={}; X=[];Y=[]; i =1; Id = []; while i <=p id{ i }= find (targets==Lab( i )); l( i )= length (id{ i }); X=[X;traindata(id{ i },:)]; Y=[Y;targets(id{ i })]; Id = [Id, id{ i }]; i = i +1; end [~,Id0]= sort (Id); lc= cumsum (l); w = []; b = []; s = cell (1,p); r = cell (1,p); K=Kernel(ker, X ',X' ,sigma); K=(K+K')/2; nch = nchoosek ([1:p],2); Code = zeros (p, size (nch,1)); for k =1: size (nch,1) Code(nch(k,:),k) = [-1,1]; i = nch(k,1); j =nch(k,2); s{k} =lc( i )-l( i )+1:lc( i ); r{k} = lc( j )-l( j )+1:lc( j ); c{k} = [1:n]; c{k}([lc( i )-l( i )+1:lc( i ) lc( j )-l( j )+1:lc( j )]) = []; Ak = X(c{k},:); Lk = X(s{k},:); Rk =X(r{k},:); row=[c{k} c{k} s{k} r{k}]; Hk = K(row,row); % model= subSVOR(Ak,Hk,Lk,Rk,c1, c2, epsilon, rho); model = subSVOR_quadgrog(Ak, Hk, Lk,Rk,c1, c2, epsilon); P{k} = model.P; p0{k} = model.p; alpha {k} = model. alpha ; alphax{k}= model.alphax; normw{k} = model.normw; time(k) = model.time; b(k,1) = model.b; Id1= [s{k} c{k} r{k}]; SVs{k}= model.SVs(Id1); end par.l= s; par.r = r; par.c= c; par.P = P; par.p = p0; par. alpha = alpha ; par.alphax = alphax; par.normw = normw; par.time = time; par.b = b; par.X=X; par.maxtime = max (par.time); par.SVs = SVs; par.Y=Y; par.Code = Code; % par.w = w; % par.b = b; end function par = subSVOR_quadgrog(Ak, H, Lk,Rk, c1, c2, epsilon) t_start = tic ; %Global constants and defaults QUIET = 0; m = size (Ak,2); lk = size (Ak,1); rk1 = size (Lk,1); rk2 = size (Rk,1); rk = rk1+rk2; %ADMM solver mP=2*lk +rk; %dimension of Phi mG=4*lk + 2*rk; %dimension of Gamma mU=mG+1; %dimension of U mp1 = 1 : lk; mp2 = lk+1 : 2*lk; mp3 = 2*lk+1: mP; c= zeros (mU,1); c([mp1 mp2]+1) = c1; c(mp3+1) = c2; q = ones (mP,1); q(mp1) = epsilon; q(mp2) = epsilon; q(mp3) = -1; p = ones (mP,1); p(mp2) = -1; p(mP-rk2+1:mP) = -1; % H = [Ak; -Ak; Bk{1}; -Bk{2}]*[Ak; -Ak; Bk{1}; -Bk{2}]'; %linear Kernel % Qk=[Ak; Ak; Lk; Rk]; % H= Kernel(ker, Qk',Qk',sigma); % H=(H'+H)/2+1; H0 = (H+1).*(p*p'); % % options = optimoptions('quadprog',... % % 'Algorithm','interior-point-convex','Display','off'); options = optimoptions( 'quadprog' ,... 'Algorithm' , 'interior-point-convex' , 'MaxIter' ,200, 'Display' , 'off' ); % % x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x0,options) A = []; b = []; f = q; Aeq =[]; beq = []; lb = zeros (mP,1); ub = c(2:mP+1); x0 = []; P = quadprog(H0,f,A,b,Aeq,beq,lb,ub,x0,options); % diagnostics, reporting, termination checks par.P= P; par.p= p; par. alpha = P(mp1); par.alphax = P(mp2); P3=P(mp3); par.SVs = [P3(1:rk1); abs (P(mp1)-P(mp2));P3(rk1+1:rk1+rk2)]; % par.normw = sqrt(P'*(H0.*(p'*p))*P); par.normw =1; bk =(p'*P); par.b =bk; % switch ker % case 'linear' % par.w = [Ak; -Ak; Bk{1}; -Bk{2}]'*P; % b1 = Ak(P(mp1)~=0,:)* par.w-epsilon; % b2 = Ak(P(mp2)~=0,:)* par.w+epsilon; % par.b = mean([b1;b2]); % end if ~QUIET par.time = toc (t_start); end end function [par, history] = subSVOR(Ak,H, Lk,Rk, c1, c2, epsilon, rho) %'traindata' is a training data matrix , each line is a sample vector %'targets' is a label vector,should start from 1 to p % rho is the augmented Lagrangian parameter. % history is a structure that contains the objective value, the primal and % dual residual norms, and the tolerances for the primal and dual residual % norms at each iteration. t_start = tic ; %Data preprocessing %Global constants and defaults QUIET = 0; MAX_ITER = 200; % ABSTOL = 1e-4; % RELTOL = 1e-2; ABSTOL = 1e-6; RELTOL = 1e-3; lk = size (Ak,1); rk1 = size (Lk,1); rk2 = size (Rk,1); rk = rk1+rk2; %ADMM solver mP=2*lk +rk; %dimension of Phi mG=4*lk + 2*rk; %dimension of Gamma mU=mG; %dimension of U P = zeros (mP,1); %Phi={ w,b, xi, xi*} G = zeros (mG,1); %Gamma={eta,eta*,delta, phi, phi*} U = zeros (mU,1); %U- -update mp1 = 1 : lk; mp2 = lk+1 : 2*lk; mp3 = 2*lk+1: mP; c= zeros (mU,1); c([mp1 mp2]) = c1; c(mp3) = c2; q = ones (mP,1); q(mp1) = epsilon; q(mp2) = epsilon; q(mp3) = -1; p = ones (mP,1); p(mp2) = -1; p(mP-rk2+1:mP) = -1; % H = [Ak; -Ak; Bk{1}; -Bk{2}]*[Ak; -Ak; Bk{1}; -Bk{2}]'; %linear Kernel % Qk=[Ak; Ak; Lk; Rk]; % H0= Kernel(ker, Qk',Qk',sigma);%Kernel Matrix of Bound SVM % H = (H0+1).*(p*p'); % H0= Kernel(ker, Qk',Qk',sigma);%Kernel Matrix of Bound SVM H = (H+1).*(p*p'); k=1; while k <=MAX_ITER %Phi={ w,b, xi, xi*}-update V = U + B(mP,G) - c; br = - q - rho * AtX(mP,V); [P, niters] = cgsolve(H, br, rho); %Gamma={eta,eta*,delta, phi, phi*}-update with relaxation Gold = G; G = pos(Bt(mP,c-AX(P)-U)); %U- -update r = AX(P) + B(mP,G) - c; U = U + r; % history.objval(k) = objective(H,P,q); s = rho*AtX(mP,B(mP,G - Gold)); history.r_norm(k) = norm (r); history.s_norm(k) = norm (s); history.eps_pri(k) = sqrt (mU)*ABSTOL + RELTOL* max ([ norm (AX(P)), norm (B(mP,G)), norm (c)]); history.eps_dual(k)= sqrt (mP)*ABSTOL + RELTOL* norm (rho*AtX(mP,U)); if history.r_norm(k) < history.eps_pri(k) && ... history.s_norm(k) < history.eps_dual(k); break end k = k+1; end if ~QUIET par.time = toc (t_start); end par.P= P; par.p= p; par. alpha = P(mp1); par.alphax = P(mp2); % par.normw = sqrt(P'*(H0.*(p'*p))*P); par.normw=1; bk =(p'*P); par.b =bk; if ~QUIET par.time = toc (t_start); end end % function obj = objective(H,P,q) % obj = 1/2 * vHv(H,P) + q'*P; % end function [x, niters] = cgsolve(H, b,rho,tol, maxiters) % cgsolve : Solve Ax=b by conjugate gradients % % Given symmetric positive definite sparse matrix A and vector b, % this runs conjugate gradient to solve for x in A*x=b. % It iterates until the residual norm is reduced by 10^-6, % or for at most max(100,sqrt(n)) iterations n = length (b); if ( nargin < 4) tol = 1e-6; maxiters = max (100, sqrt (n)); elseif ( nargin < 5) maxiters = max (100, sqrt (n)); end normb = norm (b); x = zeros (n,1); r = b; rtr = r'*r; d = r; niters = 0; while sqrt (rtr)/normb > tol && niters < maxiters niters = niters+1; % Ad = A*d; Ad = AtAX(H, d,rho); alpha = rtr / (d'*Ad); x = x + alpha * d; r = r - alpha * Ad; rtrold = rtr; rtr = r'*r; beta = rtr / rtrold; d = r + beta * d; end end %Ad = A*d function Ad = AtAX(H, d,rho) Ad = H*d +2* rho*d; end function F = AtX(mP,V) F = V(1:mP)+V(mP+1: end ); end function h = AX(P) h = [P;P]; end function h = vHv(H,d) h = d'*(H*d) ; end function Bv= B(mP,v) Bv(:,1) = [v(1:mP);-v(mP+1: end )]; end function Btd = Bt(mP,d) Btd = [d(1:mP);-d(mP+1: end )]; end function A = pos(A) A(A<0)=0; end |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧