finding Bias in dual form SVM using mean of support vector

5 views (last 30 days)
Hi , here i am trying to implement Soft margin SVM using CVX
After getting the set of alpha using CVX i try to set the constrain of alpha to a set of support vector as S , then calculate the weight (w)
Now my problem is i want to calculate the bias b by using the mean of support vector but dont actually know how to implement that formula into matlab code
function dual = svm_train_dual(X_train, y_train, C)
%r = 800 , l = 200
[r,l] = size(X_train);
H = zeros(r,r);
q = ones(r,1);
for a = 1:r
for b = 1:r
H(a,b) = X_train(a,:)*X_train(b,:)';
end
end
cvx_begin
variable alp(r);
% minimize((1/2)*alp'*H*alp - q'*alp);
minimize(0.5*quad_form(y_train'.*alp,H) - q'*alp);
subject to
alp >= 0;
alp <= C/r;
y_train * alp == 0;
cvx_end
w = zeros(1,l);
b = 0;
%constraint alp to 1*10^-2 < alp < (C/r) - 1*10^-5 as a support vector
%S is set of support vector
S = alp(alp > 1*10^-5 | alp< (C/r) - 1*10^-5);
w = (S'.*y_train)*(X_train);
% S = alp(alp > 1*10^-5 | alp < (C/r) - 1*10^-5)
b = ?????????????;
dual.w = w;
dual.b = b;
dual.alp = alp;
dual.S = S;
end

Answers (1)

Shubham Rawat
Shubham Rawat on 14 Sep 2020
Hi Kien,
Here Bias will be :
b = mean(y_train - w'*X_train);
% here you have already calculated weight vector

Categories

Find more on Automotive in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!