user3406305
user3406305

Reputation: 7

SVM- SMO algorithm implementation on C++ or MATLAB

Can somebody help me to find the error of not having the following code converge on MATLAB? Please note that k means kernel and has its own function.

I tried to implement it using the algorithm in the picture:

enter image description here

My implementation:

while 1

    num_changed_alphas = 0;
    %step 4
     if (Y .* alphas < B)   
    [Yigi , i_WS] = max(Y .* g)
     end
     %step 5
     if (A < Y .* alphas)   
    [Yjgj , j_WS] = max(Y .* g)
     end
     %step 6
     if ( (Yigi - Yjgj ) <= eps )
         num_changed_alphas = num_changed_alphas + 1;
         break;

     end
     %step 7
     lambda = min (B(i_WS) - Y(i_WS) .* alphas(i_WS), Y(j_WS) .* alphas(j_WS) - A(j_WS) );
     lambda = min (lambda, (Yigi - Yjgj) ./ (K(i_WS,i_WS) + K(j_WS,j_WS) - 2* K(j_WS,i_WS)) );

     g = sum (g - lambda .* Y .* K (:,i_WS) + lambda .* Y .* K (:,j_WS));

     alphas = alphas + Y * lambda;

    fprintf('.');
    dots = dots + 1;
    if dots > 75
        dots = 0;
        fprintf('\n');
    end
    if exist('OCTAVE_VERSION')
        fflush(stdout);
    end
end %while

Upvotes: 0

Views: 739

Answers (1)

lejlot
lejlot

Reputation: 66775

One big mistake is that

a=f(x) subject to g(x)

does not mean "if g(x) then a=f(x)", but rather "a=f(x) considering only such x's that g(x) is true".

so

if (Y .* alphas < B)   
    [Yigi , i_WS] = max(Y .* g)
end

(as well as next if) is not a valid implementation of operation

i = arg max SOMETHING   subject to CONSTRAINTS

You should do something among the lines of

selected = (Y .* alphas < B) 
[Yigi , i_WS] = max(Y(selected) .* g(selected))

Upvotes: 1

Related Questions