sequential minimal optimization,SMO for SVM, (MATLAB code)
function model = SMOforSVM(X, y, C )
%sequential minimal optimization,SMO tol = 0.001; maxIters = 3000; global i1 i2 K Alpha M1 m1 w b [m, n] = size(X); K = (X*X'); Alpha = zeros(m,1); w = 0; b = 0;
flag =1;iters = 1;
while flag >0 & iters < maxIters
[i1,i2,m1,M1] = selectWorkSet(y, C);
if m1 - M1 <= tol
break;
end
solveOptimization(X, y, C)
iters = iters +1;
end model.alpha = Alpha; id = find(Alpha < C & Alpha >0);
% b = mean(y(id)' - (y.*Alpha)'*K(:, id)); id = id(1);
b = y(id)' - (y.*Alpha)'*K(:, id); w= (y.*Alpha)'* X;
model.w = w;
model.b = b;
end %Selecting working set B
function [i1,i2,m1,M1]=selectWorkSet(y, C)
global K Alpha I_up =find ((Alpha < C & y == 1) | (Alpha > 0 & y == -1));
I_low = find( (Alpha < C & y == -1) | (Alpha > 0 & y == 1));
yGradient = - y.* (((y * y').* K) * Alpha - 1); [m1 , i1] = max(yGradient(I_up));
[M1 , i2] = min(yGradient(I_low)); i1 = I_up (i1);
i2 = I_low(i2); end %Solving the two-variables optimization problem
function solveOptimization(X, y, C)
global Alpha K i1 i2 E
alpha1_old = Alpha(i1);
alpha2_old = Alpha(i2);
y1 = y(i1);
y2 = y(i2); % x1 = X(i1,:)';
% x2 = X(i2,:)';
beta11 = K(i1,i1); beta22 = K(i2,i2); beta12 = K(i1,i2);
id =[1: length(Alpha)];
id([i1 i2]) = [];
beta1 = sum( y(id).*Alpha(id).*K(id,i1));
beta2 = sum( y(id).*Alpha(id).*K(id,i2)); E = beta1 - beta2 + alpha1_old * y1 * (beta11 - beta12) +alpha2_old*y2 * (beta12 - beta22) - y1 + y2;
kk = beta11 + beta22 - 2 * beta12;
alpha2_new_unc = alpha2_old + (y2 * E)/kk; if y1 ~= y2
L = max([0 , alpha2_old - alpha1_old]);
H = min([C, C - alpha1_old + alpha2_old]);
else
L = max([0 , alpha1_old + alpha2_old - C]);
H = min([C, alpha1_old + alpha2_old]);
end if alpha2_new_unc > H
alpha2_new = H;
elseif alpha2_new_unc < L
alpha2_new = L;
else
alpha2_new = alpha2_new_unc ;
end alpha1_new = alpha1_old + y1 * y2 * (alpha2_old - alpha2_new); Alpha(i1) = alpha1_new;
Alpha(i2) = alpha2_new; % for i=1:length(E)
% E(i) = sum(y .* Alphas .* K(i,:)) - b - y(i);
% end
%
%
% E1 = E(i1);
% E2 = E(i2);
%
% b1 = E1 + y1 * (a1 - alph1) * K(i1,i1) + y2 * (a2 - alph2) * K(i1,i2) - b;
% b2 = E2 + y1 * (a1 - alph1) * K(i1,i2) + y2 * (a2 - alph2) * K(i2,i2) - b;
%
% if b1 == b2
% b = b1;
% else
% b = mean([b1 b2]);
% end % w = w - y1 * (alpha1_new -alpha1_old) * X(i1,:)' - y2 * (alpha2_new -alpha2_old) * X(i2,:)'; end
clear
X = []; Y=[];
figure;
% Initialize training data to empty; will get points from user
% Obtain points froom the user:
trainPoints=X;
trainLabels=Y;
clf;
axis([-5 5 -5 5]);
if isempty(trainPoints)
% Define the symbols and colors we'll use in the plots later
symbols = {'o','x'};
classvals = [-1 1];
trainLabels=[];
hold on; % Allow for overwriting existing plots
xlim([-5 5]); ylim([-5 5]); for c = 1:2
title(sprintf('Click to create points from class %d. Press enter when finished.', c));
[x, y] = getpts; plot(x,y,symbols{c},'LineWidth', 2, 'Color', 'black'); % Grow the data and label matrices
trainPoints = vertcat(trainPoints, [x y]);
trainLabels = vertcat(trainLabels, repmat(classvals(c), numel(x), 1));
end end C = 10;
par = SMOforSVM(trainPoints, trainLabels , C );
p=length(par.b); m=size(trainPoints,2);
if m==2
% for i=1:p
% plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')
% hold on
% end
k = -par.w(1)/par.w(2);
b0 = - par.b/par.w(2);
bdown=(-par.b-1)/par.w(2);
bup=(-par.b+1)/par.w(2);
for i=1:p
hold on
h = refline(k,b0(i));
set(h, 'Color', 'r')
hdown=refline(k,bdown(i));
set(hdown, 'Color', 'b')
hup=refline(k,bup(i));
set(hup, 'Color', 'b')
end
end
xlim([-5 5]); ylim([-5 5]);
以上代码结果写的比较粗糙,可能不稳定,我重新贴了一个新的代码:
http://www.cnblogs.com/huadongw/p/4994657.html
sequential minimal optimization,SMO for SVM, (MATLAB code)的更多相关文章
- Sequential Minimal Optimization (SMO) 算法
SVM 最终关于 $a$ 目标函数为凸优化问题,该问题具有全局最优解,许多最优化算法都可以解决该问题,但当样本容量相对很大时,通常采用 SMO 算法(比如 LIBSVM),该算法为启发式算法,考虑在约 ...
- 支持向量机的smo算法(MATLAB code)
建立smo.m % function [alpha,bias] = smo(X, y, C, tol) function model = smo(X, y, C, tol) % SMO: SMO al ...
- SMO优化算法(Sequential minimal optimization)
原文:http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988419.html SMO算法由Microsoft Research的John C. ...
- Jordan Lecture Note-8: The Sequential Minimal Optimization Algorithm (SMO).
The Sequential Minimal Optimization Algorithm (SMO) 本文主要介绍用于解决SVM对偶模型的算法,它于1998年由John Platt在论文“Seque ...
- Support Vector Machine (2) : Sequential Minimal Optimization
目录 Support Vector Machine (1) : 简单SVM原理 Support Vector Machine (2) : Sequential Minimal Optimization ...
- Sequential Minimal Optimization: A Fast Algorithm for Training Support Vector Machines 论文研读
摘要 本文提出了一种用于训练支持向量机的新算法:序列最小优化算法(SMO).训练支持向量机需要解决非常大的二 次规划(QP)优化问题.SMO 将这个大的 QP 问题分解为一系列最小的 QP 问题.这些 ...
- Sequential Minimal Optimization(SMO,序列最小优化算法)初探
什么是SVM SVM是Support Vector Machine(支持向量机)的英文缩写,是上世纪九十年代兴起的一种机器学习算法,在目前神经网络大行其道的情况下依然保持着生命力.有人说现在是神经网络 ...
- SMO(Sequential Minimal Optimization) 伪代码(注释)
Algorithm: Simplified SMO 这个版本是简化版的,并没有采用启发式选择,但是比较容易理解. 输入: C: 调和系数 tol: 容差 (tolerance) max passes: ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
随机推荐
- Java自制人机小游戏——————————剪刀、石头、布
package com.hello.test; import java.util.Scanner; public class TestGame { public static void main(St ...
- CALayer总结(一)
1.geometryFlipped 设置为yes,则子图层或者子视图本来相对于左上角放置 改为 相对于左下角放置: 2.contents 3.contentGravity: kCAGravityC ...
- poj 2954 Triangle(Pick定理)
链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- Python基础学习笔记(五)常用字符串内建函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-strings.html 3. http://www.liaoxu ...
- 高效使用Vector
参考网页: http://www.cnblogs.com/biyeymyhjob/archive/2013/05/11/3072893.html#undefined 1.初始化的时候,最好先用rese ...
- CISCO VPN出现网关报错
今天尝试使用发现报错: 重启VPN服务即可
- 十种JAVA排序算法实例
一.冒泡(Bubble)排序 复制代码 代码如下: void BubbleSortArray() { for(int i=1;i<n;i++) { for ...
- JavaSE复习_3 继承
△先默认初始化,在显示初始化,在构造函数初始化 △继承的弊端:代码的耦合性增加了. △子类不能继承父类的构造方法. △子类会拥有父类的私有成员变量,但是必须通过get,set方法访问. △super不 ...
- 如何查看与刷新DNS本地缓存
如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...
- iOS开发之Xcode 6更新默认不支持armv7s架构
最近一次的Xcode 6更新默认不再支持arm7s架构,究竟是要废除不用呢还是仅仅只是一个疏忽? 目前的Xcode 6配置里定义${ARCHS_STANDARD}为armv7, arm64,当然这个定 ...