C++ 或 MATLAB 上的 SVM- SMO 算法实现

SVM- SMO algorithm implementation on C++ or MATLAB

谁能帮我找出以下代码没有在 MATLAB 上收敛的错误?请注意 k 表示内核并有其自身的功能。

我尝试用图中的算法实现了:

我的实现:

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

一个大错误是

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

并不意味着"if g(x) then a=f(x)",而是"a=f(x) considering only such x's that g(x) is true"。

所以

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

(以及下一个 if)不是操作的有效实现

i = arg max SOMETHING   subject to CONSTRAINTS

你应该做一些

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